Skip to content

Instantly share code, notes, and snippets.

@blasten
Last active January 26, 2017 20:49
Show Gist options
  • Save blasten/a1d66dc79e0b4d0ce4954dc5eb3431cf to your computer and use it in GitHub Desktop.
Save blasten/a1d66dc79e0b4d0ce4954dc5eb3431cf to your computer and use it in GitHub Desktop.
_queue = [];
_isRunning = false;
async _runLoop(task) {
let job, queue = this._queue;
if (this._isRunning) {
// Preempt the current tasks
while (job = queue[0]) {
queue.shift();
if (job.preemptive) {
job.run();
}
}
}
queue.push(task);
this._isRunning = true;
while (job = queue[0]) {
await job.time();
if (job === queue[0]) {
queue.shift();
job.run();
}
}
this._isRunning = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment