1 import { Observable } from '../Observable';
2 import { async } from '../scheduler/async';
3 import { SchedulerAction, SchedulerLike } from '../types';
4 import { isNumeric } from '../util/isNumeric';
5 import { Subscriber } from '../Subscriber';
8 * Creates an Observable that emits sequential numbers every specified
9 * interval of time, on a specified {@link SchedulerLike}.
11 * <span class="informal">Emits incremental numbers periodically in time.
16 * `interval` returns an Observable that emits an infinite sequence of
17 * ascending integers, with a constant interval of time of your choosing
18 * between those emissions. The first emission is not sent immediately, but
19 * only after the first period has passed. By default, this operator uses the
20 * `async` {@link SchedulerLike} to provide a notion of time, but you may pass any
21 * {@link SchedulerLike} to it.
24 * Emits ascending numbers, one every second (1000ms) up to the number 3
26 * import { interval } from 'rxjs';
27 * import { take } from 'rxjs/operators';
29 * const numbers = interval(1000);
31 * const takeFourNumbers = numbers.pipe(take(4));
33 * takeFourNumbers.subscribe(x => console.log('Next: ', x));
45 * @param {number} [period=0] The interval size in milliseconds (by default)
46 * or the time unit determined by the scheduler's clock.
47 * @param {SchedulerLike} [scheduler=async] The {@link SchedulerLike} to use for scheduling
48 * the emission of values, and providing a notion of "time".
49 * @return {Observable} An Observable that emits a sequential number each time
55 export function interval(period = 0,
56 scheduler: SchedulerLike = async): Observable<number> {
57 if (!isNumeric(period) || period < 0) {
61 if (!scheduler || typeof scheduler.schedule !== 'function') {
65 return new Observable<number>(subscriber => {
67 scheduler.schedule(dispatch, period, { subscriber, counter: 0, period })
73 function dispatch(this: SchedulerAction<IntervalState>, state: IntervalState) {
74 const { subscriber, counter, period } = state;
75 subscriber.next(counter);
76 this.schedule({ subscriber, counter: counter + 1, period }, period);
79 interface IntervalState {
80 subscriber: Subscriber<number>;