-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathqueue.js
More file actions
32 lines (27 loc) · 806 Bytes
/
Copy pathqueue.js
File metadata and controls
32 lines (27 loc) · 806 Bytes
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
import {that} from "../that.js";
export function queue(initialize) {
let resolve;
const queue = [];
const dispose = initialize(push);
if (dispose != null && typeof dispose !== "function") {
throw new Error(typeof dispose.then === "function"
? "async initializers are not supported"
: "initializer returned something, but not a dispose function");
}
function push(x) {
queue.push(x);
if (resolve) resolve(queue.shift()), resolve = null;
return x;
}
function next() {
return {done: false, value: queue.length
? Promise.resolve(queue.shift())
: new Promise(_ => (resolve = _))};
}
return {
[Symbol.iterator]: that,
throw: () => ({done: true}),
return: () => (dispose != null && dispose(), {done: true}),
next
};
}