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

RxJS: exhaustMap in V8

According to the document the exhaustMap uses an ResultSelector and this will be removed in V8. I tried to follow the example but couldn’t make a clue how to refactor my code.

interval(5000)
  .pipe(
    takeWhile(() => !this.isDialogDestroyed),
    exhaustMap(() => this.versService.checkStatus(myParameters))
  )
  .subscribe({
    next: (status) => {
      if (status === 'DONE') {
        this.isDialogDestroyed = true;
        // ...  further processing - removed for simplicity 
      }
    },
    error: () => {
      // Handle errors here
    },
  });

How to adapt the code to be ready for V8?

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 :

Because you’re not using the result selector yourself (second argument of exhaustMap) there’s nothing you’ll have to do in regards this for this code to be compatible in V8.

In your example you can see the suggestion they make is to replace the second parameter from switchMap into piping a map on the result:

import { fromEvent, switchMap, interval, map } from 'rxjs';

// deprecated
fromEvent(document, 'click').pipe(
  switchMap((x) => interval(1000), (_, x) => x + 1)
);
// suggested change
fromEvent(document, 'click').pipe(
  switchMap((x) => interval(1000).pipe(map((x) => x + 1)))
);

If we check the docs on exhaustMap operator:

function exhaustMap<T, R, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, ObservedValueOf<O> | R>

Second argument resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R will be removed, therefore don’t use it.

Since the snippet you’ve pasted doesn’t make use of it there’s no action for you to take 🙂

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