-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path4-private.js
More file actions
48 lines (38 loc) · 859 Bytes
/
4-private.js
File metadata and controls
48 lines (38 loc) · 859 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
class KeyCollector {
#finished = false;
#data = {};
#keys = [];
#count = 0;
#done = null;
constructor(keys) {
this.#keys = keys;
}
set(key, value) {
if (this.#finished) return;
const expected = this.#keys.includes(key);
const has = this.#data[key] !== undefined;
if (!has && expected) this.#count++;
this.#data[key] = value;
if (this.#count === this.#keys.length) {
this.#finished = true;
if (this.#done) this.#done(this.#data);
}
}
then(resolve) {
this.#done = resolve;
}
}
// Usage
const main = async () => {
const ac = new KeyCollector(['fileName', 'userName']);
setTimeout(() => {
ac.set('fileName', 'marcus.txt');
}, 100);
setTimeout(() => {
ac.set('userName', 'Marcus');
}, 200);
const result = await ac;
console.log(result);
};
main();