Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | import { reduce } from './reduce';
|
---|
2 | import { OperatorFunction } from '../types';
|
---|
3 |
|
---|
4 | function toArrayReducer<T>(arr: T[], item: T, index: number) {
|
---|
5 | if (index === 0) {
|
---|
6 | return [item];
|
---|
7 | }
|
---|
8 | arr.push(item);
|
---|
9 | return arr;
|
---|
10 | }
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Collects all source emissions and emits them as an array when the source completes.
|
---|
14 | *
|
---|
15 | * <span class="informal">Get all values inside an array when the source completes</span>
|
---|
16 | *
|
---|
17 | * ![](toArray.png)
|
---|
18 | *
|
---|
19 | * `toArray` will wait until the source Observable completes before emitting
|
---|
20 | * the array containing all emissions. When the source Observable errors no
|
---|
21 | * array will be emitted.
|
---|
22 | *
|
---|
23 | * ## Example
|
---|
24 | * ```ts
|
---|
25 | * import { interval } from 'rxjs';
|
---|
26 | * import { toArray, take } from 'rxjs/operators';
|
---|
27 | *
|
---|
28 | * const source = interval(1000);
|
---|
29 | * const example = source.pipe(
|
---|
30 | * take(10),
|
---|
31 | * toArray()
|
---|
32 | * );
|
---|
33 | *
|
---|
34 | * const subscribe = example.subscribe(val => console.log(val));
|
---|
35 | *
|
---|
36 | * // output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
---|
37 | *
|
---|
38 | * ```
|
---|
39 | * @return An array from an observable sequence.
|
---|
40 | * @method toArray
|
---|
41 | * @owner Observable
|
---|
42 | */
|
---|
43 | export function toArray<T>(): OperatorFunction<T, T[]> {
|
---|
44 | return reduce(toArrayReducer, [] as T[]);
|
---|
45 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.