From a03a50c02852a88ab5947453809288183e32ccb2 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 4 Jul 2017 17:28:14 -0700 Subject: [PATCH] feat(toArray): add higher-order lettable version of toArray --- src/operator/toArray.ts | 37 ++++--------------------------------- src/operators/index.ts | 1 + src/operators/toArray.ts | 11 +++++++++++ 3 files changed, 16 insertions(+), 33 deletions(-) create mode 100644 src/operators/toArray.ts diff --git a/src/operator/toArray.ts b/src/operator/toArray.ts index 05f815ad75..e9be3eab93 100644 --- a/src/operator/toArray.ts +++ b/src/operator/toArray.ts @@ -1,6 +1,6 @@ -import { Operator } from '../Operator'; -import { Subscriber } from '../Subscriber'; + import { Observable } from '../Observable'; +import { toArray as higherOrder } from '../operators'; /** * @return {Observable|WebSocketSubject|Observable} @@ -8,34 +8,5 @@ import { Observable } from '../Observable'; * @owner Observable */ export function toArray(this: Observable): Observable { - return this.lift(new ToArrayOperator()); -} - -class ToArrayOperator implements Operator { - call(subscriber: Subscriber, source: any): any { - return source.subscribe(new ToArraySubscriber(subscriber)); - } -} - -/** - * We need this JSDoc comment for affecting ESDoc. - * @ignore - * @extends {Ignored} - */ -class ToArraySubscriber extends Subscriber { - - private array: T[] = []; - - constructor(destination: Subscriber) { - super(destination); - } - - protected _next(x: T) { - this.array.push(x); - } - - protected _complete() { - this.destination.next(this.array); - this.destination.complete(); - } -} + return higherOrder()(this); +} \ No newline at end of file diff --git a/src/operators/index.ts b/src/operators/index.ts index 5f60956cd1..55cff5b4c9 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -22,6 +22,7 @@ export { switchAll } from './switchAll'; export { switchMap } from './switchMap'; export { takeLast } from './takeLast'; export { tap } from './tap'; +export { toArray } from './toArray'; export { window } from './window'; export { windowCount } from './windowCount'; export { windowTime } from './windowTime'; diff --git a/src/operators/toArray.ts b/src/operators/toArray.ts new file mode 100644 index 0000000000..4236c6c46d --- /dev/null +++ b/src/operators/toArray.ts @@ -0,0 +1,11 @@ +import { reduce } from './reduce'; +import { OperatorFunction } from '../interfaces'; + +function toArrayReducer(acc: T[], value: T) { + acc.push(value); + return acc; +} + +export function toArray(): OperatorFunction { + return reduce(toArrayReducer, []); +} \ No newline at end of file