Skip to content

Commit a03a50c

Browse files
committed
feat(toArray): add higher-order lettable version of toArray
1 parent 0b73208 commit a03a50c

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

src/operator/toArray.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
1-
import { Operator } from '../Operator';
2-
import { Subscriber } from '../Subscriber';
1+
32
import { Observable } from '../Observable';
3+
import { toArray as higherOrder } from '../operators';
44

55
/**
66
* @return {Observable<any[]>|WebSocketSubject<T>|Observable<T>}
77
* @method toArray
88
* @owner Observable
99
*/
1010
export function toArray<T>(this: Observable<T>): Observable<T[]> {
11-
return this.lift(new ToArrayOperator());
12-
}
13-
14-
class ToArrayOperator<T> implements Operator<T, T[]> {
15-
call(subscriber: Subscriber<T[]>, source: any): any {
16-
return source.subscribe(new ToArraySubscriber(subscriber));
17-
}
18-
}
19-
20-
/**
21-
* We need this JSDoc comment for affecting ESDoc.
22-
* @ignore
23-
* @extends {Ignored}
24-
*/
25-
class ToArraySubscriber<T> extends Subscriber<T> {
26-
27-
private array: T[] = [];
28-
29-
constructor(destination: Subscriber<T[]>) {
30-
super(destination);
31-
}
32-
33-
protected _next(x: T) {
34-
this.array.push(x);
35-
}
36-
37-
protected _complete() {
38-
this.destination.next(this.array);
39-
this.destination.complete();
40-
}
41-
}
11+
return higherOrder()(this);
12+
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export { switchAll } from './switchAll';
2222
export { switchMap } from './switchMap';
2323
export { takeLast } from './takeLast';
2424
export { tap } from './tap';
25+
export { toArray } from './toArray';
2526
export { window } from './window';
2627
export { windowCount } from './windowCount';
2728
export { windowTime } from './windowTime';

src/operators/toArray.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { reduce } from './reduce';
2+
import { OperatorFunction } from '../interfaces';
3+
4+
function toArrayReducer<T>(acc: T[], value: T) {
5+
acc.push(value);
6+
return acc;
7+
}
8+
9+
export function toArray<T>(): OperatorFunction<T, T[]> {
10+
return reduce(toArrayReducer, []);
11+
}

0 commit comments

Comments
 (0)