-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra.js
More file actions
152 lines (136 loc) · 3.16 KB
/
extra.js
File metadata and controls
152 lines (136 loc) · 3.16 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Represents a scheduled task.
*/
class Schedule {
constructor(schedule){
this.schedule = schedule;
}
/**
* Returns the schedule ID.
* @returns {number}
*/
get id(){
return this.schedule;
}
/**
* Clear a scheduled task.
* @returns {boolean}
*/
delete(){
clearInterval(this.schedule);
return true;
}
}
class Queue {
constructor (){
this.queue = [];
this.standby = [];
this.running = false;
}
/**
* Get queue length.
* @returns {number}
*/
get length() {
return this.queue.length;
}
/**
* Queue a task to run sequentially.
*
* For example:
*
* ```
* const queue = new Queue();
* queue.addTask(async () => {
* const response = await fetch(...);
* return response.ok ? "ok" : "error";
* });
* ```
*
* @param {() => Promise<void>} task
*/
addTask(task){
this.queue.push(task);
return this;
}
/**
* Queue an array of tasks to run sequentially.
*
* @param {(() => Promise<void>)[]} tasks
*/
addTasks(tasks){
for (const task of tasks){
this.queue.push(task);
}
return this;
}
/**
* Put a task on standby for later processing.
* @param {() => Promise<void>} task
* @returns
*/
addStandby(task){
this.standby.push(task);
return this;
}
/**
* Approve one task on standby. This method pushes the standby task to the main queue.
* @param {number} index
* @returns
*/
approveTaskById(index = 0){
if (this.standby.length === 0) return;
if (index < 0 || index >= this.standby.length) return;
const task = this.standby[index];
this.queue.push(task);
console.log("Standby task pushed to main queue...");
return this;
}
/**
* Approve all tasks on standby. This method pushes the standby tasks to the main queue.
* @returns
*/
approveAllTasks(){
if (this.standby.length === 0) return;
this.queue.push(...this.standby);
console.log("All standby tasks pushed to main queue...");
return this;
}
/**
* Remove a standby task by index.
* @param {number} index
* @returns
*/
clearStandby(index = 0){
if (this.standby.length === 0) return;
if (index < 0 || index >= this.standby.length) return;
this.standby.splice(index, 1)[0];
console.log(`Standby task ${index} removed...`);
return this;
}
/**
* Remove all standby tasks.
* @returns
*/
clearAllStandby(){
this.standby = [];
return this;
}
/**
* Start running tasks sequentially.
*/
async processQueue(){
if (this.running || this.queue.length === 0) return;
this.running = true;
while (this.queue.length > 0){
const task = this.queue[0];
await task();
this.queue.shift();
}
this.running = false;
}
}
export {
Schedule,
Queue
}