-
Notifications
You must be signed in to change notification settings - Fork 1
/
davy.js
301 lines (298 loc) · 8 KB
/
davy.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
(function(root) {
"use strict";
var nextTick;
if (typeof define === "function" && define.amd) {
define([ "subsequent" ], function(subsequent) {
nextTick = subsequent;
return Promise;
});
} else if (typeof module === "object" && module.exports) {
module.exports = Promise;
nextTick = require("subsequent");
} else {
root.Davy = Promise;
nextTick = root.subsequent;
}
function Promise(fn) {
this.value = undefined;
this.__deferreds__ = [];
if (arguments.length > 0) {
if (isFunction(fn)) {
Resolver.resolve(this, fn);
} else {
throw new TypeError("Promise constructor's argument is not a function");
}
}
}
Promise.prototype.isFulfilled = false;
Promise.prototype.isRejected = false;
Promise.prototype.then = function(onFulfilled, onRejected) {
var promise = new Promise(), deferred = new Deferred(promise, onFulfilled, onRejected);
if (this.isFulfilled || this.isRejected) {
Resolver.handle(this, deferred);
} else {
this.__deferreds__.push(deferred);
}
return promise;
};
function Deferred(promise, onFulfilled, onRejected) {
return {
fulfill: onFulfilled,
reject: onRejected,
promise: promise
};
}
function Resolver(promise) {
this.promise = promise;
}
Resolver.prototype.fulfill = function(value) {
Resolver.fulfill(this.promise, value);
};
Resolver.prototype.reject = function(error) {
Resolver.reject(this.promise, error);
};
Resolver.SUCCESS = "fulfill";
Resolver.FAILURE = "reject";
Resolver.fulfill = function(promise, value) {
if (promise.isFulfilled || promise.isRejected) return;
if (value === promise) {
Resolver.reject(promise, new TypeError("Can't resolve a promise with itself."));
return;
}
if (isObject(value) || isFunction(value)) {
var then;
try {
then = value.then;
} catch (e) {
Resolver.reject(promise, e);
return;
}
if (isFunction(then)) {
Resolver.resolve(promise, then.bind(value));
return;
}
}
promise.isFulfilled = true;
Resolver.complete(promise, value);
};
Resolver.reject = function(promise, error) {
if (promise.isFulfilled || promise.isRejected) return;
promise.isRejected = true;
Resolver.complete(promise, error);
};
Resolver.complete = function(promise, value) {
promise.value = value;
var deferreds = promise.__deferreds__;
if (!deferreds.length) return;
var i = 0;
while (i < deferreds.length) {
Resolver.handle(promise, deferreds[i++]);
}
promise.__deferreds__ = undefined;
};
Resolver.handle = function(promise, deferred) {
var type = promise.isFulfilled ? Resolver.SUCCESS : Resolver.FAILURE, fn = deferred[type], value = promise.value;
promise = deferred.promise;
nextTick(function() {
if (isFunction(fn)) {
var val;
try {
val = fn(value);
} catch (e) {
Resolver.reject(promise, e);
return;
}
Resolver.fulfill(promise, val);
} else {
Resolver[type](promise, value);
}
});
};
Resolver.resolve = function(promise, fn) {
var isPending = true;
try {
fn(function(val) {
if (isPending) {
isPending = false;
Resolver.fulfill(promise, val);
}
}, function(err) {
if (isPending) {
isPending = false;
Resolver.reject(promise, err);
}
});
} catch (e) {
if (isPending) {
Resolver.reject(promise, e);
}
}
};
Promise.prototype.progress = function(onProgress) {
return this.then(null, null, onProgress);
};
Promise.prototype["catch"] = function(onRejected) {
return this.then(null, onRejected);
};
Promise.prototype["throw"] = function() {
return this["catch"](function(error) {
nextTick(function() {
throw error;
});
});
};
Promise.prototype["finally"] = function(onResolved) {
return this.then(onResolved, onResolved);
};
Promise.prototype["yield"] = function(value) {
return this.then(function() {
return value;
});
};
Promise.prototype.tap = function(onFulfilled) {
return this.then(onFulfilled)["yield"](this);
};
Promise.prototype.spread = function(onFulfilled, onRejected) {
return this.then(function(val) {
return onFulfilled.apply(this, val);
}, onRejected);
};
Promise.resolve = Promise.cast = function(val) {
if (isLikePromise(val)) {
return val;
}
return new Promise(val);
};
Promise.reject = function(err) {
var resolver = Promise.defer();
resolver.reject(err);
return resolver.promise;
};
Promise.defer = function() {
return new Resolver(new Promise());
};
Promise.each = function(list, iterator) {
var resolver = Promise.defer(), len = list.length;
if (len === 0) resolver.fulfill(TypeError());
var i = 0;
while (i < len) {
iterator(list[i], i++, list);
}
return resolver;
};
Promise.all = function() {
var list = parse(arguments), length = list.length, resolver = Promise.each(list, resolve);
return resolver.promise;
function reject(err) {
resolver.reject(err);
}
function resolve(value, i, list) {
if (isLikePromise(value)) {
value.then(function(val) {
resolve(val, i, list);
}, reject);
return;
}
list[i] = value;
if (--length === 0) {
resolver.fulfill(list);
}
}
};
Promise.race = function() {
var list = parse(arguments), resolver = Promise.each(list, resolve);
return resolver.promise;
function reject(err) {
resolver.reject(err);
}
function resolve(value) {
if (isLikePromise(value)) {
value.then(resolve, reject);
return;
}
resolver.fulfill(value);
}
};
Promise.wrap = function(fn) {
var resolver = Promise.defer();
function callback(err, val) {
if (err) {
resolver.reject(err);
} else {
resolver.fulfill(val);
}
}
return function() {
var len = arguments.length, args = new Array(len), i = 0;
while (i < len) {
args[i] = arguments[i++];
}
try {
switch (len) {
case 2:
fn.call(this, args[0], args[1], callback);
break;
case 1:
fn.call(this, args[0], callback);
break;
case 0:
fn.call(this, callback);
break;
default:
args.push(callback);
fn.apply(this, args);
}
} catch (e) {
resolver.reject(e);
}
return resolver.promise;
};
};
Promise.traverse = function(tree, path) {
function visit(node, depth) {
return Promise.resolve(node).then(function(node) {
if (!isObject(node) || isEmpty(node)) {
return node;
}
var isArray = Array.isArray(node), result = isArray ? [] : {}, promises = Object.keys(node).map(function(key) {
if (path && path[depth] !== key) {
return Promise.resolve();
}
var value = node[key];
if (isArray) {
key = result.length;
}
result[key] = null;
return visit(value, depth + 1).then(function(unwrapped) {
result[key] = unwrapped;
});
});
return Promise.all(promises).yield(result);
});
}
return visit(tree, 0);
};
function isObject(obj) {
return obj && typeof obj === "object";
}
function isFunction(fn) {
return fn && typeof fn === "function";
}
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
function isLikePromise(obj) {
return isObject(obj) && isFunction(obj.then);
}
function parse(obj) {
if (obj.length === 1 && Array.isArray(obj[0])) {
return obj[0];
} else {
var args = new Array(obj.length), i = 0;
while (i < args.length) {
args[i] = obj[i++];
}
return args;
}
}
})(Function("return this")());