Back to Rxjs

ResultSelector Parameter

apps/rxjs.dev/content/deprecations/resultSelector.md

7.8.21.4 KB
Original Source

ResultSelector Parameter

Some operator supported a resultSelector argument that acted as mapping function on the result of that operator. The same behavior can be reproduced with the map operator, therefore this argument became deprecated.

<div class="alert is-important"> <span> This deprecation was introduced in RxJS 6.0 and will become breaking with RxJS 8. </span> </div>

There were two reasons for actually deprecating those parameters:

  1. It increases the bundle size of every operator
  2. In some scenarios values had to be retained in memory causing a general memory pressure

Operators affected by this Change

How to Refactor

Instead of using the resultSelector Argument, you can leverage the map operator on the inner Observable:

<!-- prettier-ignore -->
ts
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)))
);