Issue
I have the following code
this.form.valueChanges.pipe(
take(1),
map(val => // doSomething),
exhaustMap(val =>
// someInner observable logic
return of({someValue})
)
).subscribe(finalVal => doSomething());
Now this code in the exhaustMap
is repeated in several components and I'd like to extract it as an external function.
I have tried the following
myExhaust(obs: Observable<any>): Observable<{ someValue: SomeClass }> {
return obs.pipe(
exhaustMap((val) => {
// do some stuff
return of({someValue})
})
);
}
But then I dont know how to plug it in the original code (that if the function code itself is correct)
Solution
You are basically creating a custom operator. You are on the right track. You have to make a function that takes an observable and returns a new one.
function myExhaust<T>(): MonoTypeOperatorFunction<T> {
return input$ => input$.pipe(
exhaustMap((val) => {
// do some stuff
return of({someValue})
}))
}
Now you can use myExhaust instead of exaustMap in your pipe.
Answered By - T. van den Berg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.