-
Notifications
You must be signed in to change notification settings - Fork 3k
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([]) behaves like EMPTY #7514
Comments
combineLatest([]).subscribe({
next: (value) => console.log('combineLatest next', value),
complete: () => console.log('combineLatest complete'),
});
EMPTY.subscribe({
next: (value) => console.log('EMPTY next', value),
complete: () => console.log('EMPTY complete'),
});
NEVER.subscribe({
next: (value) => console.log('NEVER next', value),
complete: () => console.log('NEVER complete'),
});
As for me, this behavior is completely logical. Following your logic, what should happen in this case? combineLatest([EMPTY, of(2)]).subscribe((it) => {
console.log('result', it);
}); I can offer this solution: combineLatest([])
.pipe(defaultIfEmpty([]))
.subscribe((values) => {
console.log('result', values);
}); |
Thanks for the feedback; I was actually looking forward to a little discussion to see whether my expectation makes sense or whether it conflicts with other constraints. I have adjusted the title. Regarding for My use case is that I need to "flatten" an Building on your use case, I agree my expectation comes out strange:
It's like limit value consideration from two sides 😄 |
Probably best to leave I guess I can just build my own import { Observable, combineLatest, of, defaultIfEmpty } from 'rxjs';
type UnwrapObservable<T> = T extends Observable<infer U> ? U : never;
type ConvertToObservableTuple<T extends readonly any[]> = Observable<{ [K in keyof T]: UnwrapObservable<T[K]> }>;
const collect
: <T extends readonly any[]>(observables: T) => ConvertToObservableTuple<T>
= <T extends readonly any[]>(observables: T) => combineLatest(observables).pipe(defaultIfEmpty([])) as ConvertToObservableTuple<T>;
const x$: Observable<[number, string]> = collect([ of(1), of('a') ]);
const y$: Observable<[]> = collect([]); |
Describe the bug
combineLatest
will not emit a value before all, and at least one, of its observables have emitted a value, thus:combineLatest([])
=NEVER
EMPTY
-- the at least one seems wrong to me
Expected behavior
combineLatest
will not emit a value before none of its observables has not emitted a value yet, thus:combineLatest([])
=of([])
Workaround: check the array length upfront (typescript):
Reproduction code
Reproduction URL
No response
Version
7.8.1
Environment
No response
Additional context
I use
combineLatest
to map an array of observableBehaviorSubject
s to an observable array. BecauseBehaviorSubject
always has a value to return,combineLatest
will always deliver a value when the array is non-empty. But as I found out, it will never deliver a value when the array is empty, possibly blocking a code that was not expecting this behavior.The text was updated successfully, but these errors were encountered: