每 x 秒调用 api 不适用于 Ngrx + Angular

dla*_*lam 0 timer polling rxjs ngrx angular

例如,我想每 10 秒更新一次列表。我使用了运算符“timer”,但 api 没有在 Chrome -> Network 选项卡上第二次显示。

我的流程:调度加载操作 - 我的 api - 方法 GET - 仅在“网络”选项卡中调用一次。但是,如果我创建了一个带有函数调度该操作的按钮,则每次我单击时,我的 api 都会在“网络”选项卡/Chrome 中被调用多次

我的环境:ngrx 7.4、rxjs 6.5.2、Angular 7 所以我不知道服务器自动阻止重复调用 api 或 Angular 和 ngrx 是否正在使用缓存

// 我的效果

  load$: Observable<Action> = this.action$.pipe(
    ofType<Load>(NotificationActionTypes.load),
    switchMap(() => timer(0, 10000)),
    switchMap((action) => {
      return this.notiService.getNotifications().pipe(map((res: any) => {
        return new LoadSuccess({
          result: res,
        });
      }));
    })
  );```

[![enter image description here][1]][1]
See my image, 20/125 request, 125 continue increasing, 20/130, 20/145, but number 20 didn't increase


  [1]: https://i.stack.imgur.com/tKgbE.png
Run Code Online (Sandbox Code Playgroud)

tim*_*ver 5

如果您想每 x 秒重新加载一次,则不必分派操作,只需使用流即可timer:

@Effect()
ping = interval(10000).pipe(
  switchMap((action) => {
    return this.notiService.getNotifications().pipe(map((res: any) => {
      return new LoadSuccess({
        result: res,
      });
    }));
}));
Run Code Online (Sandbox Code Playgroud)

更多信息请参见开始使用 ngrx/effects