-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.js
43 lines (38 loc) · 992 Bytes
/
utils.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
40
41
42
43
const deasync = require('deasync');
Array.prototype.removeOneByValue = function(v) {
for (let i = this.length - 1; i >= 0; i--)
if (this[i] === v)
return this.splice(i, 1);
return this;
};
const Barrier = function() {
this.counter = 0;
};
const DeasyncObject = (object, synchronousByNature = []) => {
for (const m of Object.keys(object.prototype)) {
if (!synchronousByNature[m])
object.prototype[`${m}S`] = deasync(object.prototype[m]);
}
};
Barrier.prototype = {
tick() {
this.counter++;
return (err) => {
this.counter--;
if (err) this.err = err;
if (this.counter === 0 && this.endCallback) this.endCallback(err);
};
},
wait(endcb) {
this.endCallback = endcb;
if (this.counter === 0) endcb(this.err);
},
clear() {
this.endCallback = null;
this.err = null;
},
};
module.exports = {
Barrier,
DeasyncObject,
};