package com.trello.navi2.rx;

import android.support.annotation.NonNull;
import com.trello.navi2.Event;
import com.trello.navi2.Listener;
import com.trello.navi2.NaviComponent;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Cancellable;

final class NaviOnSubscribe<T> implements ObservableOnSubscribe<T>{

  private final NaviComponent component;

  private final Event<T> event;

  NaviOnSubscribe(NaviComponent component, Event<T> event) {
    this.component = component;
    this.event = event;
  }

  @Override public void subscribe(final ObservableEmitter<T> emitter) throws Exception {
    EmitterListener listener = new EmitterListener(emitter);
    emitter.setCancellable(listener);
    component.addListener(event, listener);
  }

  class EmitterListener implements Listener<T>, Cancellable {
    private final ObservableEmitter<T> emitter;

    public EmitterListener(ObservableEmitter<T> emitter) {
      this.emitter = emitter;
    }

    @Override public void call(@NonNull T t) {
      emitter.onNext(t);
    }

    @Override public void cancel() throws Exception {
      component.removeListener(this);
    }
  }
}

@@ -276,29 +276,80 @@
 vex.
-functions.Cancellable
+disposables.Disposable;%0Aimport java.util.concurrent.atomic.AtomicBoolean
 ;%0A%0Af
@@ -414,32 +414,24 @@
 ribe%3CT%3E%7B%0A%0A  
-private 
 final NaviCo
@@ -452,24 +452,16 @@
 ent;%0A%0A  
-private 
 final Ev
@@ -762,23 +762,22 @@
 tter.set
-Cancell
+Dispos
 able(lis
@@ -857,16 +857,38 @@
 istener 
+extends AtomicBoolean 
 implemen
@@ -907,15 +907,14 @@
 T%3E, 
-Cancell
+Dispos
 able
@@ -924,16 +924,8 @@
     
-private 
 fina
@@ -1162,36 +1162,62 @@
 oid 
-cancel() throws Exception
+dispose() %7B%0A      if (compareAndSet(false, true))
  %7B%0A
+  
     
@@ -1250,16 +1250,95 @@
 (this);%0A
+      %7D%0A    %7D%0A%0A    @Override public boolean isDisposed() %7B%0A      return get();%0A
     %7D%0A  

Optimize allocations in NaviOnSubscribe