angular - 仅当 Rxjs 轮询上的某些属性更改时才调度 NgRx 操作
问题描述
我有一个像这样的 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}),
])
)
)
);
我怎样才能分别发送updateFoo
和updateBar
仅在何时发送data.foo
或data.bar
更改?
我可以通过使用来改进这一点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}),
])
我想在更改和更改时进行调度,因为updateFoo
我知道它还有很多其他属性会随着时间的推移而改变。data.foo
updateBar
data.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
from的multicast
第二个参数是已被声明为第一个参数的 Subject 的实例。通过使用multicast
,我们可以将问题分成 2 个其他较小的问题,而无需冗余订阅源(这就是使用主题的原因)。
推荐阅读
- r - 试图在 ggplot 中创建条形图
- r - 为什么我不能从 uci 导入以下数据集
- c++ - C++中并行openmp多循环的问题
- docker - ffmpeg在编解码时会分配内存,不会立即释放吗?
- python - 在字符串python中定位起点和终点
- webpack - Webpack - 旧库的自动解析
- java - JavaFX 双向绑定文本到 Doubleproperty 负数
- powerbi - “创建模板组织内容包和应用程序”的用例是什么?
- docker - Portainer Docker Image - 访问我的家庭网络
- c# - 尝试在另一种方法中提及事件处理程序时出现难以理解的错误