遵循此Stack Overflow帖子中描述的最佳实践: Angular RxJS何时应取消订阅订阅 ,请考虑此Angular组件生命周期钩子:
private ngUnsubscribe: Subject<void> = new Subject(); ngOnInit() { this.fireAuth.authState .takeUntil(this.ngUnsubscribe) .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .takeUntil(this.ngUnsubscribe) .subscribe((todos) => this.userTodoArray = todos); } ngOnDestroy() { this.ngUnsubscribe.next(); this.ngUnsubscribe.complete(); }由于authState()和mergeMap()都返回Observables,我想我应该也是
取消订阅它们(如上所示)或 只调用其中一个takeUntil()运算符,来自外部Observable authState()或内部Observable this.todoService.getUserTodos(user.uid) 。哪个方法是正确的,以确保在组件被销毁后取消订阅所有Observable?
Following the best practices as described in this Stack Overflow post: Angular RxJS When Should I Unsubscribe From Subscription, consider this Angular component lifecycle hook:
private ngUnsubscribe: Subject<void> = new Subject(); ngOnInit() { this.fireAuth.authState .takeUntil(this.ngUnsubscribe) .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .takeUntil(this.ngUnsubscribe) .subscribe((todos) => this.userTodoArray = todos); } ngOnDestroy() { this.ngUnsubscribe.next(); this.ngUnsubscribe.complete(); }As authState() and mergeMap() both return Observables, I think I'm supposed to either
Unsubscribe from both of them (as shown above) or Only call one of the takeUntil() operators, either from the outer Observable authState() or the inner Observable this.todoService.getUserTodos(user.uid).Which method is correct to make sure all Observables are unsubscribed after the component is destroyed?
最满意答案
您不需要两个takeUntil运算符。 您只需要一个,但行为将根据您删除的行为而有所不同。
如果你只使用第一个takeUntil ,像这样:
this.fireAuth.authState .takeUntil(this.ngUnsubscribe) .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .subscribe((todos) => this.userTodoArray = todos);当this.ngUnsubscribe发出next通知时, takeUntil将取消订阅this.fireAuth.authState源并完成。 如可观察合同中所述,当观察完成后,其订户将收到complete通知并自动取消订阅。
但是,如果从this.fireAuth.authState发出next通知,并且mergeMap getUserTodos返回的mergeMap尚未完成,则mergeMap实现将不会取消订阅getUserTodos observable。 如果getUserTodos observable稍后发出next通知,则通知将流向subscribe 。
如果你只使用第二个takeUntil ,就像这样:
this.fireAuth.authState .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .takeUntil(this.ngUnsubscribe) .subscribe((todos) => this.userTodoArray = todos);当this.ngUnsubscribe发出next通知时, takeUntil将取消订阅mergeMap ,它将取消订阅this.fireAuth.authState以及任何返回的getUserTodos observable。
因此,在此this.ngUnsubscribe发出后,没有通知将流向subscribe 。 这可能是您正在寻找的行为,因此您应该删除第一个takeUntil运算符。
You don't need both of the takeUntil operators. You only need one, but the behaviour will differ depending upon which one you remove.
If you use only the first takeUntil, like this:
this.fireAuth.authState .takeUntil(this.ngUnsubscribe) .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .subscribe((todos) => this.userTodoArray = todos);When this.ngUnsubscribe emits a next notification, the takeUntil will unsubscribe from the this.fireAuth.authState source and will complete. As outlined in the Observable Contract, when an observable completes, its subscribers receive a complete notification and are automatically unsubscribed.
However, if a next notification was emitted from this.fireAuth.authState and the observable returned by getUserTodos in the mergeMap has not yet completed, the mergeMap implementation will not unsubscribe from the getUserTodos observable. And if the getUserTodos observable later emits a next notification, the notification will flow through to the subscribe.
If you use only the second takeUntil, like this:
this.fireAuth.authState .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .takeUntil(this.ngUnsubscribe) .subscribe((todos) => this.userTodoArray = todos);When this.ngUnsubscribe emits a next notification, the takeUntil will unsubscribe from the mergeMap, which will unsubscribe from this.fireAuth.authState and from any returned getUserTodos observable.
So, after this.ngUnsubscribe emits, no notifications will flow through to the subscribe. It's likely that this is the behaviour you are looking for, so you should remove the first takeUntil operator.
Angular RxJS - 取消订阅mergeMap?(Angular RxJS - Unsubscribe from mergeMap?)遵循此Stack Overflow帖子中描述的最佳实践: Angular RxJS何时应取消订阅订阅 ,请考虑此Angular组件生命周期钩子:
private ngUnsubscribe: Subject<void> = new Subject(); ngOnInit() { this.fireAuth.authState .takeUntil(this.ngUnsubscribe) .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .takeUntil(this.ngUnsubscribe) .subscribe((todos) => this.userTodoArray = todos); } ngOnDestroy() { this.ngUnsubscribe.next(); this.ngUnsubscribe.complete(); }由于authState()和mergeMap()都返回Observables,我想我应该也是
取消订阅它们(如上所示)或 只调用其中一个takeUntil()运算符,来自外部Observable authState()或内部Observable this.todoService.getUserTodos(user.uid) 。哪个方法是正确的,以确保在组件被销毁后取消订阅所有Observable?
Following the best practices as described in this Stack Overflow post: Angular RxJS When Should I Unsubscribe From Subscription, consider this Angular component lifecycle hook:
private ngUnsubscribe: Subject<void> = new Subject(); ngOnInit() { this.fireAuth.authState .takeUntil(this.ngUnsubscribe) .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .takeUntil(this.ngUnsubscribe) .subscribe((todos) => this.userTodoArray = todos); } ngOnDestroy() { this.ngUnsubscribe.next(); this.ngUnsubscribe.complete(); }As authState() and mergeMap() both return Observables, I think I'm supposed to either
Unsubscribe from both of them (as shown above) or Only call one of the takeUntil() operators, either from the outer Observable authState() or the inner Observable this.todoService.getUserTodos(user.uid).Which method is correct to make sure all Observables are unsubscribed after the component is destroyed?
最满意答案
您不需要两个takeUntil运算符。 您只需要一个,但行为将根据您删除的行为而有所不同。
如果你只使用第一个takeUntil ,像这样:
this.fireAuth.authState .takeUntil(this.ngUnsubscribe) .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .subscribe((todos) => this.userTodoArray = todos);当this.ngUnsubscribe发出next通知时, takeUntil将取消订阅this.fireAuth.authState源并完成。 如可观察合同中所述,当观察完成后,其订户将收到complete通知并自动取消订阅。
但是,如果从this.fireAuth.authState发出next通知,并且mergeMap getUserTodos返回的mergeMap尚未完成,则mergeMap实现将不会取消订阅getUserTodos observable。 如果getUserTodos observable稍后发出next通知,则通知将流向subscribe 。
如果你只使用第二个takeUntil ,就像这样:
this.fireAuth.authState .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .takeUntil(this.ngUnsubscribe) .subscribe((todos) => this.userTodoArray = todos);当this.ngUnsubscribe发出next通知时, takeUntil将取消订阅mergeMap ,它将取消订阅this.fireAuth.authState以及任何返回的getUserTodos observable。
因此,在此this.ngUnsubscribe发出后,没有通知将流向subscribe 。 这可能是您正在寻找的行为,因此您应该删除第一个takeUntil运算符。
You don't need both of the takeUntil operators. You only need one, but the behaviour will differ depending upon which one you remove.
If you use only the first takeUntil, like this:
this.fireAuth.authState .takeUntil(this.ngUnsubscribe) .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .subscribe((todos) => this.userTodoArray = todos);When this.ngUnsubscribe emits a next notification, the takeUntil will unsubscribe from the this.fireAuth.authState source and will complete. As outlined in the Observable Contract, when an observable completes, its subscribers receive a complete notification and are automatically unsubscribed.
However, if a next notification was emitted from this.fireAuth.authState and the observable returned by getUserTodos in the mergeMap has not yet completed, the mergeMap implementation will not unsubscribe from the getUserTodos observable. And if the getUserTodos observable later emits a next notification, the notification will flow through to the subscribe.
If you use only the second takeUntil, like this:
this.fireAuth.authState .mergeMap((user) => this.todoService.getUserTodos(user.uid)) .takeUntil(this.ngUnsubscribe) .subscribe((todos) => this.userTodoArray = todos);When this.ngUnsubscribe emits a next notification, the takeUntil will unsubscribe from the mergeMap, which will unsubscribe from this.fireAuth.authState and from any returned getUserTodos observable.
So, after this.ngUnsubscribe emits, no notifications will flow through to the subscribe. It's likely that this is the behaviour you are looking for, so you should remove the first takeUntil operator.
发布评论