rxjs-zone-less
Version:
A set of wrappers for RxJS to avoid unnecessary change detection and zone interference in Angular.
44 lines (43 loc) • 1.31 kB
JavaScript
import { Scheduler } from '../scheduler';
export class AsyncScheduler extends Scheduler {
constructor(SchedulerAction, now = Scheduler.now) {
super(SchedulerAction, now);
this.actions = [];
/**
* A flag to indicate whether the Scheduler is currently executing a batch of
* queued actions.
* @type {boolean}
* @internal
*/
this._active = false;
/**
* An internal ID used to track the latest asynchronous task such as those
* coming from `setTimeout`, `setInterval`, `requestAnimationFrame`, and
* others.
* @type {any}
* @internal
*/
this._scheduled = undefined;
}
flush(action) {
const { actions } = this;
if (this._active) {
actions.push(action);
return;
}
let error;
this._active = true;
do {
if ((error = action.execute(action.state, action.delay))) {
break;
}
} while ((action = actions.shift())); // exhaust the scheduler queue
this._active = false;
if (error) {
while ((action = actions.shift())) {
action.unsubscribe();
}
throw error;
}
}
}