@tldraw/utils
Version:
tldraw infinite canvas SDK (private utilities).
40 lines (39 loc) • 831 B
JavaScript
import { sleep } from "./control.mjs";
class ExecutionQueue {
constructor(timeout) {
this.timeout = timeout;
}
queue = [];
running = false;
isEmpty() {
return this.queue.length === 0 && !this.running;
}
async run() {
if (this.running) return;
try {
this.running = true;
while (this.queue.length) {
const task = this.queue.shift();
await task();
if (this.timeout) {
await sleep(this.timeout);
}
}
} finally {
this.running = false;
}
}
async push(task) {
return new Promise((resolve, reject) => {
this.queue.push(() => Promise.resolve(task()).then(resolve).catch(reject));
this.run();
});
}
close() {
this.queue = [];
}
}
export {
ExecutionQueue
};
//# sourceMappingURL=ExecutionQueue.mjs.map