Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is there a way to do one subscription but two callbacks (one debouced?)

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading