I have:
thingie.valueChanges.pipe(
takeUntil(this.destroy$)
).subscribe(() => doSomething());
thingie.valueChanges.pipe(
takeUntil(this.destroy$),
debouce(250),
mergeMap((x) => forMergeMap(x))
).subscribe((x) => doSomethingWithX(x));
Is there any way to combine that into one subscribe?
>Solution :
Yes, you can use the merge function of rxJs.
For example:
const nonDebounced$ = thingie.valueChanges.pipe(
map(() => ({ type: 'nonDebounced' }))
);
const debounced$ = thingie.valueChanges.pipe(
debounceTime(250),
mergeMap((x) => forMergeMap(x)),
map((x) => ({ type: 'debounced', value: x }))
);
const combined$ = merge(nonDebounced$, debounced$).pipe(
takeUntil(this.destroy$)
);
Then you can subscribe to it once and check the type value…