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

refactor(forkJoin): readd resultSelector as deprecated #3498

Merged
merged 1 commit into from Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions spec/observables/forkJoin-spec.ts
Expand Up @@ -19,6 +19,54 @@ describe('forkJoin', () => {
expectObservable(e1).toBe(expected, {x: ['d', '3']});
});

it('should support the deprecated resultSelector with an Array of ObservableInputs', () => {
const results: Array<number|string> = [];
forkJoin(
[
of(1, 2, 3),
of(4, 5, 6),
of(7, 8, 9),
],
(a: number, b: number, c: number) => a + b + c,
)
.subscribe({
next(value) {
results.push(value);
},
error(err) {
throw err;
},
complete() {
results.push('done');
}
});

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

it('should support the deprecated resultSelector with a spread of ObservableInputs', () => {
const results: Array<number|string> = [];
forkJoin(
of(1, 2, 3),
of(4, 5, 6),
of(7, 8, 9),
(a: number, b: number, c: number) => a + b + c,
)
.subscribe({
next(value) {
results.push(value);
},
error(err) {
throw err;
},
complete() {
results.push('done');
}
});

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

it('should join the last values of the provided observables into an array', () => {
const e1 = forkJoin(
hot('--a--b--c--d--|'),
Expand Down
34 changes: 25 additions & 9 deletions src/internal/observable/forkJoin.ts
Expand Up @@ -6,6 +6,7 @@ import { subscribeToResult } from '../util/subscribeToResult';
import { OuterSubscriber } from '../OuterSubscriber';
import { InnerSubscriber } from '../InnerSubscriber';
import { Subscriber } from '../Subscriber';
import { map } from '../operators/map';

/* tslint:disable:max-line-length */
// forkJoin([a$, b$, c$]);
Expand All @@ -24,7 +25,10 @@ export function forkJoin<T, T2, T3>(v1: ObservableInput<T>, v2: ObservableInput<
export function forkJoin<T, T2, T3, T4>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<[T, T2, T3, T4]>;
export function forkJoin<T, T2, T3, T4, T5>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<[T, T2, T3, T4, T5]>;
export function forkJoin<T, T2, T3, T4, T5, T6>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<[T, T2, T3, T4, T5, T6]>;
export function forkJoin<T>(...sources: Array<ObservableInput<T>>): Observable<T[]>;

/** @deprecated resultSelector is deprecated, pipe to map instead */
export function forkJoin(...args: Array<ObservableInput<any>|Function>): Observable<any>;
export function forkJoin<T>(...sources: ObservableInput<T>[]): Observable<T[]>;
/* tslint:enable:max-line-length */

/**
Expand Down Expand Up @@ -64,7 +68,7 @@ export function forkJoin<T>(...sources: Array<ObservableInput<T>>): Observable<T
* when output Observable is supposed to emit a result.
*
* @example <caption>Use forkJoin with operator emitting immediately</caption>
* import { forkJoin, of } from 'rxjs/create';
* import { forkJoin, of } from 'rxjs';
*
* const observable = forkJoin(
* of(1, 2, 3, 4),
Expand All @@ -82,7 +86,7 @@ export function forkJoin<T>(...sources: Array<ObservableInput<T>>): Observable<T
*
*
* @example <caption>Use forkJoin with operator emitting after some time</caption>
* import { forkJoin, interval } from 'rxjs/create';
* import { forkJoin, interval } from 'rxjs';
* import { take } from 'rxjs/operators';
*
* const observable = forkJoin(
Expand All @@ -101,7 +105,7 @@ export function forkJoin<T>(...sources: Array<ObservableInput<T>>): Observable<T
*
*
* @example <caption>Use forkJoin with project function</caption>
* import { jorkJoin, interval } from 'rxjs/create';
* import { jorkJoin, interval } from 'rxjs';
* import { take } from 'rxjs/operators';
*
* const observable = forkJoin(
Expand All @@ -128,12 +132,17 @@ export function forkJoin<T>(...sources: Array<ObservableInput<T>>): Observable<T
* that will appear in resulting Observable instead of default array.
* @return {Observable} Observable emitting either an array of last values emitted by passed Observables
* or value from project function.
* @static true
* @name forkJoin
* @owner Observable
*/
export function forkJoin<T>(...sources: Array<ObservableInput<T> |
Array<ObservableInput<T>>>): Observable<T[]> {
export function forkJoin<T>(
...sources: Array<ObservableInput<T> | ObservableInput<T>[] | Function>
): Observable<T[]> {

let resultSelector: Function;
if (typeof sources[sources.length - 1] === 'function') {
// DEPRECATED PATH
resultSelector = sources.pop() as Function;
}

// 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])) {
Expand All @@ -144,6 +153,13 @@ export function forkJoin<T>(...sources: Array<ObservableInput<T> |
return EMPTY;
}

if (resultSelector) {
// DEPRECATED PATH
return forkJoin(sources).pipe(
map(args => resultSelector(...args))
);
}

return new Observable(subscriber => {
return new ForkJoinSubscriber(subscriber, sources as Array<ObservableInput<T>>);
});
Expand Down