20

I'm trying to wrap some listener-pattern based API to an Observable. My code roughly looks like following.

def myObservable = Observable.create({ aSubscriber ->
    val listener = {event -> 
      aSubscriber.onNext(event);                
    }
    existingEventSource.addListener(listener)
})

However, I want my observable to immediately remove the listener from the underlying existingEventSource when the observer calls subscription.unscribe(). How could I achieve this goal?

victorx
  • 3,267
  • 2
  • 25
  • 35

1 Answers1

31

The Subscriber abstract class actually has a method add which lets you add Subscriptions which will be unsubscribed with the subscriber.

def myObservable = Observable.create({ aSubscriber ->
    val listener = {event -> 
      aSubscriber.onNext(event);                
    }
    existingEventSource.addListener(listener)

    // Adds a lambda to be executed when the Subscriber un-subscribes from your Observable
    aSubscriber.add(Subscriptions.create(() -> existingEventSource.removeListener(listener)));
})

Think of aSubscriber as the Observer that subscribed to your Observable; we'll call it a Subscriber. As long as the Subscriber is still subscribed to the Observable, the Observable can emit values. But when that Subscriber un-subscribed then it should stop. But if we want to get notified when the Subscriber unsubscribes we can register an Action to be run when it happens. This is what the add method is used for. As mentioned by @dwursteisen in the comments; you're basically registering a lambda that will be executed when the Subscriber un-subscribes.

It's also possible to have the Subscription be unsubscribe on a different Scheduler. See MainThreadSubscription from the rxanroid project for an example of how to achieve that .

Here's an example of how you'd use it for unsubscribing on the main thread

aSubscriber.add(new MainThreadSubscription() {
    @Override
    protected void onUnsubscribe() {
        existingEventSource.removeListener(listener);
    }
});
Miguel
  • 19,793
  • 8
  • 56
  • 46
  • Miguel, thanks for your answer, but I can't get it. Do you mind elaborating a little more, or pointing me to some related document or source code I can read? – victorx Nov 02 '14 at 06:04
  • Using the add method, your register a lambda that will be executed when your Observable is unsubscribe. – dwursteisen Nov 02 '14 at 13:44
  • @xwk, I added a little explanation which will hopefully give you a better idea what's going on. – Miguel Nov 03 '14 at 12:42
  • @MiguelLavigne. Thanks a lot. I understand now. But to be honest, this unsubscribing-hook mechanism is not as intuitive as other parts of RxJs to me. Not sure how it was designed this way. BTW, you might have a typo. I guess you mean "The Subscriber abstract class actually HAS a method" – victorx Nov 04 '14 at 05:51
  • Is it possible to have the lambda run on a different scheduler? – Kirill Rakhman Apr 08 '16 at 15:26
  • @KirillRakhman I've edit the response with an example and where you can find similar code to achieve that. – Miguel Apr 08 '16 at 15:50
  • Is the `unsubscribeOn` operator suited for that? – Kirill Rakhman Apr 08 '16 at 18:39
  • unsubscribeOn is different. If you want to unsubscribe on the ui thread for instance then you'll need to implement the `Scheduler` interface accordingly or you can use the rxandroid's project AndroidSchedulers.mainThread() located [here](https://github.com/ReactiveX/RxAndroid/blob/v1.0.0/rxandroid/src/main/java/rx/android/schedulers/AndroidSchedulers.java) – Miguel Apr 08 '16 at 20:05
  • Does anyone know how to accomplish the same thing in RxJS 5 (for JavaScript)? – HipsterZipster Jul 18 '17 at 18:31