question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Early completions of `switchMap()`

See original GitHub issue

We use switchMap() a lot, but recently @shushz raised the point that, most of the time, we actually don’t want its completion semantics. Whereas switchMap() completes when both the inner and outer observable do, we often want it to complete as soon as the outer one does (similar to error propagation).

As a use case, consider the old typeahead example where you have an observable of strings (“A”) that get switchMapped to a http request. It seems natural to have A complete when, for example, the input field is removed from the document, but that doesn’t give the most desirable behavior (cancelling the request). The behavior you want would actually be something like:

const smap = (a, proj) => a
  .materialize()
  .switchMap(x => x.kind === 'N'
    ? proj(x.value).materialize().filter(n => n.kind !== 'C')
    : Observable.of(x)
  )
  .dematerialize();

or maybe something with .switchMap(...).takeUntil(...).

So now we’re wondering

  1. Are we abusing observable completion?
  2. Is this behavior generally more desirable than the current behavior or are we just in a bubble?
  3. Is this behavior desirable often enough that it should be easier to do (correctly) with Rx?

or maybe we’re just missing something obvious?

cc @hansonw @trxcllnt Somewhat related: #1815

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
paulpdanielscommented, Jul 5, 2017

use .takeLast(1).defaultIfEmpty(0) instead.

1reaction
trxcllntcommented, Jul 1, 2017

@matthewwithanm does this work?

source.publish((xs) => xs
    .switchMap(project)
    .takeUntil(xs.lastOrDefault(0)))
Read more comments on GitHub >

github_iconTop Results From Across the Web

When using rxjs why doesn't switchMap trigger a complete ...
I soon realised that when using switchMap, when you subscribe to this stream, the completion block does not fire (I don't think error...
Read more >
About switchMap and friends - DEV Community ‍ ‍
This blog article covers the details about the RxJS switchMap operator, but also mergeMap, concatMap and exhaustMap.
Read more >
RxJs Mapping: switchMap vs mergeMap vs concatMap vs ...
Learn in-depth the merge, switch, concat and exhaust strategies and their operators: concatMap, mergeMap, switchMap and exhaustMap.
Read more >
switchMap() | RxJS TUTORIAL - YouTube
Awesome RxJS Operators - this time: switchMap (). What is it and how may we use it?Code (Start): https://jsfiddle.net/1w6k5sdj/Code (End): ...
Read more >
Where to place RxJs operator take(1)? | by Georgi Parlakov
In short, we get multiple emissions in the first case and one emission in the second. So in order for take to complete...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found