-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
138 lines (129 loc) · 3.33 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
([], function(){
"use strict";
// based on https://github.com/MaxMotovilov/node-promise/blob/master/promise.js
function NotRequiredError(){}
NotRequiredError.prototype = {
toString: function(){ return "[Error: not required]" }
}
function cancel(array, why, index){
array.forEach(function(p, i){
if(i !== index && p && typeof p.then == "function" &&
typeof p.cancel == "function"){
p.cancel(why, function(err){ throw err; });
}
});
}
function countToDos(array){
var todo = 0;
for(var i = 0; i < array.length; ++i) {
var p = array[i];
if(p && typeof p.then == "function"){
++todo;
}
}
return todo;
}
function All(array, failOnError){
this.array = array;
this.todo = countToDos(this.array);
this.once = true;
this.cancelled = false;
this.failed = failOnError ? this.failOnce : this.succeed;
this.allResolved = this.array;
}
All.prototype = {
succeed: function(index, resolve){
return function(value){
this.array[index] = value;
if(!--this.todo){
resolve(this.array);
}
return value;
}.bind(this);
},
failOnce: function(index, _, reject){
return function(error){
if(this.once){
this.once = false;
cancel(this.array, error, index);
if(!this.cancelled){
reject(error);
}
}
return false;
}.bind(this);
},
canceler: function(why){
this.cancelled = true;
cancel(this.array, why);
}
};
function Any(array, firstFailureConclusive){
this.array = array;
this.todo = countToDos(this.array);
this.resolved = false;
this.firstFailureConclusive = firstFailureConclusive;
if(this.todo && this.firstFailureConclusive){
this.todo = 1;
}
this.allResolved = this.array[0];
}
Any.prototype = {
succeed: function(index, resolve){
return function(value){
delete this.array[index];
if(!this.resolved) {
this.resolved = true;
resolve(value);
cancel(this.array, new NotRequiredError(), index);
}
}.bind(this);
},
failed: function(index, _, reject){
return function(err){
delete this.array[index];
if(!this.resolved && !--this.todo){
this.resolved = true;
cancel(this.array, this.firstFailureConclusive ? err : new NotRequiredError(), index);
reject(err);
}
return false;
}.bind(this);
},
canceler: function(why){
this.resolved = true;
cancel(this.array, why);
}
};
function instrument(Type, flag){
return function(array, Deferred){
if(!(array instanceof Array)){
array = Array.prototype.slice.call(arguments, 0);
Deferred = null;
}
var P = Deferred && Deferred.Wrapper || Promise,
type = new Type(array, flag);
return new P(function(resolve, reject, cancel){
if(type.todo){
array.forEach(function(p, i){
if(p && typeof p.then == "function"){
p.then(type.succeed(i, resolve, reject), type.failed(i, resolve, reject));
}
});
}else{
resolve(type.allResolved);
}
typeof cancel == "function" && cancel(type.canceler.bind(type));
});
}
}
var race = instrument(Any, true);
return {
all: instrument(All, true),
par: instrument(All, false),
any: instrument(Any, false),
one: race,
race: race
};
});