What is the purpose of calling plete() on rxjs Subject?
As an example: Calling plete on takeUntil() notifier Observable. Why do we need to do that, and not just call next() and be done with it?
P.S. If it's just a convention, why is it so?
What is the purpose of calling plete() on rxjs Subject?
As an example: Calling plete on takeUntil() notifier Observable. Why do we need to do that, and not just call next() and be done with it?
P.S. If it's just a convention, why is it so?
Share Improve this question edited Jan 28, 2020 at 10:41 Jota.Toledo 28.4k11 gold badges64 silver badges75 bronze badges asked Sep 25, 2018 at 18:00 Viacheslav GuselnykovViacheslav Guselnykov 1411 gold badge1 silver badge4 bronze badges1 Answer
Reset to default 14plete
is normally called on subjects in order to send the pleted
event through the stream. This is done in order to trigger observers that wait for that notification. For example:
var subject = new BehaviorSubject<int>(2);
var subjectStream$ = subject.asObservable();
var finalize$ = subjectStream$.pipe(finalize(()=> console.log("Stream pleted")));
var fork$ = forkJoin(subjectStream$,of(1));
....
finalize$.subscribe(value => console.log({value}));
//output: 2, notice that "Stream pleted" is not logged.
fork$.subcribe(values => console.log({values});
// no output, as one of the inner forked streams never pletes
Furthermore, is a security measure in order avoid mem. leaks, as calling plete on the source stream will remove the references to all the subscribed observers, allowing the garbage collector to eventually dispose any non unsubscribed Subscription
instance.