-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.js
More file actions
48 lines (41 loc) · 1.06 KB
/
queue.js
File metadata and controls
48 lines (41 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { getJob } from "./store.js";
const jobQueue = [];
let isRunning = false;
// Handles enqueue job state in core application logic.
export function enqueueJob(jobId, fn) {
jobQueue.push({ jobId, fn });
runQueue();
}
// Returns queued job state ids used for core application logic.
export function getQueuedJobIds() {
return jobQueue.map(j => j.jobId);
}
// Removes from queue from core application logic.
export function removeFromQueue(jobId) {
for (let i = jobQueue.length - 1; i >= 0; i--) {
if (jobQueue[i].jobId === jobId) {
jobQueue.splice(i, 1);
}
}
}
// Runs queue for core application logic.
async function runQueue() {
if (isRunning) return;
isRunning = true;
try {
while (jobQueue.length > 0) {
const { jobId, fn } = jobQueue.shift();
const job = getJob(jobId);
if (!job || job.status === "canceled" || job.canceled) {
continue;
}
try {
await fn();
} catch (err) {
console.error(`[queue] Job ${jobId} hata:`, err);
}
}
} finally {
isRunning = false;
}
}