Last active
October 6, 2017 16:23
-
-
Save Ni55aN/cd51621d9e839b1b195e51417d4f28ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fq = new FrameQueue((a,b,c) => { | |
console.log(a,b,c); | |
}); | |
fq.add(1,2,3); | |
fq.add('str'); | |
await fq.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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