JakeWharton / RxReplayingShare

An RxJava transformer which combines replay(1), publish(), and refCount() operators.
Apache License 2.0
626 stars 28 forks source link

Question: doesn't this implementation support only one stream? #7

Closed dragantl closed 8 years ago

dragantl commented 8 years ago

ReplayingShare keeps track of a static instance. This instance then keeps track of the subscribers.

Am I correct to understand that this basically is limited to only one stream?

If I have a stream of clicks and a stream of ticks, I am unable to use this implementation for both, correct?

public Observable<View> getClickObservable() {
    return RxView.clicks(mButton).compose(ReplayingShare.instance();
}

public Observable<long> getTickObservable() {
    return Observable.interval(1, TimeUnits.SECONDS).compose(ReplayingShare.instance();
}

@Override
public void onStart() {
    super.onStart();

    getTickObservable().subscribe(view -> Log.i(TAG, "Got a tick!"));
    getClickObservable().subscribe(view -> Log.i(TAG, "Got a click!"));
}

The syntax may be off but should convey a point. Won't ReplayingShare emit both clicks and ticks to both of the subscribers?

JakeWharton commented 8 years ago

The singleton instance of the transformer (aka instance()) is itself a factory for instances of the actual operator. Each stream gets its own copy because each stream invokes the factory inside of the compose() operator.

It's easy to write a test to prove the behavior: https://github.com/JakeWharton/RxReplayingShare/pull/8