| 1 |
"use strict"; |
| 2 |
|
| 3 |
var callable = require("es5-ext/object/valid-callable") |
| 4 |
, isCallable = require("es5-ext/object/is-callable") |
| 5 |
, isValue = require("es5-ext/object/is-value") |
| 6 |
, d = require("d") |
| 7 |
, ee = require("event-emitter") |
| 8 |
, isPromise = require("./is-promise"); |
| 9 |
|
| 10 |
var create = Object.create, defineProperty = Object.defineProperty, deferred, resolve, reject; |
| 11 |
|
| 12 |
module.exports = exports = function (name, unres, onres, res) { |
| 13 |
name = String(name); |
| 14 |
callable(res); |
| 15 |
if (isValue(onres)) callable(onres); |
| 16 |
callable(unres); |
| 17 |
defineProperty(exports._unresolved, name, d(unres)); |
| 18 |
exports._onresolve[name] = onres; |
| 19 |
defineProperty(exports._resolved, name, d(res)); |
| 20 |
exports._names.push(name); |
| 21 |
}; |
| 22 |
|
| 23 |
exports._names = ["done", "then", "valueOf"]; |
| 24 |
|
| 25 |
exports._unresolved = ee( |
| 26 |
create(Function.prototype, { |
| 27 |
then: d(function (win, fail) { |
| 28 |
var def; |
| 29 |
if (!this.pending) this.pending = []; |
| 30 |
def = deferred(); |
| 31 |
this.pending.push("then", [win, fail, def.resolve, def.reject]); |
| 32 |
return def.promise; |
| 33 |
}), |
| 34 |
done: d(function (win, fail) { |
| 35 |
if (isValue(win)) callable(win); |
| 36 |
if (isValue(fail)) callable(fail); |
| 37 |
if (!this.pending) this.pending = []; |
| 38 |
this.pending.push("done", arguments); |
| 39 |
}), |
| 40 |
resolved: d(false), |
| 41 |
returnsPromise: d(true), |
| 42 |
valueOf: d(function () { return this; }) |
| 43 |
}) |
| 44 |
); |
| 45 |
|
| 46 |
exports._onresolve = { |
| 47 |
then: function (win, fail, promiseResolve, promiseReject) { |
| 48 |
var value, cont = this.failed ? fail : win; |
| 49 |
if (!isValue(cont)) { |
| 50 |
if (this.failed) promiseReject(this.value); |
| 51 |
else promiseResolve(this.value); |
| 52 |
return; |
| 53 |
} |
| 54 |
if (isCallable(cont)) { |
| 55 |
if (isPromise(cont)) { |
| 56 |
if (cont.resolved) { |
| 57 |
if (cont.failed) promiseReject(cont.value); |
| 58 |
else promiseResolve(cont.value); |
| 59 |
return; |
| 60 |
} |
| 61 |
cont.done(promiseResolve, promiseReject); |
| 62 |
return; |
| 63 |
} |
| 64 |
try { |
| 65 |
value = cont(this.value); |
| 66 |
} catch (e) { |
| 67 |
promiseReject(e); |
| 68 |
return; |
| 69 |
} |
| 70 |
promiseResolve(value); |
| 71 |
return; |
| 72 |
} |
| 73 |
promiseResolve(cont); |
| 74 |
}, |
| 75 |
done: function (win, fail) { |
| 76 |
if (this.failed) { |
| 77 |
if (fail) { |
| 78 |
fail(this.value); |
| 79 |
return; |
| 80 |
} |
| 81 |
throw this.value; |
| 82 |
} |
| 83 |
if (win) win(this.value); |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
exports._resolved = ee( |
| 88 |
create(Function.prototype, { |
| 89 |
then: d(function (win, fail) { |
| 90 |
var value, cont = this.failed ? fail : win; |
| 91 |
if (!isValue(cont)) return this; |
| 92 |
if (isCallable(cont)) { |
| 93 |
if (isPromise(cont)) return cont; |
| 94 |
try { value = cont(this.value); } |
| 95 |
catch (e) { return reject(e); } |
| 96 |
return resolve(value); |
| 97 |
} |
| 98 |
return resolve(cont); |
| 99 |
}), |
| 100 |
done: d(function (win, fail) { |
| 101 |
if (isValue(win)) callable(win); |
| 102 |
if (isValue(fail)) callable(fail); |
| 103 |
if (this.failed) { |
| 104 |
if (fail) { |
| 105 |
fail(this.value); |
| 106 |
return; |
| 107 |
} |
| 108 |
throw this.value; |
| 109 |
} |
| 110 |
if (win) win(this.value); |
| 111 |
}), |
| 112 |
resolved: d(true), |
| 113 |
returnsPromise: d(true), |
| 114 |
valueOf: d(function () { return this.value; }) |
| 115 |
}) |
| 116 |
); |
| 117 |
|
| 118 |
deferred = require("./deferred"); |
| 119 |
resolve = deferred.resolve; |
| 120 |
reject = deferred.reject; |
| 121 |
deferred.extend = exports; |