Reputation: 26196
By default RxJs Subject is "Hot", but is it possible to create "Cold" Subject to get all the values propagated from it from the beginning?
i.e.:
let s = new Subject();
s.next(1);
s.next(2);
s.subscribe(n => console.log(n)); //to get here 1 2 3
s.next(3);
Upvotes: 4
Views: 1265
Reputation: 4775
You can use a ReplaySubject to do this. The one thing to keep in mind is that a ReplaySubject expects a number during creation to know how many values it should buffer. You cannot buffer all the elements.
const subject = new Rx.ReplaySubject(10);
subject.next("1");
subject.next("2");
subject.next("3");
subject.next("4");
subject.next("5");
subject.subscribe(
(val) => console.log(val)
);
subject.next("6");
// Logs out
// 1
// 2
// 3
// 4
// 5
// 6
jsbin: http://jsbin.com/rocofa/edit?js,console
Upvotes: 3
Reputation: 84754
ReplaySubject
behaves exactly as you describe. See the ReactiveX Subject docs for more details.
Upvotes: 1