I'm looking for a way to trace value 'route' among stream operators. I have a materialized stream with additional metadata on Notification object (ex. valueId property). It's definition will look something like this:
const x = stream.pipe(
materialize(),
map(x => Object.assign(x, {valueId: randomInt()}))
);
Now I need to wrap operators that are applied to x. Let's say I need to use map(x => x * 2), but I cannot do it like this:
x.pipe(dematerialize(), map(x => x * 2))
Because I will lose my metadata. How do I make a wrap function, that will apply to any operator and will still preserve my additional metadata?
x.pipe(wrap(map(x => x * 2)))
I thought about something like this:
function wrap<T, R>(
operator: OperatorFunction<T, R>
): (source: Observable<TaggedValue<T>>) => Observable<TaggedValue<R>> {
return source =>
source.pipe(
switchMap(x =>
of(x).pipe(
dematerialize(),
operator,
materialize(),
map(z => Object.assign(z, { valueId: x.valueId }))
)
)
);
}
But it generates fake complete messages from of().
Sample: https://stackblitz.com/edit/rxjs-fhv54p