rxjs - 总是有运营商吗?你怎么总是做点什么?

dan*_*y74 1 rxjs ngrx angular rxjs6

我有一个NGRX效果(大量使用RxJS)如下:

@Effect()
allFiltersRequested$ = this.actions$.pipe(
  ofType<AllFiltersRequestedAction>(FiltersActionTypes.AllFiltersRequested),
  tap((action) => debug(action)),
  withLatestFrom(this.store.pipe(select(selectAllFilters))),
  filter(([action, allFilters]) => isEmpty(allFilters)),
  mergeMap(() =>
    this.simService.getAllFilters().pipe(
      catchError(() => {
        return of({})
      })
    )
  ),
  map((allFilters) => new AllFiltersLoadedAction(allFilters))
)
Run Code Online (Sandbox Code Playgroud)

filter用来阻止发出HTTP请求(请参阅参考资料mergeMap)是否已经填充了商店(因为来自API的数据没有改变)

但是,我总是想执行:

map((allFilters) => new AllFiltersLoadedAction(allFilters))
Run Code Online (Sandbox Code Playgroud)

无论是否发出HTTP请求.然而,它不能在之前执行,mergeMap因为mergeMap确保allFilters数据可用.

有没有RxJS always do this类型的运营商?如果没有,我应该如何重构此代码以实现我想要的?

目前,唯一的解决方案是删除,filter但这将导致不必要的HTTP请求.

谢谢

mar*_*tin 5

只是不要使用filter和包装HTTP调用if条件:

...
withLatestFrom(...)
mergeMap(([action, allFilters]) => {
  if (isEmpty(allFilters)) {
    return of(allFilters);
  }
  return this.simService.getAllFilters()...
})
map((allFilters) => new AllFiltersLoadedAction(allFilters))
Run Code Online (Sandbox Code Playgroud)

我不知道你究竟想做什么,但我希望你能明白这一点.