-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path2-keys.js
More file actions
64 lines (55 loc) · 1.56 KB
/
2-keys.js
File metadata and controls
64 lines (55 loc) · 1.56 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
'use strict';
const KeyCollector = function (keys, timeout, callback) {
this.expected = keys.length;
this.count = 0;
this.keys = keys;
this.data = {};
this.finished = false;
this.doneCallback = callback;
this.timer = setTimeout(() => {
if (this.finished) return;
const err = new Error('Collector timed out');
this.finished = true;
this.doneCallback(err);
}, timeout);
};
KeyCollector.prototype.collect = function (key, data) {
if (this.finished) return;
if (this.keys.includes(key)) {
if (Object.prototype.hasOwnProperty.call(this.data, key)) return;
this.count++;
if (data instanceof Error) {
this.finished = true;
this.doneCallback(data);
return;
}
this.data[key] = data;
if (this.expected === this.count) {
if (this.timer) clearTimeout(this.timer);
this.finished = true;
this.doneCallback(null, this.data);
}
}
};
// Usage
const kc1 = new KeyCollector(['key1', 'key2', 'key3'], 1000, (err, result) => {
console.log('kc1');
console.dir({ err, result });
});
kc1.collect('key1', 1);
kc1.collect('key2', 2);
kc1.collect('key3', 3);
const kc2 = new KeyCollector(['key1', 'key2', 'key3'], 1000, (err, result) => {
console.log('kc2');
console.dir({ err, result });
});
kc2.collect('key1', 1);
kc2.collect('key2', 2);
kc2.collect('key4', 4);
const kc3 = new KeyCollector(['key1', 'key2', 'key3'], 1000, (err, result) => {
console.log('kc3');
console.dir({ err, result });
});
kc3.collect('key1', new Error('Collect an error'));
kc3.collect('key2', 2);
kc3.collect('key3', 3);