summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/MochiKit/Async.js
Side-by-side diff
Diffstat (limited to 'frontend/gamma/js/MochiKit/Async.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/js/MochiKit/Async.js120
1 files changed, 93 insertions, 27 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
@@ -9,5 +9,5 @@ See <http://mochikit.com/> for documentation, downloads, license, etc.
***/
-MochiKit.Base._module('Async', '1.5', ['Base']);
+MochiKit.Base.module(MochiKit, 'Async', '1.5', ['Base']);
/** @id MochiKit.Async.Deferred */
@@ -21,4 +21,5 @@ MochiKit.Async.Deferred = function (/* optional */ canceller) {
this.silentlyCancelled = false;
this.chained = false;
+ this.finalized = false;
};
@@ -26,13 +27,5 @@ MochiKit.Async.Deferred.prototype = {
/** @id MochiKit.Async.Deferred.prototype.repr */
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() + ')';
},
@@ -41,6 +34,17 @@ MochiKit.Async.Deferred.prototype = {
_nextId: MochiKit.Base.counter(),
+ /** @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;
if (this.fired == -1) {
@@ -51,8 +55,13 @@ 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);
}
},
@@ -66,5 +75,7 @@ MochiKit.Async.Deferred.prototype = {
this.fired = ((res instanceof Error) ? 1 : 0);
this.results[this.fired] = res;
- this._fire();
+ if (this.paused === 0) {
+ this._fire();
+ }
},
@@ -130,4 +141,7 @@ MochiKit.Async.Deferred.prototype = {
throw new Error("Chained Deferreds can not be re-used");
}
+ if (this.finalized) {
+ throw new Error("Finalized Deferreds can not be re-used");
+ }
this.chain.push([cb, eb]);
if (this.fired >= 0) {
@@ -137,4 +151,22 @@ 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 () {
/***
@@ -161,9 +193,6 @@ MochiKit.Async.Deferred.prototype = {
if (res instanceof MochiKit.Async.Deferred) {
cb = function (res) {
- self._resback(res);
self.paused--;
- if ((self.paused === 0) && (self.fired >= 0)) {
- self._fire();
- }
+ self._resback(res);
};
this.paused++;
@@ -179,4 +208,8 @@ MochiKit.Async.Deferred.prototype = {
this.fired = fired;
this.results[fired] = res;
+ if (this.chain.length == 0 && this.paused === 0 && this._finalizer) {
+ this.finalized = true;
+ this._finalizer(res);
+ }
if (cb && this.paused) {
// this is for "tail recursion" in case the dependent deferred
@@ -250,5 +283,5 @@ MochiKit.Base.update(MochiKit.Async, {
try {
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
status = 304;
@@ -338,5 +371,6 @@ MochiKit.Base.update(MochiKit.Async, {
password: undefined,
headers: undefined,
- mimeType: undefined
+ mimeType: undefined,
+ responseType: undefined
*/
}, opts);
@@ -372,4 +406,7 @@ MochiKit.Base.update(MochiKit.Async, {
}
}
+ if ("responseType" in opts && "responseType" in req) {
+ req.responseType = opts.responseType;
+ }
return self.sendXMLHttpRequest(req, opts.sendContent);
},
@@ -405,14 +442,42 @@ 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 */
wait: function (seconds, /* optional */value) {
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 () {
try {
@@ -511,4 +576,5 @@ MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, f
MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred();
+MochiKit.Async.DeferredList.prototype.constructor = MochiKit.Async.DeferredList;
MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) {