-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.js
39 lines (33 loc) · 830 Bytes
/
cache.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
export default async function* cache(array, status) {
const values = [];
let done = defer();
void async function () {
if (typeof array === 'function') {
array = array();
}
for await (const item of array) {
//console.log('Caching', item);
values.push(item);
status.overhead = values.length;
done.resolve(false);
done = defer();
}
done.resolve(true);
}()
while (values.length > 0 || !await done.promise) {
if (values.length > 0) {
//console.log('Retrieving', values[0]);
status.overhead = values.length - 1;
yield values.shift();
}
}
}
function defer() {
let resolve;
let reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
return { resolve, reject, promise };
}