Debian Codegraph @

node/rxjs/src/internal/Scheduler.ts

raw
1 import { Action } from './scheduler/Action';
2 import { Subscription } from './Subscription';
3 import { SchedulerLike, SchedulerAction } from './types';
4 import { dateTimestampProvider } from './scheduler/dateTimestampProvider';
5
6 /**
7 * An execution context and a data structure to order tasks and schedule their
8 * execution. Provides a notion of (potentially virtual) time, through the
9 * `now()` getter method.
10 *
11 * Each unit of work in a Scheduler is called an `Action`.
12 *
13 * ```ts
14 * class Scheduler {
15 * now(): number;
16 * schedule(work, delay?, state?): Subscription;
17 * }
18 * ```
19 *
20 * @deprecated Scheduler is an internal implementation detail of RxJS, and
21 * should not be used directly. Rather, create your own class and implement
22 * {@link SchedulerLike}. Will be made internal in v8.
23 */
24 export class Scheduler implements SchedulerLike {
25 public static now: () => number = dateTimestampProvider.now;
26
27 constructor(private schedulerActionCtor: typeof Action, now: () => number = Scheduler.now) {
28 this.now = now;
29 }
30
31 /**
32 * A getter method that returns a number representing the current time
33 * (at the time this function was called) according to the scheduler's own
34 * internal clock.
35 * @return A number that represents the current time. May or may not
36 * have a relation to wall-clock time. May or may not refer to a time unit
37 * (e.g. milliseconds).
38 */
39 public now: () => number;
40
41 /**
42 * Schedules a function, `work`, for execution. May happen at some point in
43 * the future, according to the `delay` parameter, if specified. May be passed
44 * some context object, `state`, which will be passed to the `work` function.
45 *
46 * The given arguments will be processed an stored as an Action object in a
47 * queue of actions.
48 *
49 * @param work A function representing a task, or some unit of work to be
50 * executed by the Scheduler.
51 * @param delay Time to wait before executing the work, where the time unit is
52 * implicit and defined by the Scheduler itself.
53 * @param state Some contextual data that the `work` function uses when called
54 * by the Scheduler.
55 * @return A subscription in order to be able to unsubscribe the scheduled work.
56 */
57 public schedule<T>(work: (this: SchedulerAction<T>, state?: T) => void, delay: number = 0, state?: T): Subscription {
58 return new this.schedulerActionCtor<T>(this, work).schedule(state, delay);
59 }
60 }