[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.app

 

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

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

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"
*/
相關文章
相關標籤/搜索