AsyncSubject

The AsyncSubject has a capacity of one, which means we can emit a ton of values, but only the latest one is something that is stored. It isn't really lost, either, but you won't see it unless you complete the stream. Let's look at a piece of code that illustrates just this:

// subjects/async-subject.js

let asyncSubject = new Rx.AsyncSubject();
asyncSubject.next(1);
asyncSubject.next(2);
asyncSubject.next(3);
asyncSubject.next(4);

asyncSubject.subscribe(data => console.log(data), err => console.error(err));

Earlier, we had fours values being emitted, but nothing seems to reach the subscriber. At this point, we don't know whether this is because it just acts like a subject and throws away all emitted values that happen before a subscription or not. Let's therefore call the complete() method and see how that plays out:

// subjects/async-subject-complete.js

let asyncSubject = new Rx.AsyncSubject();
asyncSubject.next(1);
asyncSubject.next(2);
asyncSubject.next(3);
asyncSubject.next(4);

// emits 4
asyncSubject.subscribe(data => console.log(data), err => console.error(err));
asyncSubject.complete();

This will emit a 4 due to the fact that AsyncSubject only remembers the last value and we are calling the complete() method, thereby signaling the completion of the stream.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.111.118