为什么Rxjs publishReplay(1).refCount()没有重放?

mar*_*sen 7 javascript rxjs rxjs5

为什么publishReplay(1).refCount()不重播后期订阅者的最后一个值?

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)});
Run Code Online (Sandbox Code Playgroud)
<script src="http://reactivex.io/rxjs/user/script/0-Rx.js"></script>
Run Code Online (Sandbox Code Playgroud)

预期产量:

timely subscribe: 1
late subscriber: 1
Run Code Online (Sandbox Code Playgroud)

实际输出

timely subscriber: 1
Run Code Online (Sandbox Code Playgroud)

mar*_*tin 9

这是因为在一次调用a.next(1)publishReplay(1)未签约其来源可观察(除a在这种情况下),因此内部ReplaySubject将不会收到值1.

在RxJS 5中,运算符之间的实际订阅发生在您b.subscribe(...)在此示例中链的末尾订阅时.看到:

直到你调用subscribe()运算符被链接,这要归功于lift()只接受运算符实例的方法并将其分配给新的Observable.operator.call()您在上面的两个链接中可以看到的方法稍后在订阅时调用.看到:


ols*_*lsn 1

您的第一个订阅者订阅a,因为refCount当至少有 1 个订阅者(其中没有订阅者,因为没有ba已订阅)时首先激活流,所以直到您的最后一个 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)});
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
Run Code Online (Sandbox Code Playgroud)