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

combineLatest doesn't not work correctly with zero input streams #5039

Open
trotyl opened this issue Sep 25, 2019 · 4 comments
Open

combineLatest doesn't not work correctly with zero input streams #5039

trotyl opened this issue Sep 25, 2019 · 4 comments

Comments

@trotyl
Copy link
Contributor

trotyl commented Sep 25, 2019

Bug Report

Current Behavior
A clear and concise description of the behavior.

combineLatest takes an array of input streams, namely, zero or more input streams. But in the case of zero input streams, it hangs infinitely instead of emit an empty Array/Tuple ([]).

Reproduction

  • REPL or Repo link:

https://stackblitz.com/edit/rxjs-a961ph?devtoolsheight=60

// Promise

Promise.all([Promise.resolve(1), Promise.resolve(2)]) // => [1, 2]
Promise.all([Promise.resolve(1)]) // => [1]
Promise.all([]) // => []

// RxJS

combineLatest([of(1), of(2)])  // => [1, 2]
combineLatest([of(1)]) // => [1]
combineLatest([]) // Never produce a value

Expected behavior

combineLatest([]) // => []

As per the doc:

Combines multiple Observables to create an Observable whose values are calculated from the latest values of each of its input Observables.

RxJS should follow the conventional definition of each/every in algorithms, for a zero length sequence, its .every(criteria) is always true.

Environment

  • Runtime: [e.g. Node vUnrelavant, Chrome vUnrelavant]
  • RxJS version: 6.5.2
  • (If bug is related) Loader, build configuration: [e.g webpack, angular-cli version, config]

Possible Solution

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

Relates to #3189

@cartant
Copy link
Collaborator

cartant commented Sep 25, 2019

[] is the input stream though. And it is empty.

Arrays are valid ObservableInput values and combineLatest accepts rest parameters: combineLatest(of(1), of(2)).

I'm not saying that this isn't confusing and cannot be improved, though. There is special-case shenanigans going on to figure out if it was passed an array of streams or multiple arguments - where each argument is a stream.

if (typeof observables[observables.length - 1] === 'function') {
resultSelector = observables.pop() as (...values: Array<any>) => R;
}
// if the first and only other argument besides the resultSelector is an array
// assume it's been called with `combineLatest([obs1, obs2, obs3], resultSelector)`
if (observables.length === 1 && isArray(observables[0])) {
observables = observables[0] as any;
}

@trotyl
Copy link
Contributor Author

trotyl commented Sep 25, 2019

[] is the input stream though. And it is empty.

@cartant Changing to combineLatest() would have the same result:

https://stackblitz.com/edit/rxjs-qu7wtj?file=index.ts

Is there any reason for "no arguments at all" being considered an input stream?

@cartant
Copy link
Collaborator

cartant commented Sep 25, 2019

Yeah. I'm not saying it's not inconsistent. The inconsistency is one reason I wrote this. It's just not the sort of 'bug' that can be fixed without being deemed a breaking change. IMO any changes of this nature are going to have to wait for v8.

@dobesv
Copy link

dobesv commented Nov 10, 2020

I guess the workaround here is to use rxjs-etc combineLatestArray if you want to pass an array to combineLatest that could be empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants