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

Modified behavior on empty inputs for forkJoin, combineLatest and zip #5922

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions api_guard/dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ export declare function bindCallback<A extends readonly unknown[], R extends rea
export declare function bindNodeCallback(callbackFunc: (...args: any[]) => void, resultSelector: (...args: any[]) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>;
export declare function bindNodeCallback<A extends readonly unknown[], R extends readonly unknown[]>(callbackFunc: (...args: [...A, (err: any, ...res: R) => void]) => void, schedulerLike?: SchedulerLike): (...arg: A) => Observable<R extends [] ? void : R extends [any] ? R[0] : R>;

export declare function combineLatest(sources: []): Observable<never>;
export declare function combineLatest<A extends readonly unknown[]>(sources: readonly [...ObservableInputTuple<A>]): Observable<A>;
export declare function combineLatest(): Observable<never>;
export declare function combineLatest<A extends readonly unknown[]>(...sources: [...ObservableInputTuple<A>]): Observable<A>;
export declare function combineLatest(sourcesObject: {
[K in any]: never;
}): Observable<never>;
export declare function combineLatest<T>(sourcesObject: T): Observable<{
[K in keyof T]: ObservedValueOf<T[K]>;
}>;
Expand Down Expand Up @@ -163,14 +160,11 @@ export declare type Falsy = null | undefined | false | 0 | -0 | 0n | '';

export declare function firstValueFrom<T>(source: Observable<T>): Promise<T>;

export declare function forkJoin(sources: readonly []): Observable<never>;
export declare function forkJoin<A extends readonly unknown[]>(sources: readonly [...ObservableInputTuple<A>]): Observable<A>;
export declare function forkJoin<A extends readonly unknown[], R>(sources: readonly [...ObservableInputTuple<A>], resultSelector: (...values: A) => R): Observable<R>;
export declare function forkJoin(): Observable<never>;
export declare function forkJoin<A extends readonly unknown[]>(...sources: [...ObservableInputTuple<A>]): Observable<A>;
export declare function forkJoin<A extends readonly unknown[], R>(...sourcesAndResultSelector: [...ObservableInputTuple<A>, (...values: A) => R]): Observable<R>;
export declare function forkJoin(sourcesObject: {
[K in any]: never;
}): Observable<never>;
export declare function forkJoin<T>(sourcesObject: T): Observable<{
[K in keyof T]: ObservedValueOf<T[K]>;
}>;
Expand Down Expand Up @@ -561,5 +555,9 @@ export declare class VirtualTimeScheduler extends AsyncScheduler {

export declare function zip<A extends readonly unknown[]>(sources: [...ObservableInputTuple<A>]): Observable<A>;
export declare function zip<A extends readonly unknown[], R>(sources: [...ObservableInputTuple<A>], resultSelector: (...values: A) => R): Observable<R>;
export declare function zip(): Observable<never>;
export declare function zip<A extends readonly unknown[]>(...sources: [...ObservableInputTuple<A>]): Observable<A>;
export declare function zip<A extends readonly unknown[], R>(...sourcesAndResultSelector: [...ObservableInputTuple<A>, (...values: A) => R]): Observable<R>;
export declare function zip<T>(sourcesObject: T): Observable<{
[K in keyof T]: ObservedValueOf<T[K]>;
}>;
7 changes: 6 additions & 1 deletion spec-dtslint/observables/combineLatest-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ it('should accept 7 or more params and a result selector', () => {
const o = combineLatest(a$, b$, c$, d$, e$, f$, g$, g$, g$, () => new A()); // $ExpectType Observable<A>
});

it('shoudd accept 0 params', () => {
const o = combineLatest([]); // $ExpectType Observable<[]>
const o1 = combineLatest(); // $ExpectType Observable<never>
});

it('should accept 1 param', () => {
const o = combineLatest([a$]); // $ExpectType Observable<[A]>
});
Expand Down Expand Up @@ -130,7 +135,7 @@ it('should accept 7 or more params and a result selector', () => {

describe('combineLatest({})', () => {
it('should properly type empty objects', () => {
const res = combineLatest({}); // $ExpectType Observable<never>
const res = combineLatest({}); // $ExpectType Observable<{}>
});

it('should work for the simple case', () => {
Expand Down
10 changes: 7 additions & 3 deletions spec-dtslint/observables/forkJoin-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ it('should infer of type any for more than 6 parameters', () => {

describe('forkJoin({})', () => {
it('should properly type empty objects', () => {
const res = forkJoin({}); // $ExpectType Observable<never>
const res = forkJoin({}); // $ExpectType Observable<{}>
});

it('should work for the simple case', () => {
Expand All @@ -75,9 +75,13 @@ describe('forkJoin({})', () => {
});

describe('forkJoin([])', () => {
it('should property type empty parameters', () => {
const res = forkJoin(); // $ExpectType Observable<never>
});

it('should properly type empty arrays', () => {
const res = forkJoin([]); // $ExpectType Observable<never>
const resConst = forkJoin([] as const); // $ExpectType Observable<never>
const res = forkJoin([]); // $ExpectType Observable<[]>
const resConst = forkJoin([] as const); // $ExpectType Observable<[]>
});

it('should properly type readonly arrays', () => {
Expand Down
10 changes: 10 additions & 0 deletions spec-dtslint/observables/zip-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ it('should return Array<T> when given a single promise', () => {
const o1 = zip(a); // $ExpectType Observable<[number]>
});

it('should return an empty array when given no source', () => {
const o = zip([]); // $ExpectType Observable<[]>
const o1 = zip(); // $ExpectType Observable<never>
});

it('should return Array<T> when given a single observable', () => {
const a = of(1); // $ExpectType Observable<number>
const o1 = zip(a); // $ExpectType Observable<[number]>
});

it('should support object types', () => {
const u = Math.random() > 0.5 ? of(123) : of('abc');
const o = zip({ u }); // $ExpectType Observable<{ u: string | number; }>
});

it('should support union types', () => {
const u = Math.random() > 0.5 ? of(123) : of('abc');
const o = zip(u, u, u); // $ExpectType Observable<[string | number, string | number, string | number]>
Expand Down
27 changes: 27 additions & 0 deletions spec/observables/combineLatest-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,33 @@ describe('static combineLatest', () => {
});
});

it('should complete if source is not provided', () => {
rxTestScheduler.run(({ expectObservable }) => {
const e1 = combineLatest();
const expected = '|';

expectObservable(e1).toBe(expected);
});
});

it('should emit an empty array if sources array is empty', () => {
rxTestScheduler.run(({ expectObservable }) => {
const e1 = combineLatest([]);
const expected = '(x|)';

expectObservable(e1).toBe(expected, { x: [] });
});
});

it('should emit an empty object if sources object is empty', () => {
rxTestScheduler.run(({ expectObservable }) => {
const e1 = combineLatest({});
const expected = '(x|)';

expectObservable(e1).toBe(expected, { x: {} });
});
});

it('should work with throw and never', () => {
rxTestScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot('---^----#', undefined, 'wokka wokka');
Expand Down
12 changes: 6 additions & 6 deletions spec/observables/forkJoin-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ describe('forkJoin', () => {
});
});

it('should complete if sources list is empty', () => {
it('should emit an empty array if sources list is empty', () => {
rxTestScheduler.run(({ expectObservable }) => {
const e1 = forkJoin([]);
const expected = '|';
const expected = '(x|)';

expectObservable(e1).toBe(expected);
expectObservable(e1).toBe(expected, { x: [] });
});
});

Expand Down Expand Up @@ -483,12 +483,12 @@ describe('forkJoin', () => {
});
});

it('should complete if sources object is empty', () => {
it('should emit an empty object if sources object is empty', () => {
rxTestScheduler.run(({ expectObservable }) => {
const e1 = forkJoin({});
const expected = '|';
const expected = '(x|)';

expectObservable(e1).toBe(expected);
expectObservable(e1).toBe(expected, { x: {} });
});
});

Expand Down
52 changes: 50 additions & 2 deletions spec/observables/zip-spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
import { expect } from 'chai';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { queueScheduler as rxQueueScheduler, zip, from, of } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';

const queueScheduler = rxQueueScheduler;

/** @test {zip} */
describe('static zip', () => {
it('should combine a source with a second', () => {
let rxTestScheduler: TestScheduler;

beforeEach(() => {
rxTestScheduler = new TestScheduler(observableMatcher);
});

it('should combine a source with a second (array)', () => {
const a = hot('---1---2---3---');
const asubs = '^';
const b = hot('--4--5--6--7--8--');
const bsubs = '^';
const expected = '---x---y---z';

expectObservable(zip(a, b))
expectObservable(zip([a, b]))
.toBe(expected, { x: ['1', '4'], y: ['2', '5'], z: ['3', '6'] });
expectSubscriptions(a.subscriptions).toBe(asubs);
expectSubscriptions(b.subscriptions).toBe(bsubs);
});

it('should combine a source with a second (object)', () => {
const a = hot('---1---2---3---');
const asubs = '^';
const b = hot('--4--5--6--7--8--');
const bsubs = '^';
const expected = '---x---y---z';

expectObservable(zip({ a, b }))
.toBe(expected, { x: { a: '1', b: '4' }, y: { a: '2', b: '5' }, z: { a: '3', b: '6' } });
expectSubscriptions(a.subscriptions).toBe(asubs);
expectSubscriptions(b.subscriptions).toBe(bsubs);
});

it('should zip the provided observables', (done: MochaDone) => {
const expected = ['a1', 'b2', 'c3'];
let i = 0;
Expand Down Expand Up @@ -529,6 +550,33 @@ describe('static zip', () => {
}, null, done);
});

it('should complete if source is not provided', () => {
rxTestScheduler.run(({ expectObservable }) => {
const e1 = zip();
const expected = '|';

expectObservable(e1).toBe(expected);
});
});

it('should emit an empty array if sources array is empty', () => {
rxTestScheduler.run(({ expectObservable }) => {
const e1 = zip([]);
const expected = '(x|)';

expectObservable(e1).toBe(expected, { x: [] });
});
});

it('should emit an empty object if sources object is empty', () => {
rxTestScheduler.run(({ expectObservable }) => {
const e1 = zip({});
const expected = '(x|)';

expectObservable(e1).toBe(expected, { x: {} });
});
});

it('should be able to zip all iterables', () => {
const results: any[] = [];
zip('abc', '123', 'xyz').subscribe({
Expand Down
6 changes: 3 additions & 3 deletions spec/operators/zipAll-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,12 @@ describe('zipAll operator', () => {
});
});

it('should complete when empty source', () => {
it('should emit an empty list when empty source', () => {
rxTestScheduler.run(({ hot, expectObservable }) => {
const source = hot('|');
const expected = ' |';
const expected = '(x|)';

expectObservable(source.pipe(zipAll())).toBe(expected);
expectObservable(source.pipe(zipAll())).toBe(expected, { x: [] });
});
});
});
15 changes: 13 additions & 2 deletions src/internal/observable/combineLatest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import { identity } from '../util/identity';
import { Subscription } from '../Subscription';
import { mapOneOrManyArgs } from '../util/mapOneOrManyArgs';
import { popResultSelector, popScheduler } from '../util/args';
import { EMPTY } from './empty';
import { of } from './of';

// combineLatest([a, b, c])
export function combineLatest(sources: []): Observable<never>;
export function combineLatest<A extends readonly unknown[]>(sources: readonly [...ObservableInputTuple<A>]): Observable<A>;

// combineLatest(a, b, c)
/** @deprecated Use the version that takes an empty array of Observables instead */
export function combineLatest(): Observable<never>;
/** @deprecated Use the version that takes an array of Observables instead */
export function combineLatest<A extends readonly unknown[]>(...sources: [...ObservableInputTuple<A>]): Observable<A>;

// combineLatest({a, b, c})
export function combineLatest(sourcesObject: { [K in any]: never }): Observable<never>;
export function combineLatest<T>(sourcesObject: T): Observable<{ [K in keyof T]: ObservedValueOf<T[K]> }>;

// If called with a single array, it "auto-spreads" the array, with result selector
Expand Down Expand Up @@ -463,6 +465,15 @@ export function combineLatest<O extends ObservableInput<any>, R>(...args: any[])

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

if (args === observables && args.length === 0) {
// deprecated path: empty combineLatest()
return EMPTY;
}

if (observables.length === 0) {
return of(keys ? {} : []) as Observable<R>;
}

const result = new Observable<ObservedValueOf<O>[]>(
combineLatestInit(
observables as ObservableInput<ObservedValueOf<O>>[],
Expand Down
11 changes: 9 additions & 2 deletions src/internal/observable/forkJoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { map } from '../operators/map';
import { argsArgArrayOrObject } from '../util/argsArgArrayOrObject';
import { innerFrom } from './from';
import { popResultSelector } from '../util/args';
import { EMPTY } from './empty';

// forkJoin([a, b, c])
export function forkJoin(sources: readonly []): Observable<never>;
export function forkJoin<A extends readonly unknown[]>(sources: readonly [...ObservableInputTuple<A>]): Observable<A>;
/** @deprecated resultSelector is deprecated, pipe to map instead */
export function forkJoin<A extends readonly unknown[], R>(
Expand All @@ -16,6 +16,8 @@ export function forkJoin<A extends readonly unknown[], R>(
): Observable<R>;

// forkJoin(a, b, c)
/** @deprecated Use the version that takes an empty array of Observables instead */
export function forkJoin(): Observable<never>;
/** @deprecated Use the version that takes an array of Observables instead */
export function forkJoin<A extends readonly unknown[]>(...sources: [...ObservableInputTuple<A>]): Observable<A>;
/** @deprecated resultSelector is deprecated, pipe to map instead */
Expand All @@ -24,7 +26,6 @@ export function forkJoin<A extends readonly unknown[], R>(
): Observable<R>;

// forkJoin({a, b, c})
export function forkJoin(sourcesObject: { [K in any]: never }): Observable<never>;
export function forkJoin<T>(sourcesObject: T): Observable<{ [K in keyof T]: ObservedValueOf<T[K]> }>;

/**
Expand Down Expand Up @@ -128,6 +129,11 @@ export function forkJoin(...args: any[]): Observable<any> {

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

if (args === sources && args.length === 0) {
// deprecated path for forkJoin() without any argument
return EMPTY;
}

if (resultSelector) {
// deprecated path.
return forkJoinInternal(sources, keys).pipe(map((values: any[]) => resultSelector!(...values)));
Expand All @@ -140,6 +146,7 @@ function forkJoinInternal(sources: ObservableInput<any>[], keys: string[] | null
return new Observable((subscriber) => {
const len = sources.length;
if (len === 0) {
subscriber.next(keys ? {} : []);
subscriber.complete();
return;
}
Expand Down
Loading