[RxJS] ReplaySubject

A ReplaySubject caches its values and re-emits them to any Observer that subscrubes late to it. Unlike with AsyncSubject, the sequence doesn't need to be completed for this to happen.

 

The normal subject won't emit the value before subscribe.

var subject = new Rx.Subject();

subject.onNext(1);

subject.subscribe(
  (x)=>{
    console.log("Receive the value: " + x);
  }
)

subject.onNext(2);
subject.onNext(3);

/*
"Receive the value: 2"
"Receive the value: 3"
*/

 

The ReplaySubject will cache the values:

var subject = new Rx.ReplaySubject();

subject.onNext(1);

subject.subscribe(
  (x)=>{
    console.log("Receive the value: " + x);
  }
)

subject.onNext(2);
subject.onNext(3);

/*
"Receive the value: 1"
"Receive the value: 2"
"Receive the value: 3"
*/

 

posted @ 2016-08-18 17:52  Zhentiw  阅读(371)  评论(0编辑  收藏  举报