gpt4 book ai didi

angular - 仅当 Rxjs 轮询上的某些属性发生更改时才调度 NgRx 操作

转载 作者:行者123 更新时间:2023-12-03 16:57:42 25 4
gpt4 key购买 nike

我有一个像这样的 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}),
])
)
)
);

我如何发货 updateFooupdateBar仅当 data.foodata.bar分别改变?
我可以通过使用 distinctUntilChanged 来改进这一点,如果 data.stuff,则操作不会触发然而,当任何一个 Action 发生变化时,这两个 Action 仍然会调度。
...
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}),
])
我要发货 updateFoodata.foo更改和 updateBardata.bar变化,知道 data有许多其他属性会随着时间的推移而改变。

最佳答案

我认为这可能是一种方法:

updateProductData$ = createEffect(() =>
this.actions$.pipe(
ofType(fromActions.loadProduct),
switchMap(_) =>
this.http.get('endpoint').pipe(
delay(1000),
repeat(),

multicast(
new Subject(),
source => merge(
// concerning `foo`
source.pipe(
distinctUntilChanged((prev, crt) => prev.foo === crt.foo),
map(data => fromActions.updateFoo({foo: data.foo})),
),

// concerning `bar`
source.pipe(
distinctUntilChanged((prev, crt) => prev.bar === crt.bar),
map(data => fromActions.updateBar({bar: data.bar})),
),
)
)
)
)
);
source来自 multicast的第二个参数是已声明为第一个参数的 Subject 的实例。通过使用 multicast ,我们可以将问题分成 2 个其他较小的问题,而无需冗余订阅源(这就是使用 Subject 的原因)。

关于angular - 仅当 Rxjs 轮询上的某些属性发生更改时才调度 NgRx 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66330734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com