[英]Pattern for shareReplay(1) in RxJS5
I've started playing with RxJS5, and now see that there is no longer a shareReplay
method. 我已经开始玩RxJS5,现在看到不再有shareReplay
方法了。
It's quite possible that I often misused shareReplay
in RxJS4, but now I'm struggling to get the behaviour that I want, ie: 很可能我经常在RxJS4中滥用shareReplay
,但现在我正在努力获得我想要的行为,即:
How do I implement this with RxJS5? 如何使用RxJS5实现此功能?
In general I think I understand the RxJS operators quite well, but the whole cold, hot, publish, connect is rather unclear to me. 总的来说,我认为我对RxJS运算符非常了解,但对我来说,整个冷,热,发布,连接都不太清楚。 Is there a good reference that shows how to find what kind of observable I have, so that I can find out in a logical manner why a subscribe is not getting values, or why an observable is being executed multiples times? 有没有一个很好的参考,显示如何找到我有什么样的可观察量,以便我能够以逻辑的方式找出为什么订阅没有获取值,或者为什么一个observable被执行多次?
EDIT 编辑
Happy news, shareReplay() is back in RxJS 5.4.0: 快乐新闻,shareReplay()又回到了RxJS 5.4.0:
Changelog: https://github.com/ReactiveX/rxjs/blob/892700dd4f5d5e5f9ae9276ede32208f4390c5e9/CHANGELOG.md#540-2017-05-09 更新日志: https : //github.com/ReactiveX/rxjs/blob/892700dd4f5d5e5f9ae9276ede32208f4390c5e9/CHANGELOG.md#540-2017-05-09
Barebones documentation: http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-shareReplay Barebones文档: http : //reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-shareReplay
That question is best answered by members who participate in Rxjs5, but here is my take: 参与Rxjs5的成员最能回答这个问题,但这是我的看法:
shareReplay
is the multicast
operator with a ReplaySubject
, followed by a refCount
. shareReplay
是multicast
与操作者ReplaySubject
,随后进行refCount
。 So I would bet that publishReplay(x).refCount()
should be quite close to the shareReplay
behaviour. 所以我敢打赌, publishReplay(x).refCount()
应该非常接近shareReplay
行为。 In any case, publishReplay
already gives you all the points you mentioned. 无论如何, publishReplay
已经为您提供了您提到的所有要点。 The refCount
adds the unsubscription when there is no more observers ( refCount
decreased to 0). 当没有更多的观察者( refCount
减少到0)时, refCount
添加取消订阅。 var replayed = source.publishReplay(1).refCount();
见第127行以上var replayed = source.publishReplay(1).refCount();
, that should be equivalent to your shareReplay(1)
. ,这应该等同于你的shareReplay(1)
。 About the rest of your question: 关于你的其余问题:
good reference that shows how to find what kind of observable I have...
. 我想我们都想要那个good reference that shows how to find what kind of observable I have...
There are many places, including Rxjs4 documentation where you find explanations about hot and cold observables. 有许多地方,包括Rxjs4文档,您可以在其中找到有关冷热可观测量的解释。 Follows my own present understanding of the matter: 遵循我自己目前对此事的理解:
multicast
operator and its derivatives share
, publish
, shareReplay
etc. Those operators internally all involve subjects. 在使用冷可观察热点的明确方法中,您可以使用multicast
运算符及其衍生产品share
, publish
, share
shareReplay
等。这些运算符内部都涉及主题。 Rx.Observable.fromEvent('input','click')
is hot. 例如, Rx.Observable.fromEvent('input','click')
很热门。 You can see in its implementation that there is a share
somewhere. 您可以在其实现中看到某处有share
。 connectable
kind which till it is connected, is neither hot nor cold. 对于热/冷二分法,你必须添加connectable
类型,直到它连接,既不热也不冷。 defer
always give rise to a cold observable. defer
总会引起冷漠的观察。 groupBy
. 例如groupBy
就是这种情况。 op1.op2.groupBy
is cold, but it will emit hot observables as values in the resulting stream. op1.op2.groupBy
很冷,但它会在结果流中将热观察值作为值发出。 In those cases, only the documentation (if any) can help you find out. 在这些情况下,只有文档(如果有的话)可以帮助您找到答案。 Otherwise, the source code, and the test specs. 否则,源代码和测试规范。 Or asking on SO. 或者询问SO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.