Angular应用程序因过多通知事件而被阻止

问题描述 投票:0回答:1

我在后端使用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
});

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

javascript angular signalr
1个回答
0
投票

根据我的评论,如果你想在一段时间没有事件后才发光,你应该使用debounceTime算子

private stateChange = new Subject<string>();

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


hubConnection.on("StateChanged", (state: string) => {
  this.stateChange .next(state); 
});
© www.soinside.com 2019 - 2024. All rights reserved.