简体   繁体   English

最佳GWT CodeSplitting设计封装“模块”

[英]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. 我目前面临与GWT codesplitting相关的相当具有挑战性的问题,并希望得到一些帮助。

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. 我目前正在研究一个大型的遗留GWT应用程序(MVP之前的日子),我期待基于“portlets”(我们称之为构建我们页面的各种复合小部件)的模块对此应用程序进行代码拆分。部分。

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. 目前我们的模块只是由portlet所包含的软件包标识,但我愿意更改它以更好地适应声音通用的代码分离设计。

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? 关于我如何设计此代码以指示portlet /复合属于特定“模块”然后拆分代码以便第一次加载模块X中的任何portlet /复合,整个模块X被加载的任何想法?

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. 然后将onSuccess方法中的所有内容拆分为另一个javascript文件,然后根据需要加载该文件。

In case you want to seperate composites from the rest of your code, simply put the creation of your composite inside this onSuccess method. 如果您想从其余代码中分离复合材料,只需将复合材料的创建放在此onSuccess方法中。

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. 您还可以嵌套GWT.runAsync方法,因此您可以在不同的部分再次拆分部件,例如,首先GWT.runAsync加载模块X,在模块X的构造函数中,您可以执行另一个runAsync然后加载您的组合。

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. 当然,部件之间可能存在一些依赖关系,这使得编译器难以拆分,但我已经用我的一个项目(大约40k行代码)测试了它,它就像一个魅力。

Packagaing has little to do with code splitting, the main factor that makes code splitting work is little spaghetti entanglement between classes. Packagaing与代码分割没什么关系,使代码分裂工作的主要因素是类之间的意大利面纠缠。 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. 如何使用GWTP - 它是一个很好的MVP框架,它们为您提供自动代码分割。 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: 然后添加codesplitting就像向演示者添加以下行一样简单:

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

GWTP also has an Eclipse plugin that generates most of the boilerplate code. GWTP还有一个Eclipse插件,可以生成大部分样板代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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