summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/MochiKit/Async.js
Unidiff
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
@@ -8,7 +8,7 @@ See <http://mochikit.com/> for documentation, downloads, license, etc.
8 8
9***/ 9***/
10 10
11MochiKit.Base._module('Async', '1.5', ['Base']); 11MochiKit.Base.module(MochiKit, 'Async', '1.5', ['Base']);
12 12
13/** @id MochiKit.Async.Deferred */ 13/** @id MochiKit.Async.Deferred */
14MochiKit.Async.Deferred = function (/* optional */ canceller) { 14MochiKit.Async.Deferred = function (/* optional */ canceller) {
@@ -20,28 +20,32 @@ MochiKit.Async.Deferred = function (/* optional */ canceller) {
20 this.canceller = canceller; 20 this.canceller = canceller;
21 this.silentlyCancelled = false; 21 this.silentlyCancelled = false;
22 this.chained = false; 22 this.chained = false;
23 this.finalized = false;
23}; 24};
24 25
25MochiKit.Async.Deferred.prototype = { 26MochiKit.Async.Deferred.prototype = {
26 /** @id MochiKit.Async.Deferred.prototype.repr */ 27 /** @id MochiKit.Async.Deferred.prototype.repr */
27 repr: function () { 28 repr: function () {
28 var state; 29 return 'Deferred(' + this.id + ', ' + this.state() + ')';
29 if (this.fired == -1) {
30 state = 'unfired';
31 } else if (this.fired === 0) {
32 state = 'success';
33 } else {
34 state = 'error';
35 }
36 return 'Deferred(' + this.id + ', ' + state + ')';
37 }, 30 },
38 31
39 toString: MochiKit.Base.forwardCall("repr"), 32 toString: MochiKit.Base.forwardCall("repr"),
40 33
41 _nextId: MochiKit.Base.counter(), 34 _nextId: MochiKit.Base.counter(),
42 35
36 /** @id MochiKit.Async.Deferred.prototype.state */
37 state: function () {
38 if (this.fired == -1) {
39 return 'unfired';
40 } else if (this.fired === 0) {
41 return 'success';
42 } else {
43 return 'error';
44 }
45 },
46
43 /** @id MochiKit.Async.Deferred.prototype.cancel */ 47 /** @id MochiKit.Async.Deferred.prototype.cancel */
44 cancel: function () { 48 cancel: function (e) {
45 var self = MochiKit.Async; 49 var self = MochiKit.Async;
46 if (this.fired == -1) { 50 if (this.fired == -1) {
47 if (this.canceller) { 51 if (this.canceller) {
@@ -50,10 +54,15 @@ MochiKit.Async.Deferred.prototype = {
50 this.silentlyCancelled = true; 54 this.silentlyCancelled = true;
51 } 55 }
52 if (this.fired == -1) { 56 if (this.fired == -1) {
53 this.errback(new self.CancelledError(this)); 57 if (typeof(e) === 'string') {
58 e = new self.GenericError(e);
59 } else if (!(e instanceof Error)) {
60 e = new self.CancelledError(this);
61 }
62 this.errback(e);
54 } 63 }
55 } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) { 64 } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) {
56 this.results[0].cancel(); 65 this.results[0].cancel(e);
57 } 66 }
58 }, 67 },
59 68
@@ -65,7 +74,9 @@ MochiKit.Async.Deferred.prototype = {
65 ***/ 74 ***/
66 this.fired = ((res instanceof Error) ? 1 : 0); 75 this.fired = ((res instanceof Error) ? 1 : 0);
67 this.results[this.fired] = res; 76 this.results[this.fired] = res;
68 this._fire(); 77 if (this.paused === 0) {
78 this._fire();
79 }
69 }, 80 },
70 81
71 _check: function () { 82 _check: function () {
@@ -129,6 +140,9 @@ MochiKit.Async.Deferred.prototype = {
129 if (this.chained) { 140 if (this.chained) {
130 throw new Error("Chained Deferreds can not be re-used"); 141 throw new Error("Chained Deferreds can not be re-used");
131 } 142 }
143 if (this.finalized) {
144 throw new Error("Finalized Deferreds can not be re-used");
145 }
132 this.chain.push([cb, eb]); 146 this.chain.push([cb, eb]);
133 if (this.fired >= 0) { 147 if (this.fired >= 0) {
134 this._fire(); 148 this._fire();
@@ -136,6 +150,24 @@ MochiKit.Async.Deferred.prototype = {
136 return this; 150 return this;
137 }, 151 },
138 152
153 /** @id MochiKit.Async.Deferred.prototype.setFinalizer */
154 setFinalizer: function (fn) {
155 if (this.chained) {
156 throw new Error("Chained Deferreds can not be re-used");
157 }
158 if (this.finalized) {
159 throw new Error("Finalized Deferreds can not be re-used");
160 }
161 if (arguments.length > 1) {
162 fn = MochiKit.Base.partial.apply(null, arguments);
163 }
164 this._finalizer = fn;
165 if (this.fired >= 0) {
166 this._fire();
167 }
168 return this;
169 },
170
139 _fire: function () { 171 _fire: function () {
140 /*** 172 /***
141 173
@@ -160,11 +192,8 @@ MochiKit.Async.Deferred.prototype = {
160 fired = ((res instanceof Error) ? 1 : 0); 192 fired = ((res instanceof Error) ? 1 : 0);
161 if (res instanceof MochiKit.Async.Deferred) { 193 if (res instanceof MochiKit.Async.Deferred) {
162 cb = function (res) { 194 cb = function (res) {
163 self._resback(res);
164 self.paused--; 195 self.paused--;
165 if ((self.paused === 0) && (self.fired >= 0)) { 196 self._resback(res);
166 self._fire();
167 }
168 }; 197 };
169 this.paused++; 198 this.paused++;
170 } 199 }
@@ -178,6 +207,10 @@ MochiKit.Async.Deferred.prototype = {
178 } 207 }
179 this.fired = fired; 208 this.fired = fired;
180 this.results[fired] = res; 209 this.results[fired] = res;
210 if (this.chain.length == 0 && this.paused === 0 && this._finalizer) {
211 this.finalized = true;
212 this._finalizer(res);
213 }
181 if (cb && this.paused) { 214 if (cb && this.paused) {
182 // this is for "tail recursion" in case the dependent deferred 215 // this is for "tail recursion" in case the dependent deferred
183 // is already fired 216 // is already fired
@@ -249,7 +282,7 @@ MochiKit.Base.update(MochiKit.Async, {
249 var status = null; 282 var status = null;
250 try { 283 try {
251 status = this.status; 284 status = this.status;
252 if (!status && m.isNotEmpty(this.responseText)) { 285 if (!status && (this.response || m.isNotEmpty(this.responseText))) {
253 // 0 or undefined seems to mean cached or local 286 // 0 or undefined seems to mean cached or local
254 status = 304; 287 status = 304;
255 } 288 }
@@ -337,7 +370,8 @@ MochiKit.Base.update(MochiKit.Async, {
337 username: undefined, 370 username: undefined,
338 password: undefined, 371 password: undefined,
339 headers: undefined, 372 headers: undefined,
340 mimeType: undefined 373 mimeType: undefined,
374 responseType: undefined
341 */ 375 */
342 }, opts); 376 }, opts);
343 var self = MochiKit.Async; 377 var self = MochiKit.Async;
@@ -371,6 +405,9 @@ MochiKit.Base.update(MochiKit.Async, {
371 req.setRequestHeader(name, value); 405 req.setRequestHeader(name, value);
372 } 406 }
373 } 407 }
408 if ("responseType" in opts && "responseType" in req) {
409 req.responseType = opts.responseType;
410 }
374 return self.sendXMLHttpRequest(req, opts.sendContent); 411 return self.sendXMLHttpRequest(req, opts.sendContent);
375 }, 412 },
376 413
@@ -404,16 +441,44 @@ MochiKit.Base.update(MochiKit.Async, {
404 return d; 441 return d;
405 }, 442 },
406 443
444 /** @id MochiKit.Async.loadScript */
445 loadScript: function (url) {
446 var d = new MochiKit.Async.Deferred();
447 var script = document.createElement("script");
448 script.type = "text/javascript";
449 script.src = url;
450 script.onload = function () {
451 script.onload = null;
452 script.onerror = null;
453 script.onreadystatechange = null;
454 script = null;
455 d.callback();
456 };
457 script.onerror = function (msg) {
458 script.onload = null;
459 script.onerror = null;
460 script.onreadystatechange = null;
461 script = null;
462 msg = "Failed to load script at " + url + ": " + msg;
463 d.errback(new URIError(msg, url));
464 }
465 script.onreadystatechange = function () {
466 if (script.readyState == "loaded" || script.readyState == "complete") {
467 script.onload();
468 } else {
469 // IE doesn't bother to report errors...
470 MochiKit.Async.callLater(10, script.onerror, "Script loading timed out")
471 }
472 };
473 document.getElementsByTagName("head")[0].appendChild(script);
474 return d;
475 },
476
407 /** @id MochiKit.Async.wait */ 477 /** @id MochiKit.Async.wait */
408 wait: function (seconds, /* optional */value) { 478 wait: function (seconds, /* optional */value) {
409 var d = new MochiKit.Async.Deferred(); 479 var d = new MochiKit.Async.Deferred();
410 var m = MochiKit.Base; 480 var cb = MochiKit.Base.bind("callback", d, value);
411 if (typeof(value) != 'undefined') { 481 var timeout = setTimeout(cb, Math.floor(seconds * 1000));
412 d.addCallback(function () { return value; });
413 }
414 var timeout = setTimeout(
415 m.bind("callback", d),
416 Math.floor(seconds * 1000));
417 d.canceller = function () { 482 d.canceller = function () {
418 try { 483 try {
419 clearTimeout(timeout); 484 clearTimeout(timeout);
@@ -510,6 +575,7 @@ MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, f
510}; 575};
511 576
512MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred(); 577MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred();
578MochiKit.Async.DeferredList.prototype.constructor = MochiKit.Async.DeferredList;
513 579
514MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) { 580MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) {
515 this.resultList[index] = [succeeded, result]; 581 this.resultList[index] = [succeeded, result];