| 1 |
import { Action } from './Action'; |
| 2 |
import { SchedulerAction } from '../types'; |
| 3 |
import { Subscription } from '../Subscription'; |
| 4 |
import { AsyncScheduler } from './AsyncScheduler'; |
| 5 |
import { intervalProvider } from './intervalProvider'; |
| 6 |
import { arrRemove } from '../util/arrRemove'; |
| 7 |
import { TimerHandle } from './timerHandle'; |
| 8 |
|
| 9 |
export class AsyncAction<T> extends Action<T> { |
| 10 |
public id: TimerHandle | undefined; |
| 11 |
public state?: T; |
| 12 |
// @ts-ignore: Property has no initializer and is not definitely assigned |
| 13 |
public delay: number; |
| 14 |
protected pending: boolean = false; |
| 15 |
|
| 16 |
constructor(protected scheduler: AsyncScheduler, protected work: (this: SchedulerAction<T>, state?: T) => void) { |
| 17 |
super(scheduler, work); |
| 18 |
} |
| 19 |
|
| 20 |
public schedule(state?: T, delay: number = 0): Subscription { |
| 21 |
if (this.closed) { |
| 22 |
return this; |
| 23 |
} |
| 24 |
|
| 25 |
// Always replace the current state with the new state. |
| 26 |
this.state = state; |
| 27 |
|
| 28 |
const id = this.id; |
| 29 |
const scheduler = this.scheduler; |
| 30 |
|
| 31 |
// |
| 32 |
// Important implementation note: |
| 33 |
// |
| 34 |
// Actions only execute once by default, unless rescheduled from within the |
| 35 |
// scheduled callback. This allows us to implement single and repeat |
| 36 |
// actions via the same code path, without adding API surface area, as well |
| 37 |
// as mimic traditional recursion but across asynchronous boundaries. |
| 38 |
// |
| 39 |
// However, JS runtimes and timers distinguish between intervals achieved by |
| 40 |
// serial `setTimeout` calls vs. a single `setInterval` call. An interval of |
| 41 |
// serial `setTimeout` calls can be individually delayed, which delays |
| 42 |
// scheduling the next `setTimeout`, and so on. `setInterval` attempts to |
| 43 |
// guarantee the interval callback will be invoked more precisely to the |
| 44 |
// interval period, regardless of load. |
| 45 |
// |
| 46 |
// Therefore, we use `setInterval` to schedule single and repeat actions. |
| 47 |
// If the action reschedules itself with the same delay, the interval is not |
| 48 |
// canceled. If the action doesn't reschedule, or reschedules with a |
| 49 |
// different delay, the interval will be canceled after scheduled callback |
| 50 |
// execution. |
| 51 |
// |
| 52 |
if (id != null) { |
| 53 |
this.id = this.recycleAsyncId(scheduler, id, delay); |
| 54 |
} |
| 55 |
|
| 56 |
// Set the pending flag indicating that this action has been scheduled, or |
| 57 |
// has recursively rescheduled itself. |
| 58 |
this.pending = true; |
| 59 |
|
| 60 |
this.delay = delay; |
| 61 |
// If this action has already an async Id, don't request a new one. |
| 62 |
this.id = this.id ?? this.requestAsyncId(scheduler, this.id, delay); |
| 63 |
|
| 64 |
return this; |
| 65 |
} |
| 66 |
|
| 67 |
protected requestAsyncId(scheduler: AsyncScheduler, _id?: TimerHandle, delay: number = 0): TimerHandle { |
| 68 |
return intervalProvider.setInterval(scheduler.flush.bind(scheduler, this), delay); |
| 69 |
} |
| 70 |
|
| 71 |
protected recycleAsyncId(_scheduler: AsyncScheduler, id?: TimerHandle, delay: number | null = 0): TimerHandle | undefined { |
| 72 |
// If this action is rescheduled with the same delay time, don't clear the interval id. |
| 73 |
if (delay != null && this.delay === delay && this.pending === false) { |
| 74 |
return id; |
| 75 |
} |
| 76 |
// Otherwise, if the action's delay time is different from the current delay, |
| 77 |
// or the action has been rescheduled before it's executed, clear the interval id |
| 78 |
if (id != null) { |
| 79 |
intervalProvider.clearInterval(id); |
| 80 |
} |
| 81 |
|
| 82 |
return undefined; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Immediately executes this action and the `work` it contains. |
| 87 |
*/ |
| 88 |
public execute(state: T, delay: number): any { |
| 89 |
if (this.closed) { |
| 90 |
return new Error('executing a cancelled action'); |
| 91 |
} |
| 92 |
|
| 93 |
this.pending = false; |
| 94 |
const error = this._execute(state, delay); |
| 95 |
if (error) { |
| 96 |
return error; |
| 97 |
} else if (this.pending === false && this.id != null) { |
| 98 |
// Dequeue if the action didn't reschedule itself. Don't call |
| 99 |
// unsubscribe(), because the action could reschedule later. |
| 100 |
// For example: |
| 101 |
// ``` |
| 102 |
// scheduler.schedule(function doWork(counter) { |
| 103 |
// /* ... I'm a busy worker bee ... */ |
| 104 |
// var originalAction = this; |
| 105 |
// /* wait 100ms before rescheduling the action */ |
| 106 |
// setTimeout(function () { |
| 107 |
// originalAction.schedule(counter + 1); |
| 108 |
// }, 100); |
| 109 |
// }, 1000); |
| 110 |
// ``` |
| 111 |
this.id = this.recycleAsyncId(this.scheduler, this.id, null); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
protected _execute(state: T, _delay: number): any { |
| 116 |
let errored: boolean = false; |
| 117 |
let errorValue: any; |
| 118 |
try { |
| 119 |
this.work(state); |
| 120 |
} catch (e) { |
| 121 |
errored = true; |
| 122 |
// HACK: Since code elsewhere is relying on the "truthiness" of the |
| 123 |
// return here, we can't have it return "" or 0 or false. |
| 124 |
// TODO: Clean this up when we refactor schedulers mid-version-8 or so. |
| 125 |
errorValue = e ? e : new Error('Scheduled action threw falsy error'); |
| 126 |
} |
| 127 |
if (errored) { |
| 128 |
this.unsubscribe(); |
| 129 |
return errorValue; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
unsubscribe() { |
| 134 |
if (!this.closed) { |
| 135 |
const { id, scheduler } = this; |
| 136 |
const { actions } = scheduler; |
| 137 |
|
| 138 |
this.work = this.state = this.scheduler = null!; |
| 139 |
this.pending = false; |
| 140 |
|
| 141 |
arrRemove(actions, this); |
| 142 |
if (id != null) { |
| 143 |
this.id = this.recycleAsyncId(scheduler, id, null); |
| 144 |
} |
| 145 |
|
| 146 |
this.delay = null!; |
| 147 |
super.unsubscribe(); |
| 148 |
} |
| 149 |
} |
| 150 |
} |