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

查看:20
本文介绍了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 产生第二个值,两个订阅都获得第二个值

我如何使用 RxJS5 实现这一点?

How do I implement this with RxJS5?

总的来说,我认为我对 RxJS 操作符非常了解,但是整个冷、热、发布、连接对我来说是相当不清楚的.是否有很好的参考资料来说明如何找到我拥有的可观察对象类型,以便我可以以合乎逻辑的方式找出为什么订阅没有获得值,或者为什么要多次执行可观察对象?

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:

变更日志:noreferr="nofollow".com/ReactiveX/rxjs/blob/892700dd4f5d5e5f9ae9276ede32208f4390c5e9/CHANGELOG.md#540-2017-05-09

准系统文档:http://reactx.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 是带有 ReplaySubjectmulticast 运算符,后跟 refCount.所以我敢打赌 publishReplay(x).refCount() 应该非常接近 shareReplay 行为.无论如何,publishReplay 已经为您提供了您提到的所有要点.refCount 在没有更多观察者时添加取消订阅(refCount 减少到 0).
  • 您可以在此处查看规范 http://reactx.io/rxjs/test-file/spec-js/operators/publishReplay-spec.js.html.请参阅第 127 行之后的 var replayed = 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 文档,您可以在其中找到有关热和冷 observable 的解释.
  • 这里此处是一些资源示例.
  • 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:

  • 主题很热(大多数情况下,因为您可能会争辩说重放主题的行为更接近于冷的可观察对象)
  • 除非另有明确说明,否则所有 observables 都是冷的.
  • 在使冷 observable 变热的显式方法中,您有 multicast 运算符及其派生词 sharepublishshareReplay 等,内部的那些算子都涉及到主题.
  • 请注意,您不必看到使用了这些运算符.但在这种情况下,API 或文档应该明确告诉您.例如, Rx.Observable.fromEvent('input','click') 很热.你可以在它的实现中看到在某处有一个 share.
  • 在热/冷二分法中,您必须添加 connectable 类型,直到它连接起来,既不热也不冷.
  • defer 总是会产生一个冷的 observable.
  • 最后,一些操作符不会改变可观察对象的性质,但会在内部创建热可观察对象并将它们传递到它们的流中.groupBy 的例子就是这种情况.op1.op2.groupBy 是冷的,但它会发出热的 observables 作为结果流中的值.在这些情况下,只有文档(如果有)可以帮助您找到答案.否则,源代码和测试规范.或询问 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天全站免登陆