Skip to content

Commit 157c7e8

Browse files
authored
fix(combineLatest): Ensure EMPTY is returned if no observables are passed. (#5963)
This resolves an issue found by testing Angular router. There was code that expected this behavior (rightfully so), and it was broken during a refactor in v7. Resolves #5962
1 parent 82e406a commit 157c7e8

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

spec/observables/combineLatest-spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,50 @@ describe('static combineLatest', () => {
1414
rxTestScheduler = new TestScheduler(observableMatcher);
1515
});
1616

17+
it('should return EMPTY if passed an empty array as the only argument', () => {
18+
const results: string[] = [];
19+
combineLatest([]).subscribe({
20+
next: () => {
21+
throw new Error('should not emit')
22+
},
23+
complete: () => {
24+
results.push('done');
25+
}
26+
});
27+
28+
expect(results).to.deep.equal(['done']);
29+
});
30+
31+
it('should return EMPTY if passed an empty POJO as the only argument', () => {
32+
const results: string[] = [];
33+
combineLatest({}).subscribe({
34+
next: () => {
35+
throw new Error('should not emit')
36+
},
37+
complete: () => {
38+
results.push('done');
39+
}
40+
});
41+
42+
expect(results).to.deep.equal(['done']);
43+
});
44+
45+
it('should return EMPTY if passed an empty array and scheduler as the only argument', () => {
46+
const results: string[] = [];
47+
combineLatest([], rxTestScheduler).subscribe({
48+
next: () => {
49+
throw new Error('should not emit')
50+
},
51+
complete: () => {
52+
results.push('done');
53+
}
54+
});
55+
56+
expect(results).to.deep.equal([]);
57+
rxTestScheduler.flush();
58+
expect(results).to.deep.equal(['done']);
59+
});
60+
1761
it('should combineLatest the provided observables', () => {
1862
rxTestScheduler.run(({ hot, expectObservable }) => {
1963
const firstSource = hot(' ----a----b----c----|');

src/internal/observable/combineLatest.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ export function combineLatest<O extends ObservableInput<any>, R>(...args: any[])
463463

464464
const { args: observables, keys } = argsArgArrayOrObject(args);
465465

466+
if (observables.length === 0) {
467+
// If no observables are passed, or someone has passed an ampty array
468+
// of observables, or even an empty object POJO, we need to just
469+
// complete (EMPTY), but we have to honor the scheduler provided if any.
470+
return from([], scheduler as any);
471+
}
472+
466473
const result = new Observable<ObservedValueOf<O>[]>(
467474
combineLatestInit(
468475
observables as ObservableInput<ObservedValueOf<O>>[],

0 commit comments

Comments
 (0)