Search code examples
gwtgwt-gingwtp

GWT: Provider vs AsyncProvider


what is the difference between provider and AsyncProvider in GWT. My understanding is that both are used for code splitting and delayed instantiation. so I am not able to decide, in which case we go for Provider and not for AsyncProvider ?

Thanks in advance!


Solution

  • Provider is about:

    • delayed instantiation (but instantiation is instantaneous, synchronous); FYI, Dagger introduces a Lazy type for this very use case.
    • a factory of objects: unless the object is scoped, a call to get() will give you a new object each time
    • using shorter-lived scoped objects in longer-lived scopes: if you need access to request-scoped objects from a singleton-scoped (or session-scoped) object, you have to use a Provider, otherwise you'll get injected an object from the current request scope, which won't be usable for the next request. GIN only supports the Singleton scope os it doesn't really apply here, as it's then just a variation of the above factory use case; but on the server-side (with Guice, Spring DI, Dagger, etc.), where servlets or RequestFactory services/locators are singletons (or pseudo-singletons for RF) this is a key part of the DI framework.

    AsyncProvider is the same except it's asynchronous, wrapping a call to GWT.runAsync().

    In other words: only AsyncProvider is really about code splitting. Provider will have an impact on the code splitting output but no different than any factory (hand-coded, or GIN's AssistedInject).