-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync.js
54 lines (42 loc) · 1.15 KB
/
async.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
44
45
46
47
48
49
50
51
52
53
54
function AsyncResolvers(onFailedOne, onResolvedAll) {
this.onResolvedAll = onResolvedAll;
this.onFailedOne = onFailedOne;
this.resolvers = {};
this.resolversCount = 0;
this.passed = [];
this.failed = [];
this.firing = false;
}
AsyncResolvers.prototype = {
add: function(rule) {
var index = this.resolversCount;
this.resolvers[index] = rule;
this.resolversCount++;
return index;
},
resolve: function(index) {
var rule = this.resolvers[index];
if (rule.passes === true) {
this.passed.push(rule);
} else if (rule.passes === false) {
this.failed.push(rule);
this.onFailedOne(rule);
}
this.fire();
},
isAllResolved: function() {
return (this.passed.length + this.failed.length) === this.resolversCount;
},
fire: function() {
if (!this.firing) {
return;
}
if (this.isAllResolved()) {
this.onResolvedAll(this.failed.length === 0);
}
},
enableFiring: function() {
this.firing = true;
}
};
module.exports = AsyncResolvers;