在整个长管道中传递/保持值的理智方法 [英] sane way to pass/keep a value throughout a long pipe
本文介绍了在整个长管道中传递/保持值的理智方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下 rxjs 管道:
Assuming i have the following rxjs pipe:
start$.pip(
map((id)=> {}), //I want to save the "id" value to be used in the end of the pipe
map(...),
switchMap(...),
map(...),
switchMap(...),
map(...),
switchMap(...),
switchMap(...)//I need the original "id" value here
).subscribe()
有没有办法在整个 pip 中保留id"值,以便它可以在管道的末端使用?
Is there a way to keep the 'id" value throughout the pip so it can be used at the end of the pipe?
动机:经常出现在 NGRX 效果中,我想使用触发源操作的原始负载数据来生成新操作.
Motivation: It comes often in NGRX effects where i want to use the original payload data of the triggering source action for generating the new action.
推荐答案
我认为正确的做法是再做一次闭包
I think the correct way is to make another closure
const processId = (id) => observableOf(id)
.pipe(
map(() => { ... }),
map(...),
switchMap(...),
map(...),
switchMap(...),
map(...),
switchMap(...),
switchMap(...) // Use id here
);
const getPipe = () => start$
.pipe(switchMap(processId));
在 getPipe
中存储局部变量作为副作用是可以的,但如果 start$
Observable 发出更多值,它可能会中断.
Storing local variable as a side effect in getPipe
is OK, but it can break if start$
Observable emits more values.
这篇关于在整个长管道中传递/保持值的理智方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文