Search code examples
rxjs

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?


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 :)