source: trip-planner-front/node_modules/rxjs/src/internal/observable/timer.ts

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: 3.3 KB
Line 
1import { Observable } from '../Observable';
2import { SchedulerAction, SchedulerLike } from '../types';
3import { async } from '../scheduler/async';
4import { isNumeric } from '../util/isNumeric';
5import { isScheduler } from '../util/isScheduler';
6import { Subscriber } from '../Subscriber';
7
8/**
9 * Creates an Observable that starts emitting after an `dueTime` and
10 * emits ever increasing numbers after each `period` of time thereafter.
11 *
12 * <span class="informal">Its like {@link index/interval}, but you can specify when
13 * should the emissions start.</span>
14 *
15 * ![](timer.png)
16 *
17 * `timer` returns an Observable that emits an infinite sequence of ascending
18 * integers, with a constant interval of time, `period` of your choosing
19 * between those emissions. The first emission happens after the specified
20 * `dueTime`. The initial delay may be a `Date`. By default, this
21 * operator uses the {@link asyncScheduler} {@link SchedulerLike} to provide a notion of time, but you
22 * may pass any {@link SchedulerLike} to it. If `period` is not specified, the output
23 * Observable emits only one value, `0`. Otherwise, it emits an infinite
24 * sequence.
25 *
26 * ## Examples
27 * ### Emits ascending numbers, one every second (1000ms), starting after 3 seconds
28 * ```ts
29 * import { timer } from 'rxjs';
30 *
31 * const numbers = timer(3000, 1000);
32 * numbers.subscribe(x => console.log(x));
33 * ```
34 *
35 * ### Emits one number after five seconds
36 * ```ts
37 * import { timer } from 'rxjs';
38 *
39 * const numbers = timer(5000);
40 * numbers.subscribe(x => console.log(x));
41 * ```
42 * @see {@link index/interval}
43 * @see {@link delay}
44 *
45 * @param {number|Date} [dueTime] The initial delay time specified as a Date object or as an integer denoting
46 * milliseconds to wait before emitting the first value of 0`.
47 * @param {number|SchedulerLike} [periodOrScheduler] The period of time between emissions of the
48 * subsequent numbers.
49 * @param {SchedulerLike} [scheduler=async] The {@link SchedulerLike} to use for scheduling
50 * the emission of values, and providing a notion of "time".
51 * @return {Observable} An Observable that emits a `0` after the
52 * `dueTime` and ever increasing numbers after each `period` of time
53 * thereafter.
54 * @static true
55 * @name timer
56 * @owner Observable
57 */
58export function timer(dueTime: number | Date = 0,
59 periodOrScheduler?: number | SchedulerLike,
60 scheduler?: SchedulerLike): Observable<number> {
61 let period = -1;
62 if (isNumeric(periodOrScheduler)) {
63 period = Number(periodOrScheduler) < 1 && 1 || Number(periodOrScheduler);
64 } else if (isScheduler(periodOrScheduler)) {
65 scheduler = periodOrScheduler as any;
66 }
67
68 if (!isScheduler(scheduler)) {
69 scheduler = async;
70 }
71
72 return new Observable(subscriber => {
73 const due = isNumeric(dueTime)
74 ? (dueTime as number)
75 : (+dueTime - scheduler.now());
76
77 return scheduler.schedule(dispatch, due, {
78 index: 0, period, subscriber
79 });
80 });
81}
82
83interface TimerState {
84 index: number;
85 period: number;
86 subscriber: Subscriber<number>;
87}
88
89function dispatch(this: SchedulerAction<TimerState>, state: TimerState) {
90 const { index, period, subscriber } = state;
91 subscriber.next(index);
92
93 if (subscriber.closed) {
94 return;
95 } else if (period === -1) {
96 return subscriber.complete();
97 }
98
99 state.index = index + 1;
100 this.schedule(state, period);
101}
Note: See TracBrowser for help on using the repository browser.