Diffstat (limited to 'frontend/gamma/js/MochiKit/Async.js') (more/less context) (show whitespace changes)
-rw-r--r-- | frontend/gamma/js/MochiKit/Async.js | 118 |
1 files changed, 92 insertions, 26 deletions
diff --git a/frontend/gamma/js/MochiKit/Async.js b/frontend/gamma/js/MochiKit/Async.js index c7408e7..cc43835 100644 --- a/frontend/gamma/js/MochiKit/Async.js +++ b/frontend/gamma/js/MochiKit/Async.js @@ -10,3 +10,3 @@ See <http://mochikit.com/> for documentation, downloads, license, etc. -MochiKit.Base._module('Async', '1.5', ['Base']); +MochiKit.Base.module(MochiKit, 'Async', '1.5', ['Base']); @@ -22,2 +22,3 @@ MochiKit.Async.Deferred = function (/* optional */ canceller) { this.chained = false; + this.finalized = false; }; @@ -27,11 +28,3 @@ MochiKit.Async.Deferred.prototype = { repr: function () { - var state; - if (this.fired == -1) { - state = 'unfired'; - } else if (this.fired === 0) { - state = 'success'; - } else { - state = 'error'; - } - return 'Deferred(' + this.id + ', ' + state + ')'; + return 'Deferred(' + this.id + ', ' + this.state() + ')'; }, @@ -42,4 +35,15 @@ MochiKit.Async.Deferred.prototype = { + /** @id MochiKit.Async.Deferred.prototype.state */ + state: function () { + if (this.fired == -1) { + return 'unfired'; + } else if (this.fired === 0) { + return 'success'; + } else { + return 'error'; + } + }, + /** @id MochiKit.Async.Deferred.prototype.cancel */ - cancel: function () { + cancel: function (e) { var self = MochiKit.Async; @@ -52,6 +56,11 @@ MochiKit.Async.Deferred.prototype = { if (this.fired == -1) { - this.errback(new self.CancelledError(this)); + if (typeof(e) === 'string') { + e = new self.GenericError(e); + } else if (!(e instanceof Error)) { + e = new self.CancelledError(this); + } + this.errback(e); } } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) { - this.results[0].cancel(); + this.results[0].cancel(e); } @@ -67,3 +76,5 @@ MochiKit.Async.Deferred.prototype = { this.results[this.fired] = res; + if (this.paused === 0) { this._fire(); + } }, @@ -131,2 +142,5 @@ MochiKit.Async.Deferred.prototype = { } + if (this.finalized) { + throw new Error("Finalized Deferreds can not be re-used"); + } this.chain.push([cb, eb]); @@ -138,2 +152,20 @@ MochiKit.Async.Deferred.prototype = { + /** @id MochiKit.Async.Deferred.prototype.setFinalizer */ + setFinalizer: function (fn) { + if (this.chained) { + throw new Error("Chained Deferreds can not be re-used"); + } + if (this.finalized) { + throw new Error("Finalized Deferreds can not be re-used"); + } + if (arguments.length > 1) { + fn = MochiKit.Base.partial.apply(null, arguments); + } + this._finalizer = fn; + if (this.fired >= 0) { + this._fire(); + } + return this; + }, + _fire: function () { @@ -162,7 +194,4 @@ MochiKit.Async.Deferred.prototype = { cb = function (res) { - self._resback(res); self.paused--; - if ((self.paused === 0) && (self.fired >= 0)) { - self._fire(); - } + self._resback(res); }; @@ -180,2 +209,6 @@ MochiKit.Async.Deferred.prototype = { this.results[fired] = res; + if (this.chain.length == 0 && this.paused === 0 && this._finalizer) { + this.finalized = true; + this._finalizer(res); + } if (cb && this.paused) { @@ -251,3 +284,3 @@ MochiKit.Base.update(MochiKit.Async, { status = this.status; - if (!status && m.isNotEmpty(this.responseText)) { + if (!status && (this.response || m.isNotEmpty(this.responseText))) { // 0 or undefined seems to mean cached or local @@ -339,3 +372,4 @@ MochiKit.Base.update(MochiKit.Async, { headers: undefined, - mimeType: undefined + mimeType: undefined, + responseType: undefined */ @@ -373,2 +407,5 @@ MochiKit.Base.update(MochiKit.Async, { } + if ("responseType" in opts && "responseType" in req) { + req.responseType = opts.responseType; + } return self.sendXMLHttpRequest(req, opts.sendContent); @@ -406,2 +443,35 @@ MochiKit.Base.update(MochiKit.Async, { + /** @id MochiKit.Async.loadScript */ + loadScript: function (url) { + var d = new MochiKit.Async.Deferred(); + var script = document.createElement("script"); + script.type = "text/javascript"; + script.src = url; + script.onload = function () { + script.onload = null; + script.onerror = null; + script.onreadystatechange = null; + script = null; + d.callback(); + }; + script.onerror = function (msg) { + script.onload = null; + script.onerror = null; + script.onreadystatechange = null; + script = null; + msg = "Failed to load script at " + url + ": " + msg; + d.errback(new URIError(msg, url)); + } + script.onreadystatechange = function () { + if (script.readyState == "loaded" || script.readyState == "complete") { + script.onload(); + } else { + // IE doesn't bother to report errors... + MochiKit.Async.callLater(10, script.onerror, "Script loading timed out") + } + }; + document.getElementsByTagName("head")[0].appendChild(script); + return d; + }, + /** @id MochiKit.Async.wait */ @@ -409,9 +479,4 @@ MochiKit.Base.update(MochiKit.Async, { var d = new MochiKit.Async.Deferred(); - var m = MochiKit.Base; - if (typeof(value) != 'undefined') { - d.addCallback(function () { return value; }); - } - var timeout = setTimeout( - m.bind("callback", d), - Math.floor(seconds * 1000)); + var cb = MochiKit.Base.bind("callback", d, value); + var timeout = setTimeout(cb, Math.floor(seconds * 1000)); d.canceller = function () { @@ -512,2 +577,3 @@ MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, f MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred(); +MochiKit.Async.DeferredList.prototype.constructor = MochiKit.Async.DeferredList; |