Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

javascript - Pattern for shareReplay(1) in RxJS5

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

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:

  • 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

How do I implement this with RxJS5?

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?

EDIT

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

Changelog: 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  • 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).

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.... 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:

  • 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...