最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between Cold observable and Replay subject? - Stack Overflow

programmeradmin1浏览0评论

I can see from the docs that:

Cold Observables emit the entire sequence of values from the start to every Observer.

and

ReplaySubject emits to any observer all of the items that were emitted by the source Observable(s), regardless of when the observer subscribes.

So what is the difference between these two concepts?

Thanks

I can see from the docs that:

Cold Observables emit the entire sequence of values from the start to every Observer.

and

ReplaySubject emits to any observer all of the items that were emitted by the source Observable(s), regardless of when the observer subscribes.

So what is the difference between these two concepts?

Thanks

Share Improve this question edited Nov 18, 2016 at 5:05 user3743222 18.7k5 gold badges73 silver badges75 bronze badges asked Mar 26, 2016 at 20:21 luanpedluanped 3,1982 gold badges29 silver badges40 bronze badges 1
  • Please watch this one egghead.io/lessons/… or this video channel9.msdn.com/Blogs/J.Van.Gogh/… – xgrommx Commented Mar 28, 2016 at 12:08
Add a comment  | 

2 Answers 2

Reset to default 14

Once the ReplaySubject subscribes to the source observable, the source begins emitting, and the ReplaySubject becomes a hot observable. From a subscriber standpoint, subscribing the the ReplaySubject it may not be obvious, as it gets all the previously emitted values. But the ReplaySubject only subscribes to the source once, so the only way for the subject to be able to re-emit those source values to later subscribers, is to cache those values.

So the difference would be that when subscribing straight to the source observable, the source re-emits the values for each subscriber, whereas with the ReplaySubject subscribing to the source, the source only emits once, and any subscriber to the subject, gets those cached values.

Consider the following source observable

var source = Rx.Observable.create(function(subscriber) {
  for (var i = 0; i < 2; i++) {
    subscriber.onNext(i);
    console.log("onNext: " + i);
  }
  subscriber.onCompleted();
});

If we subscribe directly to the source, we should see the set of logs twice

source.subscribe(function(value) {});
source.subscribe(function(value) {});
//onNext: 0
//onNext: 1
//onNext: 0
//onNext: 1

With the ReplaySubject, once it subscribes to the source, the source begins emitting, causing the hot subject to begin emitting and caching the values.

var subject = new Rx.ReplaySubject();
source.subscribe(subject);
setTimeout(function() {
  console.log('subscribe subject');
  subject.subscribe(function(value) { console.log('subscriber')});
  subject.subscribe(function(value) { console.log('subscriber')});
}, 100);
//onNext: 0
//onNext: 1
//subscribe subject
//subscriber
//subscriber
//subscriber
//subscriber

Here, you see the source begins emitting once it is subscribed to by the subject. But any subscriptions to the subject, doesn't cause the source to re-emit, as with the previous example where each new subscriber causes the source to re-emit.

To elaborate further on the previous answer, I would recommend you to have a look at another SO question which should help make things more clear : Hot and Cold observables : are there 'hot' and 'cold' operators?

Yes, if we talk about a replay(), i.e. without any arguments, the behaviour of a replay and a cold observable look similar. However, they are not the same.

For instance,

cold$ = Rx.Observable.just(1).map(function(){return Math.random();});
coldReplay$ = cold$.replay();
cold$.subscribe(function(x){console.log('cold:' + x);})
cold$.subscribe(function(x){console.log('cold:' + x);})
coldReplay$.subscribe(function(x){console.log('replay:' + x);});
coldReplay$.subscribe(function(x){console.log('replay:' + x);});

will never give you the same values when you subscribe to the replay and when you subscribe directly the cold observable.

Why is that should be clear from the link aforementioned. When you subscribe to a cold observable, you start from the first source down to the subscription. When you replay, you don't restart, the replay has kept in a buffer the values that were emitted, and pass directly immediately those values from the buffer and the new ones.

发布评论

评论列表(0)

  1. 暂无评论