Debian Codegraph @

node/rxjs/src/internal/observable/range.ts

raw
1 import { SchedulerLike } from '../types';
2 import { Observable } from '../Observable';
3 import { EMPTY } from './empty';
4
5 export function range(start: number, count?: number): Observable<number>;
6
7 /**
8 * @deprecated The `scheduler` parameter will be removed in v8. Use `range(start, count).pipe(observeOn(scheduler))` instead. Details: Details: https://rxjs.dev/deprecations/scheduler-argument
9 */
10 export function range(start: number, count: number | undefined, scheduler: SchedulerLike): Observable<number>;
11
12 /**
13 * Creates an Observable that emits a sequence of numbers within a specified
14 * range.
15 *
16 * <span class="informal">Emits a sequence of numbers in a range.</span>
17 *
18 * ![](range.png)
19 *
20 * `range` operator emits a range of sequential integers, in order, where you
21 * select the `start` of the range and its `length`. By default, uses no
22 * {@link SchedulerLike} and just delivers the notifications synchronously, but may use
23 * an optional {@link SchedulerLike} to regulate those deliveries.
24 *
25 * ## Example
26 *
27 * Produce a range of numbers
28 *
29 * ```ts
30 * import { range } from 'rxjs';
31 *
32 * const numbers = range(1, 3);
33 *
34 * numbers.subscribe({
35 * next: value => console.log(value),
36 * complete: () => console.log('Complete!')
37 * });
38 *
39 * // Logs:
40 * // 1
41 * // 2
42 * // 3
43 * // 'Complete!'
44 * ```
45 *
46 * @see {@link timer}
47 * @see {@link interval}
48 *
49 * @param start The value of the first integer in the sequence.
50 * @param count The number of sequential integers to generate.
51 * @param scheduler A {@link SchedulerLike} to use for scheduling the emissions
52 * of the notifications.
53 * @return An Observable of numbers that emits a finite range of sequential integers.
54 */
55 export function range(start: number, count?: number, scheduler?: SchedulerLike): Observable<number> {
56 if (count == null) {
57 // If one argument was passed, it's the count, not the start.
58 count = start;
59 start = 0;
60 }
61
62 if (count <= 0) {
63 // No count? We're going nowhere. Return EMPTY.
64 return EMPTY;
65 }
66
67 // Where the range should stop.
68 const end = count + start;
69
70 return new Observable(
71 scheduler
72 ? // The deprecated scheduled path.
73 (subscriber) => {
74 let n = start;
75 return scheduler.schedule(function () {
76 if (n < end) {
77 subscriber.next(n++);
78 this.schedule();
79 } else {
80 subscriber.complete();
81 }
82 });
83 }
84 : // Standard synchronous range.
85 (subscriber) => {
86 let n = start;
87 while (n < end && !subscriber.closed) {
88 subscriber.next(n++);
89 }
90 subscriber.complete();
91 }
92 );
93 }