1 import { Action } from './Action';
2 import { SchedulerAction } from '../types';
3 import { Subscription } from '../Subscription';
4 import { AsyncScheduler } from './AsyncScheduler';
7 * We need this JSDoc comment for affecting ESDoc.
11 export class AsyncAction<T> extends Action<T> {
16 protected pending: boolean = false;
18 constructor(protected scheduler: AsyncScheduler,
19 protected work: (this: SchedulerAction<T>, state?: T) => void) {
20 super(scheduler, work);
23 public schedule(state?: T, delay: number = 0): Subscription {
29 // Always replace the current state with the new state.
33 const scheduler = this.scheduler;
36 // Important implementation note:
38 // Actions only execute once by default, unless rescheduled from within the
39 // scheduled callback. This allows us to implement single and repeat
40 // actions via the same code path, without adding API surface area, as well
41 // as mimic traditional recursion but across asynchronous boundaries.
43 // However, JS runtimes and timers distinguish between intervals achieved by
44 // serial `setTimeout` calls vs. a single `setInterval` call. An interval of
45 // serial `setTimeout` calls can be individually delayed, which delays
46 // scheduling the next `setTimeout`, and so on. `setInterval` attempts to
47 // guarantee the interval callback will be invoked more precisely to the
48 // interval period, regardless of load.
50 // Therefore, we use `setInterval` to schedule single and repeat actions.
51 // If the action reschedules itself with the same delay, the interval is not
52 // canceled. If the action doesn't reschedule, or reschedules with a
53 // different delay, the interval will be canceled after scheduled callback
57 this.id = this.recycleAsyncId(scheduler, id, delay);
60 // Set the pending flag indicating that this action has been scheduled, or
61 // has recursively rescheduled itself.
65 // If this action has already an async Id, don't request a new one.
66 this.id = this.id || this.requestAsyncId(scheduler, this.id, delay);
71 protected requestAsyncId(scheduler: AsyncScheduler, id?: any, delay: number = 0): any {
72 return setInterval(scheduler.flush.bind(scheduler, this), delay);
75 protected recycleAsyncId(scheduler: AsyncScheduler, id: any, delay: number = 0): any {
76 // If this action is rescheduled with the same delay time, don't clear the interval id.
77 if (delay !== null && this.delay === delay && this.pending === false) {
80 // Otherwise, if the action's delay time is different from the current delay,
81 // or the action has been rescheduled before it's executed, clear the interval id
87 * Immediately executes this action and the `work` it contains.
90 public execute(state: T, delay: number): any {
93 return new Error('executing a cancelled action');
97 const error = this._execute(state, delay);
100 } else if (this.pending === false && this.id != null) {
101 // Dequeue if the action didn't reschedule itself. Don't call
102 // unsubscribe(), because the action could reschedule later.
105 // scheduler.schedule(function doWork(counter) {
106 // /* ... I'm a busy worker bee ... */
107 // var originalAction = this;
108 // /* wait 100ms before rescheduling the action */
109 // setTimeout(function () {
110 // originalAction.schedule(counter + 1);
114 this.id = this.recycleAsyncId(this.scheduler, this.id, null);
118 protected _execute(state: T, delay: number): any {
119 let errored: boolean = false;
120 let errorValue: any = undefined;
125 errorValue = !!e && e || new Error(e);
133 /** @deprecated This is an internal implementation detail, do not use. */
137 const scheduler = this.scheduler;
138 const actions = scheduler.actions;
139 const index = actions.indexOf(this);
143 this.pending = false;
144 this.scheduler = null;
147 actions.splice(index, 1);
151 this.id = this.recycleAsyncId(scheduler, id, null);