UNPKG

954 BPlain TextView Raw
1
2import { Observable } from 'rxjs';
3import { toArray as higherOrder } from 'rxjs/operators';
4
5/**
6 * Collects all source emissions and emits them as an array when the source completes.
7 *
8 * <span class="informal">Get all values inside an array when the source completes</span>
9 *
10 * <img src="./img/toArray.png" width="100%">
11 *
12 * `toArray` will wait until the source Observable completes
13 * before emitting the array containing all emissions.
14 * When the source Observable errors no array will be emitted.
15 *
16 * @example <caption>Create array from input</caption>
17 * const input = Rx.Observable.interval(100).take(4);
18 *
19 * input.toArray()
20 * .subscribe(arr => console.log(arr)); // [0,1,2,3]
21 *
22 * @see {@link buffer}
23 *
24 * @return {Observable<any[]>|WebSocketSubject<T>|Observable<T>}
25 * @method toArray
26 * @owner Observable
27 */
28export function toArray<T>(this: Observable<T>): Observable<T[]> {
29 return higherOrder()(this) as Observable<T[]>;
30}