-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toArray): add higher-order lettable version of toArray
- Loading branch information
Showing
3 changed files
with
16 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,12 @@ | ||
import { Operator } from '../Operator'; | ||
import { Subscriber } from '../Subscriber'; | ||
|
||
import { Observable } from '../Observable'; | ||
import { toArray as higherOrder } from '../operators'; | ||
|
||
/** | ||
* @return {Observable<any[]>|WebSocketSubject<T>|Observable<T>} | ||
* @method toArray | ||
* @owner Observable | ||
*/ | ||
export function toArray<T>(this: Observable<T>): Observable<T[]> { | ||
return this.lift(new ToArrayOperator()); | ||
} | ||
|
||
class ToArrayOperator<T> implements Operator<T, T[]> { | ||
call(subscriber: Subscriber<T[]>, source: any): any { | ||
return source.subscribe(new ToArraySubscriber(subscriber)); | ||
} | ||
} | ||
|
||
/** | ||
* We need this JSDoc comment for affecting ESDoc. | ||
* @ignore | ||
* @extends {Ignored} | ||
*/ | ||
class ToArraySubscriber<T> extends Subscriber<T> { | ||
|
||
private array: T[] = []; | ||
|
||
constructor(destination: Subscriber<T[]>) { | ||
super(destination); | ||
} | ||
|
||
protected _next(x: T) { | ||
this.array.push(x); | ||
} | ||
|
||
protected _complete() { | ||
this.destination.next(this.array); | ||
this.destination.complete(); | ||
} | ||
} | ||
return higherOrder()(this); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { reduce } from './reduce'; | ||
import { OperatorFunction } from '../interfaces'; | ||
|
||
function toArrayReducer<T>(acc: T[], value: T) { | ||
acc.push(value); | ||
return acc; | ||
} | ||
|
||
export function toArray<T>(): OperatorFunction<T, T[]> { | ||
return reduce(toArrayReducer, []); | ||
} |