1 | import { SchedulerAction, SchedulerLike } from '../types';
|
---|
2 | import { Observable } from '../Observable';
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Creates an Observable that emits a sequence of numbers within a specified
|
---|
6 | * range.
|
---|
7 | *
|
---|
8 | * <span class="informal">Emits a sequence of numbers in a range.</span>
|
---|
9 | *
|
---|
10 | * ![](range.png)
|
---|
11 | *
|
---|
12 | * `range` operator emits a range of sequential integers, in order, where you
|
---|
13 | * select the `start` of the range and its `length`. By default, uses no
|
---|
14 | * {@link SchedulerLike} and just delivers the notifications synchronously, but may use
|
---|
15 | * an optional {@link SchedulerLike} to regulate those deliveries.
|
---|
16 | *
|
---|
17 | * ## Example
|
---|
18 | * Emits the numbers 1 to 10</caption>
|
---|
19 | * ```ts
|
---|
20 | * import { range } from 'rxjs';
|
---|
21 | *
|
---|
22 | * const numbers = range(1, 10);
|
---|
23 | * numbers.subscribe(x => console.log(x));
|
---|
24 | * ```
|
---|
25 | * @see {@link timer}
|
---|
26 | * @see {@link index/interval}
|
---|
27 | *
|
---|
28 | * @param {number} [start=0] The value of the first integer in the sequence.
|
---|
29 | * @param {number} count The number of sequential integers to generate.
|
---|
30 | * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} to use for scheduling
|
---|
31 | * the emissions of the notifications.
|
---|
32 | * @return {Observable} An Observable of numbers that emits a finite range of
|
---|
33 | * sequential integers.
|
---|
34 | * @static true
|
---|
35 | * @name range
|
---|
36 | * @owner Observable
|
---|
37 | */
|
---|
38 | export function range(start: number = 0,
|
---|
39 | count?: number,
|
---|
40 | scheduler?: SchedulerLike): Observable<number> {
|
---|
41 | return new Observable<number>(subscriber => {
|
---|
42 | if (count === undefined) {
|
---|
43 | count = start;
|
---|
44 | start = 0;
|
---|
45 | }
|
---|
46 |
|
---|
47 | let index = 0;
|
---|
48 | let current = start;
|
---|
49 |
|
---|
50 | if (scheduler) {
|
---|
51 | return scheduler.schedule(dispatch, 0, {
|
---|
52 | index, count, start, subscriber
|
---|
53 | });
|
---|
54 | } else {
|
---|
55 | do {
|
---|
56 | if (index++ >= count) {
|
---|
57 | subscriber.complete();
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | subscriber.next(current++);
|
---|
61 | if (subscriber.closed) {
|
---|
62 | break;
|
---|
63 | }
|
---|
64 | } while (true);
|
---|
65 | }
|
---|
66 |
|
---|
67 | return undefined;
|
---|
68 | });
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** @internal */
|
---|
72 | export function dispatch(this: SchedulerAction<any>, state: any) {
|
---|
73 | const { start, index, count, subscriber } = state;
|
---|
74 |
|
---|
75 | if (index >= count) {
|
---|
76 | subscriber.complete();
|
---|
77 | return;
|
---|
78 | }
|
---|
79 |
|
---|
80 | subscriber.next(start);
|
---|
81 |
|
---|
82 | if (subscriber.closed) {
|
---|
83 | return;
|
---|
84 | }
|
---|
85 |
|
---|
86 | state.index = index + 1;
|
---|
87 | state.start = start + 1;
|
---|
88 |
|
---|
89 | this.schedule(state);
|
---|
90 | }
|
---|