1 import { Scheduler } from '../Scheduler';
2 import { Action } from './Action';
3 import { AsyncAction } from './AsyncAction';
4 import { SchedulerAction } from '../types';
5 import { Subscription } from '../Subscription';
7 export class AsyncScheduler extends Scheduler {
8 public static delegate?: Scheduler;
9 public actions: Array<AsyncAction<any>> = [];
11 * A flag to indicate whether the Scheduler is currently executing a batch of
14 * @deprecated internal use only
16 public active: boolean = false;
18 * An internal ID used to track the latest asynchronous task such as those
19 * coming from `setTimeout`, `setInterval`, `requestAnimationFrame`, and
22 * @deprecated internal use only
24 public scheduled: any = undefined;
26 constructor(SchedulerAction: typeof Action,
27 now: () => number = Scheduler.now) {
28 super(SchedulerAction, () => {
29 if (AsyncScheduler.delegate && AsyncScheduler.delegate !== this) {
30 return AsyncScheduler.delegate.now();
37 public schedule<T>(work: (this: SchedulerAction<T>, state?: T) => void, delay: number = 0, state?: T): Subscription {
38 if (AsyncScheduler.delegate && AsyncScheduler.delegate !== this) {
39 return AsyncScheduler.delegate.schedule(work, delay, state);
41 return super.schedule(work, delay, state);
45 public flush(action: AsyncAction<any>): void {
47 const {actions} = this;
58 if (error = action.execute(action.state, action.delay)) {
61 } while (action = actions.shift()); // exhaust the scheduler queue
66 while (action = actions.shift()) {