diff --git a/src/operator/toArray.ts b/src/operator/toArray.ts index 05f815ad75..78111f9f81 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); } 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..9ca61b63af --- /dev/null +++ b/src/operators/toArray.ts @@ -0,0 +1,11 @@ +import { reduce } from './reduce'; +import { OperatorFunction } from '../interfaces'; + +function toArrayReducer(arr: T[], item: T, index: number) { + arr.push(item); + return arr; +} + +export function toArray(): OperatorFunction { + return reduce(toArrayReducer, []); +}