RxJS5中shareReplay(1)的模式 [英] Pattern for shareReplay(1) in RxJS5

查看:232
本文介绍了RxJS5中shareReplay(1)的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始玩RxJS5,现在看到不再有 shareReplay 方法。

I've started playing with RxJS5, and now see that there is no longer a shareReplay method.

我很可能在RxJS4中误用了 shareReplay ,但现在我很难得到我想要的行为,即:

It's quite possible that I often misused shareReplay in RxJS4, but now I'm struggling to get the behaviour that I want, i.e:


  • 创建一个observable

  • 订阅observable,observable生成一个值

  • 订阅第二次到observable,我得到相同的第一个值

  • Observable产生第二个值,两个订阅得到第二个值

  • Create an observable
  • Subscribe to the observable, and the observable produces a value
  • Subscribe to the observable a second time, and I get the same first value
  • Observable produces a second value, and both subscriptions get the second value

如何用RxJS5实现这个?

How do I implement this with RxJS5?

一般来说我觉得我对RxJS运算符了解得很好但是整个冷,热,发布,连接对我来说还不太清楚。有没有一个很好的参考,显示如何找到我有什么样的可观察量,以便我能够以逻辑的方式找出为什么订阅没有获取值,或者为什么一个observable被执行多次?

In general I think I understand the RxJS operators quite well, but the whole cold, hot, publish, connect is rather unclear to me. 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?

编辑

快乐新闻,shareReplay()又回到了RxJS 5.4.0:

Happy news, shareReplay() is back in RxJS 5.4.0:

更改日志: https://github.com/ReactiveX/rxjs/blob/892700dd4f5d5e5f9ae9276ede32208f4390c5e9/CHANGELOG.md#540-2017-05-09

Barebones文档: http://reactivex.io /rxjs/class/es6/Observable.js~Observable.html#instance-method-shareReplay

Barebones documentation: http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-shareReplay

推荐答案

那个参与Rxjs5的成员最好回答问题,但这是我的看法:

That question is best answered by members who participate in Rxjs5, but here is my take:


  • shareReplay 多播运算符,其中包含 ReplaySubject ,后跟 refCount 。所以我敢打赌 publishReplay(x).refCount()应该非常接近 shareReplay 行为。在任何情况下, publishReplay 已经为您提供了您提到的所有要点。当没有更多观察者( refCount 减少到0)时, refCount 会添加取消订阅。

  • 你可以看看这里的规格 http://reactivex.io/rxjs/test-file/spec-js/operators/publishReplay-spec.js.html 。请参阅第127行 var replaied = source.publishReplay(1).refCount(); ,这应该等同于您的 shareReplay(1)

  • shareReplay is the multicast operator with a ReplaySubject, followed by a refCount. So I would bet that publishReplay(x).refCount() should be quite close to the shareReplay behaviour. In any case, publishReplay already gives you all the points you mentioned. The refCount adds the unsubscription when there is no more observers (refCount decreased to 0).
  • you can have a look at the specs here http://reactivex.io/rxjs/test-file/spec-js/operators/publishReplay-spec.js.html. See line 127 onwards var replayed = source.publishReplay(1).refCount();, that should be equivalent to your shareReplay(1).

关于你的其余问题:


  • 我想我们都想要那个好的参考资料,展示如何找到我所拥有的那种可观察物...... 。有许多地方,包括Rxjs4文档,您可以在其中找到有关冷热可观测量的解释。

  • 此处这里是资源的一些例子。

  • I think we all want that 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.
  • Here, and here are some examples of resources.

遵循我自己目前对此事的理解:

Follows my own present understanding of the matter:


  • 受试者很热门(大多数情况下都是如此,因为你可能认为重播主题的行为比冷观察者更接近)

  • 除非明确另有说明,否则所有可观察对象都是冷的。

  • 在使用冷可观察热的明确方法中,你有多播运营商及其衍生产品分享发布 shareReplay 等那些运营商内部都涉及主题。

  • 请注意,它不一定对你来说是可见的。那些经营者被使用了。但在这种情况下,API或文档应明确告诉您。例如, Rx.Observable.fromEvent('input','click')很热。您可以在其实现中看到某处有分享

  • 到热/冷二分法,您必须添加可连接直到它连接的那种,既不热也不冷。

  • 推迟

  • 最后,一些运算符不会改变observable的性质,但会在内部创建热的observable并在它们的流中传递它们。例如 groupBy 就是这种情况。 op1.op2.groupBy 很冷,但它会在结果流中将热观察值作为值发出。在这些情况下,只有文档(如果有的话)可以帮助您找到答案。否则,源代码和测试规范。或询问SO。

  • 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.
  • 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. For instance, Rx.Observable.fromEvent('input','click') is hot. You can see in its implementation that there is a share somewhere.
  • to the hot/cold dichotomy you have to add the connectable kind which till it is connected, is neither hot nor cold.
  • defer always give rise to a cold observable.
  • lastly some operators do not change the nature of the observable, but do create hot observables internally and pass them on in their stream. That is the case for instance of groupBy. op1.op2.groupBy is cold, but it will emit hot observables as values in the resulting stream. 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.

这篇关于RxJS5中shareReplay(1)的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆