From bf288b09c56635b65eb626463516eeba5819b9a8 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Sat, 16 Aug 2014 16:50:44 -0400 Subject: [PATCH] move Deferred constructor out of m.deferred --- mithril.js | 256 ++++++++++++++++++++++++++--------------------------- 1 file changed, 127 insertions(+), 129 deletions(-) diff --git a/mithril.js b/mithril.js index 688a2c7b..baff05a8 100644 --- a/mithril.js +++ b/mithril.js @@ -563,7 +563,6 @@ Mithril = m = new function app(window, undefined) { cellCache[cacheKey] = undefined } - var none = {} function newPromisedProp(prop, promise) { prop.then = function () { var newProp = m.prop() @@ -584,136 +583,135 @@ Mithril = m = new function app(window, undefined) { return prop } m.deferred = function () { - - // Promiz.mithril.js | Zolmeister | MIT - function Deferred(fn, er) { - // states - // 0: pending - // 1: resolving - // 2: rejecting - // 3: resolved - // 4: rejected - var self = this, - state = 0, - val = 0, - next = []; - - self['promise'] = self - - self['resolve'] = function (v) { - if (!state) { - val = v - state = 1 - - fire() - } - return this - } - - self['reject'] = function (v) { - if (!state) { - val = v - state = 2 - - fire() - } - return this - } - - self['then'] = function (fn, er) { - var d = new Deferred(fn, er) - if (state == 3) { - d.resolve(val) - } - else if (state == 4) { - d.reject(val) - } - else { - next.push(d) - } - return d - } - - var finish = function (type) { - state = type || 4 - next.map(function (p) { - state == 3 && p.resolve(val) || p.reject(val) - }) - } - - // ref : reference to 'then' function - // cb, ec, cn : successCallback, failureCallback, notThennableCallback - function thennable (ref, cb, ec, cn) { - if ((typeof val == 'object' || typeof val == 'function') && typeof ref == 'function') { - try { - - // cnt protects against abuse calls from spec checker - var cnt = 0 - ref.call(val, function (v) { - if (cnt++) return - val = v - cb() - }, function (v) { - if (cnt++) return - val = v - ec() - }) - } catch (e) { - val = e - ec() - } - } else { - cn() - } - }; - - function fire() { - - // check if it's a thenable - var ref; - try { - ref = val && val.then - } catch (e) { - val = e - state = 2 - return fire() - } - thennable(ref, function () { - state = 1 - fire() - }, function () { - state = 2 - fire() - }, function () { - try { - if (state == 1 && typeof fn == 'function') { - val = fn(val) - } - - else if (state == 2 && typeof er == 'function') { - val = er(val) - state = 1 - } - } catch (e) { - val = e - return finish() - } - - if (val == self) { - val = TypeError() - finish() - } else thennable(ref, function () { - finish(3) - }, finish, function () { - finish(state == 1 && 3) - }) - - }) - } - } - return newPromisedProp(m.prop(), new Deferred()) } + // Promiz.mithril.js | Zolmeister | MIT + function Deferred(fn, er) { + // states + // 0: pending + // 1: resolving + // 2: rejecting + // 3: resolved + // 4: rejected + var self = this, + state = 0, + val = 0, + next = []; + + self['promise'] = self + + self['resolve'] = function (v) { + if (!state) { + val = v + state = 1 + + fire() + } + return this + } + + self['reject'] = function (v) { + if (!state) { + val = v + state = 2 + + fire() + } + return this + } + + self['then'] = function (fn, er) { + var d = new Deferred(fn, er) + if (state == 3) { + d.resolve(val) + } + else if (state == 4) { + d.reject(val) + } + else { + next.push(d) + } + return d + } + + var finish = function (type) { + state = type || 4 + next.map(function (p) { + state == 3 && p.resolve(val) || p.reject(val) + }) + } + + // ref : reference to 'then' function + // cb, ec, cn : successCallback, failureCallback, notThennableCallback + function thennable (ref, cb, ec, cn) { + if ((typeof val == 'object' || typeof val == 'function') && typeof ref == 'function') { + try { + + // cnt protects against abuse calls from spec checker + var cnt = 0 + ref.call(val, function (v) { + if (cnt++) return + val = v + cb() + }, function (v) { + if (cnt++) return + val = v + ec() + }) + } catch (e) { + val = e + ec() + } + } else { + cn() + } + }; + + function fire() { + + // check if it's a thenable + var ref; + try { + ref = val && val.then + } catch (e) { + val = e + state = 2 + return fire() + } + thennable(ref, function () { + state = 1 + fire() + }, function () { + state = 2 + fire() + }, function () { + try { + if (state == 1 && typeof fn == 'function') { + val = fn(val) + } + + else if (state == 2 && typeof er == 'function') { + val = er(val) + state = 1 + } + } catch (e) { + val = e + return finish() + } + + if (val == self) { + val = TypeError() + finish() + } else thennable(ref, function () { + finish(3) + }, finish, function () { + finish(state == 1 && 3) + }) + + }) + } + } + m.sync = function(args) { var method = "resolve" function synchronizer(pos, resolved) {