A library for interacting with SQLite databases
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

46 lines
796 B

export class TaskManager {
constructor() {
this._queue = [];
this._inTask = false;
}
enqueue() {
let completeSignal;
let taskComplete = new Promise((r, _) => {
completeSignal = r;
});
let readySignal;
let dbReady = new Promise((r, _) => {
readySignal = () => {
r(completeSignal);
};
});
this._queue.push({readySignal, taskComplete});
this._processQueue();
return dbReady;
}
async _processQueue() {
// We're already processing the queue
if (this._inTask) {
return;
}
while (true) {
let item = this._queue.shift();
if (!item) {
return;
}
this._inTask = true;
item.readySignal();
await item.taskComplete;
this._inTask = false;
}
}
}