These are chat archives for ReactiveX/RxJava

24th
Feb 2016
David Stemmer
@weefbellington
Feb 24 2016 20:47
here is an rxjava pattern that I'm not familiar with -- inside an Observable.OnSubscribe implementation, I see a block like this:
public void call(final Subscriber subscriber) {
    // some code here...
    subscriber.add(Subscriptions.create(new Action0() {
        someService.removeListener(listener);
    });
    someService.addListener(listener);
}
is this an accepted/common pattern for removing a listener when an observable is unsubscribed
Dorus
@Dorus
Feb 24 2016 20:48
subscriber.add acts like a compositeSubscription.
Looks correct at first sight.
I do wonder what the goal of that code it, all it does is binding the lifetime of the listener to the lifetime of the observable.
David Stemmer
@weefbellington
Feb 24 2016 20:56
there is more to it, it calls an external payment service and sets up a listener object to listen for a result. The listener object pushes the result to onNext. I suppose that this is supposed to unsubscribe the internal listener if the Observable is unsubscribed from
Dorus
@Dorus
Feb 24 2016 20:57
ah right, missed the // some code here....
o
David Stemmer
@weefbellington
Feb 24 2016 21:01
the pattern is pretty interesting though...I see that Scheduler.Worker implements Subscription so you could do some work on a thread or an event loop and bail when unsubscribe is called
maybe that's obvious but it wasn't to me ¯_(ツ)_/¯
Dorus
@Dorus
Feb 24 2016 21:14
I've seen the pattern before so i was familiar with it, but the first time i strumbled on it, it was pretty confusing also.
And that was in C#, the scheduler.Schedule() they have is slightly more complicated, with recursive scheduling and more fun stuff.
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Feb 24 2016 21:34
@weefbellington subscriber implements Subscription and unfortunately add() is non-obviously named method to release resources when Subscription will be unsubscribed
Dorus
@Dorus
Feb 24 2016 21:35
The weird part is that the RxJS people decided to ditch CompositeDisposable in favor of subscriber.add in the next major update.
David Stemmer
@weefbellington
Feb 24 2016 22:09
@artem-zinnatullin I had forgotten that Subscriber implements Subscription. Is there a use case for doing subscriber.add(secondarySubscriber)in Observable.OnSubscribe#call(subscriber)? Would all onNext, onError, onCompleted etc. calls that pass through the original subscriber be forwarded to the secondary subscriber?
Dorus
@Dorus
Feb 24 2016 22:13
@weefbellington no. The signature is add(Subscription s) and Subscription is an interface with only isUnsubscribed() and unsubscribe(), so it can't even forwards events.
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Feb 24 2016 22:15
@weefbellington nope, Subscription.add() is as I said just for triggering some code when origin subscription gets unsubscribed, usually it's a good place to remove listeners/etc and anything that may create memory leaks
thought it's not needed for most RxJava use cases
David Stemmer
@weefbellington
Feb 24 2016 22:18
ah. I should have looked more closely at the Subscription interface. OK, makes sense, even if it's a little unintuitive
Dorus
@Dorus
Feb 24 2016 22:18
Totally unintuitive yes.