Skip to content

Commit

Permalink
feat(Observable.prototype.pipe): Now supports passing an array of ope…
Browse files Browse the repository at this point in the history
…rators directly

resolves ReactiveX#4177
  • Loading branch information
benlesh committed Sep 24, 2018
1 parent 2ac0f1f commit aa178af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 5 additions & 4 deletions spec-dtslint/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ describe('pipe', () => {
const o: Observable<'10'> = of('foo').pipe(a('1'), a('2'), a('3'), a('4'), a('5'), a('6'), a('7'), a('8'), a('9'), a('10')); // $ExpectError
});

it('should infer any for a spread of an array of operators', () => {
const operators = ['1', '2', '3', '4'].map(n => a(n));
const o = of('foo').pipe(...operators); // $ExpectType Observable<any>
it('should support an array of operators but require typing', () => {
const operators = Array.from({ length: 100 }, n => '' + n).map(a);
const o = of('foo').pipe(operators); // $ExpectType Observable<{}>
const o2 = of('foo').pipe<string>(operators); // $ExpectType Observable<string>
});

it('should enforce types for the 1st argument', () => {
const o = of('foo').pipe(a('#', '1')); // $ExpectError
});

it('should enforce types for the 2nd argument', () => {
const o = of('foo').pipe(a('1'), a('#', '2')); // $ExpectError
const o = of('foo').pipe(a('1'), (source: Observable<number>) => new Observable<string>()); // $ExpectError
});

it('should enforce types for the 3rd argument', () => {
Expand Down
13 changes: 9 additions & 4 deletions src/internal/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export class Observable<T> implements Subscribable<T> {
pipe<A, B, C, D, E, F, G, H>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>): Observable<H>;
pipe<A, B, C, D, E, F, G, H, I>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>, op9: OperatorFunction<H, I>): Observable<I>;
pipe<A, B, C, D, E, F, G, H, I>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, E>, op6: OperatorFunction<E, F>, op7: OperatorFunction<F, G>, op8: OperatorFunction<G, H>, op9: OperatorFunction<H, I>, ...operations: OperatorFunction<any, any>[]): Observable<{}>;
pipe(...operators: OperatorFunction<any, any>[]): Observable<any>;
pipe<R>(operations: OperatorFunction<any, any>[]): Observable<R>;
/* tslint:enable:max-line-length */

/**
Expand All @@ -324,12 +324,17 @@ export class Observable<T> implements Subscribable<T> {
* .subscribe(x => console.log(x))
* ```
*/
pipe(...operations: OperatorFunction<any, any>[]): Observable<any> {
if (operations.length === 0) {
pipe(...args: any[]): Observable<any> {
if (args.length === 0) {
return this as any;
}

return pipeFromArray(operations)(this);
const first = args[0];
if (Array.isArray(first)) {
return pipeFromArray(first)(this) as Observable<any>;
}

return pipeFromArray(args)(this) as Observable<any>;
}

/* tslint:disable:max-line-length */
Expand Down

0 comments on commit aa178af

Please sign in to comment.