简体   繁体   中英

Best GWT CodeSplitting design to encapsulate “modules”

I am currently faced with quite a challenging issue related to GWT codesplitting and was hoping for some help.

I currently work on a large legacy GWT application (pre-MVP days) and I am looking to code split this application based on the modules that the "portlets" (what we call the various composite widgets that we build our pages up with) are part of.

Currently our modules are just identified by the package that the portlet falls into but I am open to changing this to better suit a sound generic codesplitting design.

Any ideas on how I can design this code to indicate that a portlet / composite belongs to a specific "module" and then split the code so that the first time any portlet / composite within module X is loaded, the whole of module X is loaded?

Thanks

Hmm ... normally, it's quite simple but I guess that's not your real problem ...

Simply use this:

GWT.runAsync(new RunAsyncCallback() {
    public void onFailure(Throwable reason) {
        ...
    }
    public void onSuccess() {
        ...
    }
});

Everything within the onSuccess method will then be splitted in another javascript file which will then be loaded on demand.

In case you want to seperate composites from the rest of your code, simply put the creation of your composite inside this onSuccess method.

You also can nest GWT.runAsync methods, so you can split the part again in different parts, eg first GWT.runAsync loads module X, in constructor of module X you could do another runAsync which then loads your composize.

Of couse, there could be some dependencies between the part which make it difficult for the compiler to split but I have tested it with one of my projects (about 40k lines of code) and it worked like a charm.

Packagaing has little to do with code splitting, the main factor that makes code splitting work is little spaghetti entanglement between classes. If one class requires another and so on which eventually reaches and grabs all classes then code splitting cant very well break things up into many, because the very act of requiring the first means everything is required. If you separate your concerns aka loose coupling then you should be able to have something that is well suited to being splittable.

How about using GWTP - it's a good MVP framework and they provide you with automatic code splitting. You would have to reorganize your code base to use a presenter/view for each of the modules that you want to split. Then adding codesplitting is as simple as adding the following lines to your presenter:

@ProxyCodeSplit
@NameToken("firstpage")
public interface MyProxy extends ProxyPlace<FirstPagePresenter> {}

GWTP also has an Eclipse plugin that generates most of the boilerplate code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM