Early completions of `switchMap()`
See original GitHub issueWe 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
- Are we abusing observable completion?
- Is this behavior generally more desirable than the current behavior or are we just in a bubble?
- Is this behavior desirable often enough that it should be easier to do (correctly) with Rx?
or maybe we’re just missing something obvious?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:6 (3 by maintainers)
use
.takeLast(1).defaultIfEmpty(0)
instead.@matthewwithanm does this work?