sane way to pass/keep a value throwout a long pipe


sane way to pass/keep a value throwout a long pipe
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()
Is there a way to keep the 'id" value throughout the pip so it can be used at the end of the pipe?
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.
1 Answer
1
The way suggested by @René Winkler is the right way for short pipes.
If the pipe is long though it can be tedious and somehow can make the code less readable.
One alternative approach can be to create a function (or method) where you define id
as an input variable so that you can use it at your convenience all along the chain of operators, something like
id
getLongPipe(id)
return start$.pipe(
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(...)//Here you can use the orginal id
)
getLongPipe(id).subscribe()
but "id" is an event item of "start$"...
– amit
35 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I think you have to pass the id from one operator to the next one throught the whole chain. You can try something like that map(id => (id: id, data:data))
– René Winkler
54 mins ago