-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.28 KB
/
index.js
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
import "zero-timeout";
export function init() {
const tasks = [];
let taskId = 0;
let queueIsRunning = false;
return {
enqueueTask,
cancelTask,
sortTasks,
countTasks: () => tasks.length,
};
function enqueueTask(newTask) {
const defaultPriority = () => 0;
taskId += 1;
tasks.push({
id: taskId,
getPriority: newTask.getPriority || defaultPriority,
chunks: newTask.chunks,
});
if (!queueIsRunning) window.setZeroTimeout(runTaskQueue);
return taskId;
}
function cancelTask(id) {
const task = tasks.find(task => task.id === id);
if (task) task.canceled = true;
}
function sortTasks() {
tasks.sort( (a, b) => compareNums(a.getPriority(), b.getPriority()) );
}
function compareNums(a, b) {
if (a === b) return 0;
return (a === undefined || a < b) ? -1 : 1;
}
function runTaskQueue() {
// Remove canceled and completed tasks
while (isDone(tasks[0])) tasks.shift();
queueIsRunning = (tasks.length > 0);
if (!queueIsRunning) return;
// Get the next chunk from the current task, and run it
const chunk = tasks[0].chunks.shift();
chunk();
window.setZeroTimeout(runTaskQueue);
}
function isDone(task) {
return task && (task.canceled || task.chunks.length < 1);
}
}