Created
July 16, 2017 17:31
-
-
Save vegarringdal/3f21b63e8453262a53ec1dc393595485 to your computer and use it in GitHub Desktop.
queue
This file contains hidden or 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
| @vegar ringdal <vegar.ringdal@gmail.com> | |
| export class Queue { | |
| private tasks: any[] = []; | |
| public add(microtask: boolean): Promise<Function> { | |
| return new Promise((resolve) => { | |
| if (microtask) { | |
| this.tasks.splice(1, 0, resolve); | |
| } else { | |
| this.tasks.push(resolve); | |
| } | |
| if (this.tasks.length === 1) { | |
| let task = this.tasks[0]; | |
| let finishedCallback: Function; | |
| finishedCallback = () => { | |
| requestAnimationFrame(() => { | |
| let old = this.tasks.shift(); | |
| old = null; | |
| let nextTask = this.tasks[0]; | |
| if (nextTask) { | |
| nextTask(finishedCallback); | |
| } | |
| }); | |
| }; | |
| requestAnimationFrame(() => { | |
| task(finishedCallback); | |
| }); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment