This repository was archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAsyncTaskQueue.js
More file actions
162 lines (139 loc) · 3.43 KB
/
AsyncTaskQueue.js
File metadata and controls
162 lines (139 loc) · 3.43 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
153
154
155
156
157
158
159
160
161
162
import { getDeferred, onIdle, onAnimationFrame, onTimeout, callAsAsync } from './promises.js';
import { isAborted } from './abort.js';
const symbols = {
resolve: Symbol('resolve'),
set: Symbol('set'),
reject: Symbol('reject'),
};
export class AsyncTaskQueue {
constructor(...callbacks) {
Object.defineProperty(this, symbols.set, {
configurable: false,
enumerable: false,
writable: false,
value: new Set(),
});
if (callbacks.length !== 0) {
this.add(...callbacks);
}
}
get size() {
return this[symbols.set].size;
}
get empty() {
return this.size === 0;
}
get waiting() {
return this.hasOwnProperty(symbols.resolve);
}
clear() {
if (this.waiting) {
this[symbols.reject]('Task queue cleared');
delete this[symbols.resolve];
delete this[symbols.reject];
}
return this[symbols.set].clear();
}
forEach(callback, thisArg) {
return this[symbols.set].forEach(callback, thisArg);
}
values() {
return this[symbols.set].values();
}
keys() {
return this[symbols.set].keys();
}
entries() {
return this[symbols.set].entries();
}
[Symbol.iterator]() {
return this.values();
}
add(...callbacks) {
callbacks.forEach(callback => {
if (! (callback instanceof Function)) {
throw new TypeError('Callback must be a function');
} else if (this.waiting) {
const resolve = this[symbols.resolve];
resolve(callback);
delete this[symbols.resolve];
delete this[symbols.reject];
} else {
this[symbols.set].add(callback);
}
});
}
has(...callbacks) {
return callbacks.every(callback => this[symbols.set].has(callback));
}
delete(callback) {
return this[symbols.set].delete(callback);
}
async execute({ signal, thisArg = globalThis } = {}) {
for (const callback of this) {
if (! isAborted(signal) &&callback instanceof Function) {
this.delete(callback);
await callAsAsync(callback, [], { signal, thisArg });
}
}
}
async executeOnIdle({ signal, thisArg = globalThis, timeout } = {}) {
for (const callback of this) {
if (! isAborted(signal) &&callback instanceof Function) {
this.delete(callback);
await onIdle(callback, { signal, thisArg, timeout });
}
}
}
async executeOnAnimationFrame({ signal, thisArg = globalThis } = {}) {
for (const callback of this) {
if (! isAborted(signal) &&callback instanceof Function) {
this.delete(callback);
await onAnimationFrame(callback, { signal, thisArg });
}
}
}
async executeOnTimeout({ timeout = 0, signal, thisArg = globalThis } = {}) {
for (const callback of this) {
if (! isAborted(signal) &&callback instanceof Function) {
this.delete(callback);
await onTimeout(callback, { timeout, signal, thisArg });
}
}
}
async *getQueue({ signal } = {}) {
while(! isAborted(signal)) {
if (this.empty) {
const { promise, resolve, reject } = getDeferred({ signal });
Object.defineProperty(this, symbols.resolve, {
configurable: true,
writable: false,
enumerable: false,
value: resolve,
});
Object.defineProperty(this, symbols.reject, {
configurable: true,
writable: false,
enumerable: false,
value: reject,
});
try {
const callback = await promise;
yield callback;
} catch(err) {
console.error(err);
return;
}
} else {
for (const callback of this) {
if (isAborted(signal)) {
return;
} else {
this.delete(callback);
yield callback;
}
}
}
}
}
}