最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Purpose of "completing" an rxjs Subject? - Stack Overflow

programmeradmin7浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 14

plete 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.

发布评论

评论列表(0)

  1. 暂无评论