From 4d2338bc48a48d73ae721911a371486a9346251f Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Wed, 7 Feb 2018 14:41:57 -0800 Subject: [PATCH] feat(forkJoin): simplify interface - removes resultSelector from forkJoin - updates tests --- spec/observables/forkJoin-spec.ts | 133 ---------------------------- src/internal/observable/forkJoin.ts | 45 ++-------- 2 files changed, 9 insertions(+), 169 deletions(-) diff --git a/spec/observables/forkJoin-spec.ts b/spec/observables/forkJoin-spec.ts index 98f4c1f531..7e041c6c27 100644 --- a/spec/observables/forkJoin-spec.ts +++ b/spec/observables/forkJoin-spec.ts @@ -42,22 +42,6 @@ describe('forkJoin', () => { expectObservable(e2).toBe(expected2, {x: [null, 'b', '3', undefined]}); }); - it('should join the last values of the provided observables with selector', () => { - function selector(x: string, y: string, z: string) { - return x + y + z; - } - - const e1 = forkJoin( - hot('--a--b--c--d--|'), - hot('(b|)'), - hot('--1--2--3--|'), - selector - ); - const expected = '--------------(x|)'; - - expectObservable(e1).toBe(expected, {x: 'db3'}); - }); - it('should accept single observable', () => { const e1 = forkJoin( hot('--a--b--c--d--|') @@ -76,34 +60,6 @@ describe('forkJoin', () => { expectObservable(e1).toBe(expected, {x: ['d']}); }); - it('should accept single observable with selector', () => { - function selector(x: string) { - return x + x; - } - - const e1 = forkJoin( - hot('--a--b--c--d--|'), - selector - ); - const expected = '--------------(x|)'; - - expectObservable(e1).toBe(expected, {x: 'dd'}); - }); - - it('should accept array of observable contains single with selector', () => { - function selector(x: string) { - return x + x; - } - - const e1 = forkJoin( - [hot('--a--b--c--d--|')], - selector - ); - const expected = '--------------(x|)'; - - expectObservable(e1).toBe(expected, {x: 'dd'}); - }); - it('should accept lowercase-o observables', () => { const e1 = forkJoin( hot('--a--b--c--d--|'), @@ -153,22 +109,6 @@ describe('forkJoin', () => { expectObservable(e1).toBe(expected, {x: ['d', 'b', '3']}); }); - it('should accept array of observables with selector', () => { - function selector(x: string, y: string, z: string) { - return x + y + z; - } - - const e1 = forkJoin( - [hot('--a--b--c--d--|'), - hot('(b|)'), - hot('--1--2--3--|')], - selector - ); - const expected = '--------------(x|)'; - - expectObservable(e1).toBe(expected, {x: 'db3'}); - }); - it('should not emit if any of source observable is empty', () => { const e1 = forkJoin( hot('--a--b--c--d--|'), @@ -244,34 +184,6 @@ describe('forkJoin', () => { expectObservable(e1).toBe(expected); }); - it('should complete when any of source is empty with selector', () => { - function selector(x: string, y: string) { - return x + y; - } - - const e1 = forkJoin( - hot('--a--b--c--d--|'), - hot('---------|'), - selector); - const expected = '---------|'; - - expectObservable(e1).toBe(expected); - }); - - it('should emit results by resultselector', () => { - function selector(x: string, y: string) { - return x + y; - } - - const e1 = forkJoin( - hot('--a--b--c--d--|'), - hot('---2-----|'), - selector); - const expected = '--------------(x|)'; - - expectObservable(e1).toBe(expected, {x: 'd2'}); - }); - it('should raise error when any of source raises error with empty observable', () => { const e1 = forkJoin( hot('------#'), @@ -290,20 +202,6 @@ describe('forkJoin', () => { expectObservable(e1).toBe(expected); }); - it('should raise error when any of source raises error with selector with empty observable', () => { - function selector(x: string, y: string) { - return x + y; - } - - const e1 = forkJoin( - hot('------#'), - hot('---------|'), - selector); - const expected = '------#'; - - expectObservable(e1).toBe(expected); - }); - it('should raise error when source raises error', () => { const e1 = forkJoin( hot('------#'), @@ -313,34 +211,6 @@ describe('forkJoin', () => { expectObservable(e1).toBe(expected); }); - it('should raise error when source raises error with selector', () => { - function selector(x: string, y: string) { - return x + y; - } - - const e1 = forkJoin( - hot('------#'), - hot('-------b-|'), - selector); - const expected = '------#'; - - expectObservable(e1).toBe(expected); - }); - - it('should raise error when the selector throws', () => { - function selector(x: string, y: string) { - throw 'error'; - } - - const e1 = forkJoin( - hot('--a-|'), - hot('---b-|'), - selector); - const expected = '-----#'; - - expectObservable(e1).toBe(expected); - }); - it('should allow unsubscribing early and explicitly', () => { const e1 = hot('--a--^--b--c---d-| '); const e1subs = '^ ! '; @@ -397,7 +267,6 @@ describe('forkJoin', () => { let c: Promise; let d: Observable; let o1: Observable<[number, string, boolean, string[]]> = forkJoin(a, b, c, d); - let o2: Observable = forkJoin(a, b, c, d, (aa, bb, cc, dd) => !!aa && !!bb && cc && !!dd.length); /* tslint:enable:no-unused-variable */ }); @@ -406,7 +275,6 @@ describe('forkJoin', () => { let a: Promise[]; let o1: Observable = forkJoin(a); let o2: Observable = forkJoin(...a); - let o3: Observable = forkJoin(a, (...x) => x.length); /* tslint:enable:no-unused-variable */ }); @@ -415,7 +283,6 @@ describe('forkJoin', () => { let a: Observable[]; let o1: Observable = forkJoin(a); let o2: Observable = forkJoin(...a); - let o3: Observable = forkJoin(a, (...x) => x.length); /* tslint:enable:no-unused-variable */ }); diff --git a/src/internal/observable/forkJoin.ts b/src/internal/observable/forkJoin.ts index e4ff069c52..ea6140713a 100644 --- a/src/internal/observable/forkJoin.ts +++ b/src/internal/observable/forkJoin.ts @@ -13,18 +13,9 @@ export function forkJoin(v1: ObservableInput, v2: ObservableInput< export function forkJoin(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput): Observable<[T, T2, T3, T4]>; export function forkJoin(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput): Observable<[T, T2, T3, T4, T5]>; export function forkJoin(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, v6: ObservableInput): Observable<[T, T2, T3, T4, T5, T6]>; -export function forkJoin(v1: ObservableInput, project: (v1: T) => R): Observable; -export function forkJoin(v1: ObservableInput, v2: ObservableInput, project: (v1: T, v2: T2) => R): Observable; -export function forkJoin(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, project: (v1: T, v2: T2, v3: T3) => R): Observable; -export function forkJoin(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4) => R): Observable; -export function forkJoin(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => R): Observable; -export function forkJoin(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, v6: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => R): Observable; +export function forkJoin(v1: ObservableInput): Observable; export function forkJoin(sources: Array>): Observable; -export function forkJoin(sources: Array>): Observable; -export function forkJoin(sources: Array>, project: (...values: T[]) => R): Observable; -export function forkJoin(sources: Array>, project: (...values: any[]) => R): Observable; export function forkJoin(...sources: Array>): Observable; -export function forkJoin(...sources: Array>): Observable; /* tslint:enable:max-line-length */ /** @@ -132,20 +123,10 @@ export function forkJoin(...sources: Array>): Observable * @name forkJoin * @owner Observable */ -export function forkJoin(...sources: Array | - Array> | - ((...values: T[]) => R)>): Observable { - if (sources === null || arguments.length === 0) { - return EMPTY; - } - - let resultSelector: (...values: T[]) => R = null; - if (typeof sources[sources.length - 1] === 'function') { - resultSelector = sources.pop() as ((...values: T[]) => R); - } - - // if the first and only other argument besides the resultSelector is an array - // assume it's been called with `forkJoin([obs1, obs2, obs3], resultSelector)` +export function forkJoin(...sources: Array | + Array>>): Observable { + // if the first and only other argument is an array + // assume it's been called with `forkJoin([obs1, obs2, obs3])` if (sources.length === 1 && isArray(sources[0])) { sources = sources[0] as Array>; } @@ -155,7 +136,7 @@ export function forkJoin(...sources: Array | } return new Observable(subscriber => { - return new ForkJoinSubscriber(subscriber, sources as Array>, resultSelector); + return new ForkJoinSubscriber(subscriber, sources as Array>); }); } /** @@ -169,8 +150,7 @@ class ForkJoinSubscriber extends OuterSubscriber { private haveValues = 0; constructor(destination: Subscriber, - private sources: Array>, - private resultSelector?: (...values: T[]) => R) { + private sources: Array>) { super(destination); const len = sources.length; @@ -197,7 +177,7 @@ class ForkJoinSubscriber extends OuterSubscriber { } notifyComplete(innerSub: InnerSubscriber): void { - const { destination, haveValues, resultSelector, values } = this; + const { destination, haveValues, values } = this; const len = values.length; if (!(innerSub as any)._hasValue) { @@ -212,14 +192,7 @@ class ForkJoinSubscriber extends OuterSubscriber { } if (haveValues === len) { - let result: R | T[]; - try { - result = resultSelector ? resultSelector(...values) : values; - } catch (err) { - destination.error(err); - return; - } - destination.next(result); + destination.next(values); } destination.complete();