code style

This commit is contained in:
Leo Horie 2014-09-01 11:29:53 -04:00
parent 18dcfce5e2
commit b70284d493

View file

@ -612,128 +612,120 @@ Mithril = m = new function app(window, undefined) {
return newPromisedProp(m.prop(), new Deferred()) return newPromisedProp(m.prop(), new Deferred())
} }
// Promiz.mithril.js | Zolmeister | MIT // Promiz.mithril.js | Zolmeister | MIT
function Deferred(fn, er) { function Deferred(successCallback, failureCallback) {
// states var RESOLVING = 1, REJECTING = 2, RESOLVED = 3, REJECTED = 4
// 0: pending var self = this, state = 0, promiseValue = 0, next = []
// 1: resolving
// 2: rejecting
// 3: resolved
// 4: rejected
var self = this,
state = 0,
val = 0,
next = [];
self['promise'] = self self["promise"] = self
self['resolve'] = function (v) { self["resolve"] = function(value) {
if (!state) { if (!state) {
val = v promiseValue = value
state = 1 state = RESOLVING
fire() fire()
} }
return this return this
} }
self['reject'] = function (v) { self["reject"] = function(value) {
if (!state) { if (!state) {
val = v promiseValue = value
state = 2 state = REJECTING
fire() fire()
} }
return this return this
} }
self['then'] = function (fn, er) { self["then"] = function(successCallback, failureCallback) {
var d = new Deferred(fn, er) var deferred = new Deferred(successCallback, failureCallback)
if (state == 3) { if (state == RESOLVED) {
d.resolve(val) deferred.resolve(promiseValue)
} }
else if (state == 4) { else if (state == REJECTED) {
d.reject(val) deferred.reject(promiseValue)
} }
else { else {
next.push(d) next.push(deferred)
} }
return d return deferred
} }
var finish = function (type) { function finish(type) {
state = type || 4 state = type || REJECTED
next.map(function (p) { next.map(function (p) {
state == 3 && p.resolve(val) || p.reject(val) state == RESOLVED && p.resolve(promiseValue) || p.reject(promiseValue)
}) })
} }
// ref : reference to 'then' function function thennable(then, successCallback, failureCallback, notThennableCallback) {
// cb, ec, cn : successCallback, failureCallback, notThennableCallback if ((typeof promiseValue == "object" || typeof promiseValue == "function") && typeof then == "function") {
function thennable (ref, cb, ec, cn) {
if ((typeof val == 'object' || typeof val == 'function') && typeof ref == 'function') {
try { try {
// count protects against abuse calls from spec checker
// cnt protects against abuse calls from spec checker var count = 0
var cnt = 0 then.call(promiseValue, function(value) {
ref.call(val, function (v) { if (count++) return
if (cnt++) return promiseValue = value
val = v successCallback()
cb() }, function (value) {
}, function (v) { if (count++) return
if (cnt++) return promiseValue = value
val = v failureCallback()
ec()
}) })
} catch (e) { }
val = e catch (e) {
ec() promiseValue = e
failureCallback()
} }
} else { } else {
cn() notThennableCallback()
} }
}; }
function fire() { function fire() {
// check if it's a thenable // check if it's a thenable
var ref; var then
try { try {
ref = val && val.then then = promiseValue && promiseValue.then
} catch (e) { }
val = e catch (e) {
state = 2 promiseValue = e
state = REJECTING
return fire() return fire()
} }
thennable(ref, function () { thennable(then, function() {
state = 1 state = RESOLVING
fire() fire()
}, function () { }, function() {
state = 2 state = REJECTING
fire() fire()
}, function () { }, function() {
try { try {
if (state == 1 && typeof fn == 'function') { if (state == RESOLVING && typeof successCallback == "function") {
val = fn(val) promiseValue = successCallback(promiseValue)
} }
else if (state == REJECTING && typeof failureCallback == "function") {
else if (state == 2 && typeof er == 'function') { promiseValue = failureCallback(promiseValue)
val = er(val) state = RESOLVING
state = 1
} }
} catch (e) { }
val = e catch (e) {
promiseValue = e
return finish() return finish()
} }
if (val == self) { if (promiseValue == self) {
val = TypeError() promiseValue = TypeError()
finish() finish()
} else thennable(ref, function () { }
finish(3) else {
thennable(then, function () {
finish(RESOLVED)
}, finish, function () { }, finish, function () {
finish(state == 1 && 3) finish(state == RESOLVING && RESOLVED)
}) })
}
}) })
} }
} }