Thanks Thomas, I implemented something similar to what you said (I think)
and it works fine ! All my activities are code splitted "automatically" now.

I used the AsyncProvider<T>.
I used a gin version compiled by gwtp team that include the AsyncProxy
(althouth that I do not use gwtp for severals resons, thanks to gwtp dev for
this :) ) .

@Ashton, you can get it from here : http://code.google.com/p/gwt-platform/
http://code.google.com/p/gwt-platform/downloads/detail?name=gin-r137.jar

The usage of AsyncProvider<T> is very simple. It's exactly like a normal
Provider<T> instead that you have to give an AsyncCallback to the get()
method.
example :

@Inject
Provider<MyClass> myProvider;

@Inject
AsyncProvider<MyClass> myAsyncProvider;

private MyClass instance1;
private MyClass instance2;

public void myMethod() {
  // To get an instance without code splitting
  instance1 = myProvider.get();

  // To get an instance with code splitting
  myAsyncProvider.get(new AsyncCallback<MyClass>() {
 @Override
public void onSuccess(MyClass result) {
     instance2 = result;
  }
 }
 @Override
public void onFailure(Throwable caught) {
 }
  }

In the previous example, instance1 is instanciated without code splitting,
instance2 with code splitting.
This is just a small example to show you the syntax. This example is very
stupid since if you declare a class with a class Provider<MyClass> in the
same file than an AsyncProvider<MyClass>, MyClass code will not be code
splitted.

Now, just for people who would like to know how to implement an
ActivityAsyncProxy, my implementation looks like this : (And it might really
not be the best one. Thomas, maybe that you have a better code to share ;) )

public class ActivityAsyncProxy<T> implements Activity {

 @Inject
private AsyncProvider<T> provider;
 private boolean canceled = false;
private Activity impl;

@Override
 public String mayStop() {
if (impl != null) return impl.mayStop();
 return null;
}

@Override
 public void onCancel() {
if (impl != null) {
 impl.onCancel();
} else {
 canceled = true;
}
 }

 @Override
public void onStop() {
 if (impl != null) {
impl.onStop();
 } else {
canceled = true;
 }
}

@Override
 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
 provider.get(new AsyncCallback<T>() {

 @Override
public void onSuccess(T result) {
 // Do not starts loaded activity if it has been canceled
 if (!canceled) {
impl = (Activity) result;
 impl.start(panel, eventBus);
}
 }

 @Override
public void onFailure(Throwable caught) {
 // TODO : send error message
}
 });
}
}

Now, in my ActivityMapper :

public class RootActivityMapper implements ActivityMapper {

@Inject
 Provider<ActivityAsyncProxy<LoginActivity>> loginActivityProvider;

@Inject
 Provider<ActivityAsyncProxy<ProfileActivity>> profileActivityProvider;

@Inject
 Provider<ActivityAsyncProxy<PrivacyActivity>> privacyActivityProvider;

@Override
 public Activity getActivity(Place place) {
if (place instanceof LoginPlace) return loginActivityProvider.get();
 if (place instanceof ProfilePlace) return profileActivityProvider.get();
 if (place instanceof PrivacyPlace) return privacyActivityProvider.get();

return null;
 }
}

And that's all.

Well, Just for information, I created my custom provider for
ActivityAsyncProxy :

public class ActivityAsyncProxyProvider<T extends Activity> implements
Provider<ActivityAsyncProxy<T>> {

@Inject
 Provider<ActivityProxy<T>> provider;

 @Override
public ActivityAsyncProxy<T> get() {
 return provider.get();
}
}

Now, in my ActivityMapper, injection are less verbose :

@Inject
ActivityAsyncProxyProvider<LoginActivity> loginActivityProvider;

@Inject
ActivityAsyncProxyProvider<ProfileActivity> profileActivityProvider;

@Inject
ActivityAsyncProxyProvider<PrivacyActivity> privacyActivityProvider;


Thanks Thomas, you helped me a lot this week with all your tips ! :)

Nicolas

2010/11/6 Ashton Thomas <[email protected]>

> Does anyone have some words for implementing the AsyncProvider<T>
> (Still in trunk as Thomas points out)??
>
> I have not see any code showing how to implement this so I am a little
> lost on where to start.
>
> How do we need to use AsyncProvider and what else needs to be change /
> restructure anything?
>
> Is the AsyncProvider even usable right now if we compile from source?
>
>
>
> On Nov 6, 9:27 am, Thomas Broyer <[email protected]> wrote:
> > On 6 nov, 01:24, Nicolas Antoniazzi <[email protected]>
> > wrote:
> >
> > > Hello,
> >
> > > I am trying to use CodeSplitting with Activites on 2.1.
> > > I read this very interresting threadhttp://
> code.google.com/p/google-web-toolkit/issues/detail?id=5129and
> > > T.Broyer says that the best approach is to use an AsyncProxy for
> Activities.
> >
> > I certainly didn't say it's the best approach (just that Jarod's code
> > wasn't adding anything to AsyncProxy).
> >
> > > It makes sense but I have problem with it since my "Activities" are
> binded
> > > with Gin. AsyncProxy uses GWT.create() to instantiate the concrete
> types and
> > > all my @Inject in my Activities are thus not initialized.
> >
> > That's probably the reason why GIN added AsyncProvider<?>s
> > (unfortunately not released yet)
> >
> > > Does anyone tried to mix new Activity concepts with "Code Splitting" ?
> And
> > > do you know if it could be compatible with "Gin activities" ?
> >
> > I started coding the ActivityAsyncProxy I talked about in the issue
> > tracker (i.e. don't even call start() on the activity if it's
> > cancelled before being "downloaded"), but haven't yet tried it. I
> > think (read: I *suppose*) that for it to work with GIN, your concrete
> > implementations would have to use GIN's AsyncProvider, or get the
> > Activity from the Ginjector from within a GWT.runAsync.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-web-toolkit%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to