Skip to content

Instantly share code, notes, and snippets.

@Ni55aN
Last active October 6, 2017 16:23
Show Gist options
  • Save Ni55aN/cd51621d9e839b1b195e51417d4f28ce to your computer and use it in GitHub Desktop.
Save Ni55aN/cd51621d9e839b1b195e51417d4f28ce to your computer and use it in GitHub Desktop.
var fq = new FrameQueue((a,b,c) => {
console.log(a,b,c);
});
fq.add(1,2,3);
fq.add('str');
await fq.run();
export class FrameQueue {
constructor(method) {
this.queue = [];
this.method = method;
this.ondone = null;
}
add() {
this.queue.push(this._f.apply(this, arguments));
}
_f() {
var args = arguments;
var self = this;
return function () {
self.method(...args);
var next = self.queue.shift();
if (typeof next === 'function') {
requestAnimationFrame(next);
}
else
self.ondone();
}
}
async run() {
var self = this;
return new Promise(res => {
self.ondone = res;
if (self.queue.length === 0)
throw new Error('Queue have no elements');
else
self.queue.shift()();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment