【问题标题】:Angular app is blocked with too many notification eventsAngular 应用程序因通知事件过多而被阻止
【发布时间】:2019-04-18 21:17:00
【问题描述】:

我在后端使用 Angular 7 和 SignalR 进行推送通知。在某个时刻,我收到大量通知,我的应用程序被完全阻止。

signalR 服务组件:

stateChangeNotifier = new Subject<string>();

hubConnection.on("StateChanged", (state: string) => {
  this.stateChangeNotifier.next(state);  // this fire enormous number of notification which block application
});

另一个订阅事件的组件:

stateChangeNotifier.subscribe(result => {
     // here is enormous number of notification
});

有什么方法可以让我在单独的线程中处理这些通知,或者以某种方式收集它们并每两秒只处理最后一个?

【问题讨论】:

  • 请分享您的代码
  • 读入 rxjs,我想你正在寻找 bufferTimedebounceTime 取决于你的需要。但是如果没有您身边的任何实际代码,就很难分辨
  • 这里是代码示例。

标签: javascript angular signalr


【解决方案1】:

根据我的评论,如果您只想在一段时间没有事件后发出,您应该使用debounceTime 运算符

private stateChange = new Subject<string>();

stateChangeNotifier = stateChange.asObservable().pipe(
  debounceTime(500)
);


hubConnection.on("StateChanged", (state: string) => {
  this.stateChange .next(state); 
});

【讨论】:

  • 感谢这是正确的解决方案,但无论如何我的应用程序仍然被后端发送到客户端服务的大量事件所阻塞。只有单独的线程可以解决这个问题。或者减少后端的通知数量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 2011-10-24
  • 1970-01-01
相关资源
最近更新 更多