Skip to content

Instantly share code, notes, and snippets.

@vegarringdal
Created July 16, 2017 17:31
Show Gist options
  • Select an option

  • Save vegarringdal/3f21b63e8453262a53ec1dc393595485 to your computer and use it in GitHub Desktop.

Select an option

Save vegarringdal/3f21b63e8453262a53ec1dc393595485 to your computer and use it in GitHub Desktop.
queue
@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