Angular -BehaviorSubject next() 调用导致注销问题
我正在开发一个支持多租户的 Angular 项目。当用户切换到另一个租户时,其他组件会收到通知,以便它们可以执行重新加载以显示新租户的数据。这是通过订阅组件内的 BehaviorSubject
来完成的,该组件将触发重新加载,如下所示:
this.tenantService.currentTenant$.subscribe(() => {
// Perform reload
});
这就是 BehaviorSubject
在租户服务中的样子:
currentTenant$ = new BehaviorSubject<Tenant | undefined>(undefined);
get currentTenant(): Tenant | undefined {
return this.currentTenant$.value;
}
set currentTenant(value: Tenant | undefined) {
if (CredentialsService.isEqual(this.currentTenant$.value, value)) {
return;
}
this.currentTenant$.next(value);
}
要更改租户,我只需将 currentTenant
设置为新租户即可。这在 99% 的情况下都可以正常工作,但我只是在用户注销应用程序时发现了一个问题。当用户注销时,会调用以下函数:
logout() {
sessionStorage.clear();
this.currentTenant = undefined;
}
当用户还没有切换到另一个租户时,这里的租户已经是未定义的。因此,不会调用 currentTenant$.next()
。但是,如果用户在切换到另一个租户后注销,this.currentTenant = undefined
将触发 currentTenant$.next()
的调用,这将导致各种不必要的错误其他组件中的行为,因为它们在用户尝试注销时尝试重新加载数据。
有没有办法“安全”地将当前租户设置回 undefined
而不触发 currentTenant$.next()
的调用,或者我是否必须手动检查这在我的代码中(例如,只有在执行注销时才设置为 true 的布尔值)?
I am working on an Angular project with multi-tenant support. When the user switches to another tenant, other components are notified so they can perform a reload to show the data for the new tenant. This is done by subscribing to a BehaviorSubject
inside the components which will trigger the reload like this:
this.tenantService.currentTenant$.subscribe(() => {
// Perform reload
});
This is how the BehaviorSubject
looks like in the tenant service:
currentTenant$ = new BehaviorSubject<Tenant | undefined>(undefined);
get currentTenant(): Tenant | undefined {
return this.currentTenant$.value;
}
set currentTenant(value: Tenant | undefined) {
if (CredentialsService.isEqual(this.currentTenant$.value, value)) {
return;
}
this.currentTenant$.next(value);
}
To change the tenant, I can simply set the currentTenant
to the new tenant. This works just fine in 99% of all cases, but I just found a problem when the user logs out of the application. When the user is logging out, the following function is called:
logout() {
sessionStorage.clear();
this.currentTenant = undefined;
}
When the user has not switched to another tenant, the tenant is already undefined here. Therefore, currentTenant$.next()
is not called. However, if the user logs out after switching to another tenant, this.currentTenant = undefined
will trigger a call of currentTenant$.next()
which will cause all sorts of unwanted behavior in the other components because they try reloading data while the user is trying to log out.
Is there a way how I can "safely" set the current tenant back to undefined
without triggering a call of currentTenant$.next()
, or do I have to manually check this in my code (for example with a boolean that is only set to true while a logout is being performed)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你只是想完成这个主题并创建一个新的主题。
complete()
将取消所有观察者的订阅,并且主题不会接受新的订阅。我假设您的其他组件将订阅 init 上的新主题。I think you just want to complete the subject and create a new one.
complete()
will unsubscribe all observers and the subject will not accept new subscriptions. I assume your other components will subscribe to the new subject on init.