-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-queue.js
More file actions
68 lines (58 loc) · 1.78 KB
/
Copy pathsync-queue.js
File metadata and controls
68 lines (58 loc) · 1.78 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
window.SuperTaskSyncQueue = (() => {
const STORAGE_KEY = "super-task-sync-queue";
function readQueue() {
try {
const raw = localStorage.getItem(STORAGE_KEY);
const parsed = raw ? JSON.parse(raw) : [];
return Array.isArray(parsed) ? parsed : [];
} catch (_) {
return [];
}
}
function writeQueue(queue) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(queue));
} catch (_) {
// Storage unavailable/full: queue is best-effort, not fatal.
}
}
// Queuing again for the same entity replaces the prior pending op
// (e.g. a queued delete supersedes an earlier queued upsert).
function enqueue(entityType, entityId, kind, payload) {
const queue = readQueue().filter(op => !(op.entityType === entityType && op.entityId === entityId));
queue.push({
opId: `${entityType}:${entityId}:${Date.now()}`,
entityType,
entityId,
kind,
payload,
attempts: 0,
queuedAt: Date.now()
});
writeQueue(queue);
return queue;
}
function removeOps(entityType, entityId) {
const queue = readQueue().filter(op => !(op.entityType === entityType && op.entityId === entityId));
writeQueue(queue);
return queue;
}
function updateOp(opId, changes) {
const queue = readQueue().map(op => (op.opId === opId ? { ...op, ...changes } : op));
writeQueue(queue);
return queue;
}
function getQueue() {
return readQueue();
}
function isPending(entityType, entityId) {
return readQueue().some(op => op.entityType === entityType && op.entityId === entityId);
}
function getPendingCount() {
return readQueue().length;
}
function clear() {
writeQueue([]);
}
return { enqueue, removeOps, updateOp, getQueue, isPending, getPendingCount, clear };
})();