Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous Injection

Asynchronous Injection

funnelbit

June 22, 2016
Tweet

More Decks by funnelbit

Other Decks in Technology

Transcript

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


    // ~ ͳΜ͔ΊͬͪΌ͕͔͔࣌ؒΔॲཧ ~
 return Futures.immediateFuture(new UserData("userName"));
 }
 
 @Produces
 String userName(UserData userData) { return userData.name; // Activity ͸͜Ε͕΄͍͠
 }
  2. .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 ͸͜Ε͕΄͍͠
 }
  3. .PEVMF @Module
 public class ExecutorModule {
 @Provides
 @Production
 static Executor

    executor() {
 return Executors.newCachedThreadPool();
 }
 }
  4. "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());
 }
 }); …
  5. .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();
 }
 }
  6. @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(); } }
  7. @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 }
  8. .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 "Կ͔͕ى͖ͨ";
 }
 }
 }