【发布时间】:2021-02-23 09:47:44
【问题描述】:
我有一个像这样的 Rxjs 轮询效果:
updateProductData$ = createEffect(() =>
this.actions$.pipe(
ofType(fromActions.loadProduct),
switchMap(_) =>
this.http.get('endpoint').pipe(
delay(1000),
repeat(),
switchMap((data) => [
fromActions.updateFoo({foo: data.foo}),
fromActions.updateBar({bar: data.bar}),
])
)
)
);
仅当data.foo 或data.bar 分别更改时,我如何才能调度updateFoo 和updateBar?
我可以通过使用distinctUntilChanged 来改进这一点,这样如果data.stuff 发生更改,这些操作将不会触发,但是,当任一操作发生更改时,这两个操作仍会分派。
...
repeat(),
distinctUntileChanged((prev, curr) => prev.foo === curr.foo && prev.bar === curr.bar) // works but it fires both actions when either one changes
switchMap((data) => [
fromActions.updateFoo({foo: data.foo}),
fromActions.updateBar({bar: data.bar}),
])
我想在data.foo 更改时调度updateFoo,在data.bar 更改时调度updateBar,因为我知道data 有许多其他属性会随着时间的推移而更改。
【问题讨论】: