[英]Unsubscribe in a condition does not allow to subscribe in Angular RXJS
Im having a service that show wornings,info, and error notifications.我有一项服务,可以显示磨损、信息和错误通知。 Sometimes the notifications come very often.
有时通知来得非常频繁。 Lets say 20-30 in same time and im trying to avoid this thing by unsubscribing when a specifiv time interval happens.
让我们同时说 20-30,我试图通过在特定时间间隔发生时取消订阅来避免这件事。
After unsubscribe than its not anymore possibile to subscribe.取消订阅后,它不再可能订阅。
const locationsSubscription = this.messageService .getMessage() .pipe(timeInterval(), tap(console.log)) .subscribe(message => { if (message.interval < 500) { locationsSubscription.unsubscribe(); } else { switch (message.type){ case MessageType.Info: this.showInfo(message.text); break; case MessageType.Warning: this.showWarning(message..text); break; case MessageType.Error: this.showError(message.value); break; } } });
Just use debounceTime
or throttleTime
to trigger warnings after some time is passed:只需使用
debounceTime
或throttleTime
在经过一段时间后触发警告:
this.messageService
.getMessage()
.pipe(
debounceTime(500) // to allow messages to trigger after 500ms of inactivity (idle)
// throttleTime(500) // to allow only one message per 500ms (first or last)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.