简体   繁体   English

“完成”一个 rxjs 主题的目的?

[英]Purpose of "completing" an rxjs Subject?

What is the purpose of calling complete() on rxjs Subject?在 rxjs Subject 上调用 complete() 的目的是什么?

As an example: Calling complete on takeUntil() notifier Observable.例如:在 takeUntil() 通知程序 Observable 上调用完成。 Why do we need to do that, and not just call next() and be done with it?为什么我们需要这样做,而不是调用 next() 就可以了?

PS If it's just a convention, why is it so? PS如果这只是一个约定,为什么会这样?

complete is normally called on subjects in order to send the completed event through the stream. complete通常在主题上调用,以便通过流发送已completed事件。 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 completed")));
var fork$ = forkJoin(subjectStream$,of(1));

....

finalize$.subscribe(value => console.log({value})); 
//output: 2, notice that "Stream completed" is not logged.
fork$.subcribe(values => console.log({values}); 
// no output, as one of the inner forked streams never completes

Furthermore, is a security measure in order avoid mem.此外,是一种安全措施,以避免内存。 leaks, as calling complete 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.泄漏,因为在源流上调用 complete 将删除对所有订阅观察者的引用,从而允许垃圾收集器最终处理任何未订阅的Subscription实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM