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