Pure Javascript OCR for more than 100 Languages 📖🎉🖥
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.

58 lines
1.2 KiB

module.exports = () => {
const workers = {};
6 years ago
const runningWorkers = {};
let jobQueue = [];
const dequeue = () => {
if (jobQueue.length !== 0) {
const wIds = Object.keys(workers);
for (let i = 0; i < wIds.length; i += 1) {
6 years ago
if (typeof runningWorkers[wIds[i]] === 'undefined') {
jobQueue[0](workers[wIds[i]]);
break;
}
}
}
};
6 years ago
const queue = (action, payload) => (
new Promise((resolve, reject) => {
6 years ago
jobQueue.push(async (w) => {
jobQueue.shift();
6 years ago
runningWorkers[w.id] = true;
try {
resolve(await w[action].apply(this, payload));
} catch (err) {
reject(err);
} finally {
delete runningWorkers[w.id];
dequeue();
6 years ago
}
});
dequeue();
})
);
const addWorker = (w) => {
workers[w.id] = w;
return w.id;
};
6 years ago
const addJob = (action, ...payload) => (
queue(action, payload)
);
6 years ago
const terminate = async () => {
Object.keys(workers).forEach(async (id) => {
await workers[id].terminate();
});
jobQueue = [];
};
return {
addWorker,
addJob,
terminate,
};
};