Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Early completions of switchMap() #2714

Closed
matthewwithanm opened this issue Jul 1, 2017 · 6 comments
Closed

Early completions of switchMap() #2714

matthewwithanm opened this issue Jul 1, 2017 · 6 comments

Comments

@matthewwithanm
Copy link

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

@trxcllnt
Copy link
Member

trxcllnt commented Jul 1, 2017

@matthewwithanm does this work?

source.publish((xs) => xs
    .switchMap(project)
    .takeUntil(xs.lastOrDefault(0)))

@matthewwithanm
Copy link
Author

Hey @trxcllnt! lastOrDefault() isn't in RxJS anymore, right? But yeah, that's the general idea.

@paulpdaniels
Copy link
Contributor

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

@matthewwithanm
Copy link
Author

Thanks @paulpdaniels! I understood @trxcllnt's response but I was really looking for answers to the questions in the OP—especially 2. I'm assuming your answer to (3) is that combining these five operators (publish(), switchMap(), takeUntil(), takeLast(), and defaultIfEmpty()) is obvious/clear/easy enough?

@benlesh
Copy link
Member

benlesh commented Mar 16, 2018

Closing as stale.

@benlesh benlesh closed this as completed Mar 16, 2018
@lock
Copy link

lock bot commented Jun 5, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants