Why is Rxjs publishReplay (1) .refCount () not playing?

Why does publishReplay (1) .refCount () not reproduce the last value for late subscribers?

a = new Rx.Subject(); b = a.publishReplay(1).refCount(); a.subscribe(function(x){console.log('timely subscriber:',x)}); a.next(1); b.subscribe(function(x){console.log('late subscriber:',x)}); 
 <script src="http://reactivex.io/rxjs/user/script/0-Rx.js"></script> 

Expected Result:

 timely subscribe: 1 late subscriber: 1 

Actual output

 timely subscriber: 1 
+6
source share
2 answers

This is because at the time you call a.next(1) , publishReplay(1) did not subscribe to its Observable source (Subject a in this case), and therefore the internal ReplaySubject will not get the value 1 .

In RxJS 5, the actual subscription between operators occurs when you sign at the end of the chain, which is b.subscribe(...) in this example. Cm:

Until you name subscribe() , the operators are bound using the lift() method, which simply takes an instance of the operator and assigns it to a new Observable. The operator.call() method, as you can see in the two links above, is called later when subscribing. Cm:

+9
source

Your first subscriber subscribes to a , since refCount first activates the stream, if there is at least one subscriber (who is not, because not b , but a signed), this is not active until your last loc.

 a = new Rx.Subject(); b = a.publishReplay(1).refCount(); b.subscribe(function(x){console.log('timely subscriber:',x)}); a.next(1); b.subscribe(function(x){console.log('late subscriber:',x)}); 
 <script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script> 
+1
source

Source: https://habr.com/ru/post/1014565/


All Articles