ReactiveX / ReactiveX/rxjs

Remove secondary mappings for flattening operators

Open
#2,929 16 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
31.7k
Forks
3k
PR merge metrics
No merged PRs in 30d

Description

For operators like mergeMap, switchMap, concatMap, etc, we have a secondary selector. It's not used a lot in practice that I've seen. Originally, it added a lot of value in terms of perf because it prevented a closure... however, in practice it makes it harder to isolate errors from observation chains, and in a world with Turbofan, I'm not sure the performance benefit is there. We could eliminate a good amount of code in the library written to support this.

Use and Alternatives
Observable.of(1, 2, 3).mergeMap(a => range(0, a), (a, b) => a + b);

/// which is mostly the same as

Observable.of(1, 2, 3).mergeMap(a => range(0, a).map(b => a + b))

// however if there's an error in that second map, you're forced to kill the whole observable...

Observable.of(1, 2, 3)
  .mergeMap(a => range(0, a), (a, b) => { throw new Error('lol') })
  .catch(err => empty())

// rather than this, which is much more common

Observable.of(1, 2, 3)
  .mergeMap(
    a => range(0, a)
      .map(b => a + b)
      .catch(err => empty())
  )


Proposed Change

Eliminate the second argument to mergeMap, concatMap, switchMap, et. al. and instruct people to use .map within the projection function if they want a secondary level of mapping.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the flattening operators named in the issue, including mergeMap, concatMap, and switchMap, and inspect how their secondary selector arguments are exposed and tested. The work is done when those secondary arguments are eliminated consistently and the documented alternative is to use map within the projection function.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.