简体   繁体   English

RxJS5中shareReplay(1)的模式

[英]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 ,但现在我正在努力获得我想要的行为,即:

  • Create an observable 创建一个可观察的
  • Subscribe to the observable, and the observable produces a value 订阅observable,observable产生一个值
  • Subscribe to the observable a second time, and I get the same first value 第二次订阅observable,我得到相同的第一个值
  • Observable produces a second value, and both subscriptions get the second value Observable产生第二个值,两个订阅都获得第二个值

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 . shareReplaymulticast与操作者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添加取消订阅。
  • you can have a look at the specs here http://reactivex.io/rxjs/test-file/spec-js/operators/publishReplay-spec.js.html . 你可以看看这里的规格http://reactivex.io/rxjs/test-file/spec-js/operators/publishReplay-spec.js.html See line 127 onwards 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: 关于你的其余问题:

  • I think we all want that 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文档,您可以在其中找到有关冷热可观测量的解释。
  • Here , and here are some examples of resources. 在这里 ,并在这里是资源的一些例子。

Follows my own present understanding of the matter: 遵循我自己目前对此事的理解:

  • subjects are hot (mostly anyways, as you could argue that a replay subject has a behaviour closer to than of a cold observable) 受试者很热(大多数情况下反正,因为你可能认为重播主题的行为比冷观察者更接近)
  • all observables are cold, unless made explicitly otherwise. 除非明确另有说明,否则所有可观察物都是冷的。
  • among the explicit ways to make a cold observable hot, you have the multicast operator and its derivatives share , publish , shareReplay etc. Those operators internally all involve subjects. 在使用冷可观察热点的明确方法中,您可以使用multicast运算符及其衍生产品sharepublishshare shareReplay等。这些运算符内部都涉及主题。
  • Note that it does not have to be visible to you that those operators were used. 请注意,您不必看到使用这些运算符。 But in that case, the API or documentation should explicitly tell you. 但在这种情况下,API或文档应明确告诉您。 For instance, 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
  • to the hot/cold dichotomy you have to add the connectable kind which till it is connected, is neither hot nor cold. 对于热/冷二分法,你必须添加connectable类型,直到它连接,既不热也不冷。
  • defer always give rise to a cold observable. defer总会引起冷漠的观察。
  • lastly some operators do not change the nature of the observable, but do create hot observables internally and pass them on in their stream. 最后,一些运算符不会改变observable的性质,但会在内部创建热的observable并在其流中传递它们。 That is the case for instance of 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM