29 Jan

GWT: client code optimization

I’d like to share with you one tip which I’ve learned only yesterday, in spite of the fact that you may already know it.

Our GWT project is written with a lot of modules that include initialization of child modules in their constructors which initialize a lot of another modules in their constructors and so on… Some modules can load different data for initial rendering, despite the fact that user won’t see them in current session, however this data and asynchronous calls are forcing application server to do unuseful job and weighty client code makes some delays on application start up in user’s browser.

Here is the solution! Recently I read about ReadAsyncCallback interface which allows to easily make deferred calls right in the client code. Just imagine, it allows you to make deferred method call in a client code in the same way as you make requests to your remote services:

GWT.runAsync(new RunAsyncCallback() {
    public void onSuccess() {
      new SomeModule().show();
    }
    public void onFailure(Throwable ohNoes) {
    }
  });

This really simple method tells compiler to split your code into parts if it possible without any bad consequences.

To learn some details and limitations of this feature read the official documentation.