Asynchronous Injection

Asynchronous Injection

8928b5277aa7234dbca4ff782952343a?s=128

funnelbit

June 22, 2016
Tweet

Transcript

  1. 3.
  2. 7.

    .PEVMF @ProducerModule
 final class AppProducerModule {
 @Produces
 ListenableFuture<UserData> getUserData() {


    // ~ ͳΜ͔ΊͬͪΌ͕͔͔࣌ؒΔॲཧ ~
 return Futures.immediateFuture(new UserData("userName"));
 }
 
 @Produces
 String userName(UserData userData) { return userData.name; // Activity ͸͜Ε͕΄͍͠
 }
  3. 8.

    .PEVMF @ProducerModule
 final class AppProducerModule {
 @Produces
 ListenableFuture<UserData> getUserData() {

    // 1
 // ~ ͳΜ͔ΊͬͪΌ͕͔͔࣌ؒΔॲཧ ~
 return Futures.immediateFuture(new UserData("userName"));
 }
 
 @Produces
 String userName(UserData userData) { // 2 return userData.name; // Activity ͸͜Ε͕΄͍͠
 }
  4. 9.

    .PEVMF @Module
 public class ExecutorModule {
 @Provides
 @Production
 static Executor

    executor() {
 return Executors.newCachedThreadPool();
 }
 }
  5. 11.

    "DUJWJUZ … ListenableFuture<String> userDataListenableFuture = DaggerAppProducerComponent.create().userName();
 Futures.addCallback( userDataListenableFuture, new FutureCallback<String>()

    {
 @Override
 public void onSuccess(String result) {
 Log.e("name", result);
 }
 
 @Override
 public void onFailure(Throwable t) {
 Log.e("failure", t.toString());
 }
 }); …
  6. 13.

    .PEVMF @ProducerModule
 public class AppProducerLazyModule {
 @Produces @Normal
 ListenableFuture<UserData> provideNUserData()

    {
 return Futures.immediateFuture(new UserData("normal"));
 }
 
 @Produces @Special
 ListenableFuture<UserData> provideSUserData() {
 return Futures.immediateFuture(new UserData("special"));
 } 
 @Produces
 ListenableFuture<UserData> provideUserData( @Normal Producer<UserData> nProducer, @Special Producer<UserData> sProducer) {
 return sProducer.get();
 }
 }
  7. 14.

    @ProducerModule public class MyModule { @Produces ListenableFuture<A> a() { …

    } @Produces ListenableFuture<B> b(A a) { … } @Produces ListenableFuture<C> c(B b) { … } @Produces @Delayed ListenableFuture<C> delayedC(A a, Producer<C> c) { … return c.get(); } }
  8. 15.

    @ProducerModule public class MyModule { @Produces ListenableFuture<A> a() { …

    } // 1 @Produces ListenableFuture<B> b(A a) { … } // 3 @Produces ListenableFuture<C> c(B b) { … } // 4 @Produces @Delayed ListenableFuture<C> delayedC(A a, Producer<C> c) { … return c.get(); } // 2 }
  9. 17.

    .PEVMF @ProducerModule
 final class AppProducerModule {
 @Produces
 ListenableFuture<UserData> getUserData() {


    // ~ ͳΜ͔ΊͬͪΌ͕͔͔࣌ؒΔॲཧ ~
 throw new IllegalStateException(); // Θ͟ͱམͱ͢
 }
 
 @Produces
 String userName(Produced<UserData> userData) {
 try {
 return userData.get().name;
 } catch (ExecutionException e) {
 e.printStackTrace();
 return "Կ͔͕ى͖ͨ";
 }
 }
 }