JakeWharton / RxReplayingShare

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

new subscription gets last value from earlier subscription #4

Closed ralph-bergmann closed 8 years ago

ralph-bergmann commented 8 years ago

I'm not sure if it is the expected behavior or is it a bug. When I run the following code:

final Observable<Object> observable = Observable.create(new Observable.OnSubscribe<String>() {

    @Override
    public void call(final Subscriber<? super String> subscriber) {

        subscriber.onNext("1");
        subscriber.onNext("2");
        subscriber.onNext("3");
        subscriber.onCompleted();
    }
}).compose(ReplayingShare.instance());

observable
    .subscribe(new Action1<Object>() {

                   @Override
                   public void call(final Object text) {
                       Log.i(TAG, "onNext1: " + text);
                   }
               },
               new Action1<Throwable>() {

                   @Override
                   public void call(final Throwable throwable) {
                       Log.e(TAG, "onError1", throwable);
                   }
               },
               new Action0() {

                   @Override
                   public void call() {
                       Log.i(TAG, "onCompleted1");
                   }
               });

new Thread(new Runnable() {

    @Override
    public void run() {

        try {
            Thread.sleep(200);
        }
        catch (InterruptedException e) {
        }

        observable
            .subscribe(new Action1<Object>() {

                           @Override
                           public void call(final Object text) {
                               Log.i(TAG, "onNext2: " + text);
                           }
                       },
                       new Action1<Throwable>() {

                           @Override
                           public void call(final Throwable throwable) {
                               Log.e(TAG, "onError2", throwable);
                           }
                       },
                       new Action0() {

                           @Override
                           public void call() {
                               Log.i(TAG, "onCompleted2");
                           }
                       });
    }
}).start();

I get this output:

MainActivity: onNext1: 1
MainActivity: onNext1: 2
MainActivity: onNext1: 3
MainActivity: onCompleted1
MainActivity: onNext2: 3
MainActivity: onNext2: 1
MainActivity: onNext2: 2
MainActivity: onNext2: 3
MainActivity: onCompleted2

The second subscription get the last value from the first subscription. But I think the ouput should be:

MainActivity: onNext1: 1
MainActivity: onNext1: 2
MainActivity: onNext1: 3
MainActivity: onCompleted1
MainActivity: onNext2: 1
MainActivity: onNext2: 2
MainActivity: onNext2: 3
MainActivity: onCompleted2
JakeWharton commented 8 years ago

This is the desired behavior. If you don't want the last value cached and replaced to new subscribers then simply remove your use of this operator from the chain.