Skip to content

Commit

Permalink
fix(combineLatest): Ensure EMPTY is returned if no observables are …
Browse files Browse the repository at this point in the history
…passed.

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
  • Loading branch information
benlesh committed Jan 18, 2021
1 parent 82e406a commit bcf8f8d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
44 changes: 44 additions & 0 deletions spec/observables/combineLatest-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,50 @@ describe('static combineLatest', () => {
rxTestScheduler = new TestScheduler(observableMatcher);
});

it('should return EMPTY if passed an empty array as the only argument', () => {
const results: string[] = [];
combineLatest([]).subscribe({
next: () => {
throw new Error('should not emit')
},
complete: () => {
results.push('done');
}
});

expect(results).to.deep.equal(['done']);
});

it('should return EMPTY if passed an empty POJO as the only argument', () => {
const results: string[] = [];
combineLatest({}).subscribe({
next: () => {
throw new Error('should not emit')
},
complete: () => {
results.push('done');
}
});

expect(results).to.deep.equal(['done']);
});

it('should return EMPTY if passed an empty array and scheduler as the only argument', () => {
const results: string[] = [];
combineLatest([], rxTestScheduler).subscribe({
next: () => {
throw new Error('should not emit')
},
complete: () => {
results.push('done');
}
});

expect(results).to.deep.equal([]);
rxTestScheduler.flush();
expect(results).to.deep.equal(['done']);
});

it('should combineLatest the provided observables', () => {
rxTestScheduler.run(({ hot, expectObservable }) => {
const firstSource = hot(' ----a----b----c----|');
Expand Down
7 changes: 7 additions & 0 deletions src/internal/observable/combineLatest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,13 @@ export function combineLatest<O extends ObservableInput<any>, R>(...args: any[])

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

if (observables.length === 0) {
// If no observables are passed, or someone has passed an ampty array
// of observables, or even an empty object POJO, we need to just
// complete (EMPTY), but we have to honor the scheduler provided if any.
return from([], scheduler as any);
}

const result = new Observable<ObservedValueOf<O>[]>(
combineLatestInit(
observables as ObservableInput<ObservedValueOf<O>>[],
Expand Down

0 comments on commit bcf8f8d

Please sign in to comment.