dom/tests/mochitest/ajax/mochikit/MochiKit/Async.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /***
michael@0 2
michael@0 3 MochiKit.Async 1.4
michael@0 4
michael@0 5 See <http://mochikit.com/> for documentation, downloads, license, etc.
michael@0 6
michael@0 7 (c) 2005 Bob Ippolito. All rights Reserved.
michael@0 8
michael@0 9 ***/
michael@0 10
michael@0 11 if (typeof(dojo) != 'undefined') {
michael@0 12 dojo.provide("MochiKit.Async");
michael@0 13 dojo.require("MochiKit.Base");
michael@0 14 }
michael@0 15 if (typeof(JSAN) != 'undefined') {
michael@0 16 JSAN.use("MochiKit.Base", []);
michael@0 17 }
michael@0 18
michael@0 19 try {
michael@0 20 if (typeof(MochiKit.Base) == 'undefined') {
michael@0 21 throw "";
michael@0 22 }
michael@0 23 } catch (e) {
michael@0 24 throw "MochiKit.Async depends on MochiKit.Base!";
michael@0 25 }
michael@0 26
michael@0 27 if (typeof(MochiKit.Async) == 'undefined') {
michael@0 28 MochiKit.Async = {};
michael@0 29 }
michael@0 30
michael@0 31 MochiKit.Async.NAME = "MochiKit.Async";
michael@0 32 MochiKit.Async.VERSION = "1.4";
michael@0 33 MochiKit.Async.__repr__ = function () {
michael@0 34 return "[" + this.NAME + " " + this.VERSION + "]";
michael@0 35 };
michael@0 36 MochiKit.Async.toString = function () {
michael@0 37 return this.__repr__();
michael@0 38 };
michael@0 39
michael@0 40 /** @id MochiKit.Async.Deferred */
michael@0 41 MochiKit.Async.Deferred = function (/* optional */ canceller) {
michael@0 42 this.chain = [];
michael@0 43 this.id = this._nextId();
michael@0 44 this.fired = -1;
michael@0 45 this.paused = 0;
michael@0 46 this.results = [null, null];
michael@0 47 this.canceller = canceller;
michael@0 48 this.silentlyCancelled = false;
michael@0 49 this.chained = false;
michael@0 50 };
michael@0 51
michael@0 52 MochiKit.Async.Deferred.prototype = {
michael@0 53 /** @id MochiKit.Async.Deferred.prototype.repr */
michael@0 54 repr: function () {
michael@0 55 var state;
michael@0 56 if (this.fired == -1) {
michael@0 57 state = 'unfired';
michael@0 58 } else if (this.fired === 0) {
michael@0 59 state = 'success';
michael@0 60 } else {
michael@0 61 state = 'error';
michael@0 62 }
michael@0 63 return 'Deferred(' + this.id + ', ' + state + ')';
michael@0 64 },
michael@0 65
michael@0 66 toString: MochiKit.Base.forwardCall("repr"),
michael@0 67
michael@0 68 _nextId: MochiKit.Base.counter(),
michael@0 69
michael@0 70 /** @id MochiKit.Async.Deferred.prototype.cancel */
michael@0 71 cancel: function () {
michael@0 72 var self = MochiKit.Async;
michael@0 73 if (this.fired == -1) {
michael@0 74 if (this.canceller) {
michael@0 75 this.canceller(this);
michael@0 76 } else {
michael@0 77 this.silentlyCancelled = true;
michael@0 78 }
michael@0 79 if (this.fired == -1) {
michael@0 80 this.errback(new self.CancelledError(this));
michael@0 81 }
michael@0 82 } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) {
michael@0 83 this.results[0].cancel();
michael@0 84 }
michael@0 85 },
michael@0 86
michael@0 87 _resback: function (res) {
michael@0 88 /***
michael@0 89
michael@0 90 The primitive that means either callback or errback
michael@0 91
michael@0 92 ***/
michael@0 93 this.fired = ((res instanceof Error) ? 1 : 0);
michael@0 94 this.results[this.fired] = res;
michael@0 95 this._fire();
michael@0 96 },
michael@0 97
michael@0 98 _check: function () {
michael@0 99 if (this.fired != -1) {
michael@0 100 if (!this.silentlyCancelled) {
michael@0 101 throw new MochiKit.Async.AlreadyCalledError(this);
michael@0 102 }
michael@0 103 this.silentlyCancelled = false;
michael@0 104 return;
michael@0 105 }
michael@0 106 },
michael@0 107
michael@0 108 /** @id MochiKit.Async.Deferred.prototype.callback */
michael@0 109 callback: function (res) {
michael@0 110 this._check();
michael@0 111 if (res instanceof MochiKit.Async.Deferred) {
michael@0 112 throw new Error("Deferred instances can only be chained if they are the result of a callback");
michael@0 113 }
michael@0 114 this._resback(res);
michael@0 115 },
michael@0 116
michael@0 117 /** @id MochiKit.Async.Deferred.prototype.errback */
michael@0 118 errback: function (res) {
michael@0 119 this._check();
michael@0 120 var self = MochiKit.Async;
michael@0 121 if (res instanceof self.Deferred) {
michael@0 122 throw new Error("Deferred instances can only be chained if they are the result of a callback");
michael@0 123 }
michael@0 124 if (!(res instanceof Error)) {
michael@0 125 res = new self.GenericError(res);
michael@0 126 }
michael@0 127 this._resback(res);
michael@0 128 },
michael@0 129
michael@0 130 /** @id MochiKit.Async.Deferred.prototype.addBoth */
michael@0 131 addBoth: function (fn) {
michael@0 132 if (arguments.length > 1) {
michael@0 133 fn = MochiKit.Base.partial.apply(null, arguments);
michael@0 134 }
michael@0 135 return this.addCallbacks(fn, fn);
michael@0 136 },
michael@0 137
michael@0 138 /** @id MochiKit.Async.Deferred.prototype.addCallback */
michael@0 139 addCallback: function (fn) {
michael@0 140 if (arguments.length > 1) {
michael@0 141 fn = MochiKit.Base.partial.apply(null, arguments);
michael@0 142 }
michael@0 143 return this.addCallbacks(fn, null);
michael@0 144 },
michael@0 145
michael@0 146 /** @id MochiKit.Async.Deferred.prototype.addErrback */
michael@0 147 addErrback: function (fn) {
michael@0 148 if (arguments.length > 1) {
michael@0 149 fn = MochiKit.Base.partial.apply(null, arguments);
michael@0 150 }
michael@0 151 return this.addCallbacks(null, fn);
michael@0 152 },
michael@0 153
michael@0 154 /** @id MochiKit.Async.Deferred.prototype.addCallbacks */
michael@0 155 addCallbacks: function (cb, eb) {
michael@0 156 if (this.chained) {
michael@0 157 throw new Error("Chained Deferreds can not be re-used");
michael@0 158 }
michael@0 159 this.chain.push([cb, eb]);
michael@0 160 if (this.fired >= 0) {
michael@0 161 this._fire();
michael@0 162 }
michael@0 163 return this;
michael@0 164 },
michael@0 165
michael@0 166 _fire: function () {
michael@0 167 /***
michael@0 168
michael@0 169 Used internally to exhaust the callback sequence when a result
michael@0 170 is available.
michael@0 171
michael@0 172 ***/
michael@0 173 var chain = this.chain;
michael@0 174 var fired = this.fired;
michael@0 175 var res = this.results[fired];
michael@0 176 var self = this;
michael@0 177 var cb = null;
michael@0 178 while (chain.length > 0 && this.paused === 0) {
michael@0 179 // Array
michael@0 180 var pair = chain.shift();
michael@0 181 var f = pair[fired];
michael@0 182 if (f === null) {
michael@0 183 continue;
michael@0 184 }
michael@0 185 try {
michael@0 186 res = f(res);
michael@0 187 fired = ((res instanceof Error) ? 1 : 0);
michael@0 188 if (res instanceof MochiKit.Async.Deferred) {
michael@0 189 cb = function (res) {
michael@0 190 self._resback(res);
michael@0 191 self.paused--;
michael@0 192 if ((self.paused === 0) && (self.fired >= 0)) {
michael@0 193 self._fire();
michael@0 194 }
michael@0 195 };
michael@0 196 this.paused++;
michael@0 197 }
michael@0 198 } catch (err) {
michael@0 199 fired = 1;
michael@0 200 if (!(err instanceof Error)) {
michael@0 201 err = new MochiKit.Async.GenericError(err);
michael@0 202 }
michael@0 203 res = err;
michael@0 204 }
michael@0 205 }
michael@0 206 this.fired = fired;
michael@0 207 this.results[fired] = res;
michael@0 208 if (cb && this.paused) {
michael@0 209 // this is for "tail recursion" in case the dependent deferred
michael@0 210 // is already fired
michael@0 211 res.addBoth(cb);
michael@0 212 res.chained = true;
michael@0 213 }
michael@0 214 }
michael@0 215 };
michael@0 216
michael@0 217 MochiKit.Base.update(MochiKit.Async, {
michael@0 218 /** @id MochiKit.Async.evalJSONRequest */
michael@0 219 evalJSONRequest: function (req) {
michael@0 220 return MochiKit.Base.evalJSON(req.responseText);
michael@0 221 },
michael@0 222
michael@0 223 /** @id MochiKit.Async.succeed */
michael@0 224 succeed: function (/* optional */result) {
michael@0 225 var d = new MochiKit.Async.Deferred();
michael@0 226 d.callback.apply(d, arguments);
michael@0 227 return d;
michael@0 228 },
michael@0 229
michael@0 230 /** @id MochiKit.Async.fail */
michael@0 231 fail: function (/* optional */result) {
michael@0 232 var d = new MochiKit.Async.Deferred();
michael@0 233 d.errback.apply(d, arguments);
michael@0 234 return d;
michael@0 235 },
michael@0 236
michael@0 237 /** @id MochiKit.Async.getXMLHttpRequest */
michael@0 238 getXMLHttpRequest: function () {
michael@0 239 var self = arguments.callee;
michael@0 240 if (!self.XMLHttpRequest) {
michael@0 241 var tryThese = [
michael@0 242 function () { return new XMLHttpRequest(); },
michael@0 243 function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
michael@0 244 function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
michael@0 245 function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
michael@0 246 function () {
michael@0 247 throw new MochiKit.Async.BrowserComplianceError("Browser does not support XMLHttpRequest");
michael@0 248 }
michael@0 249 ];
michael@0 250 for (var i = 0; i < tryThese.length; i++) {
michael@0 251 var func = tryThese[i];
michael@0 252 try {
michael@0 253 self.XMLHttpRequest = func;
michael@0 254 return func();
michael@0 255 } catch (e) {
michael@0 256 // pass
michael@0 257 }
michael@0 258 }
michael@0 259 }
michael@0 260 return self.XMLHttpRequest();
michael@0 261 },
michael@0 262
michael@0 263 _xhr_onreadystatechange: function (d) {
michael@0 264 // MochiKit.Logging.logDebug('this.readyState', this.readyState);
michael@0 265 var m = MochiKit.Base;
michael@0 266 if (this.readyState == 4) {
michael@0 267 // IE SUCKS
michael@0 268 try {
michael@0 269 this.onreadystatechange = null;
michael@0 270 } catch (e) {
michael@0 271 try {
michael@0 272 this.onreadystatechange = m.noop;
michael@0 273 } catch (e) {
michael@0 274 }
michael@0 275 }
michael@0 276 var status = null;
michael@0 277 try {
michael@0 278 status = this.status;
michael@0 279 if (!status && m.isNotEmpty(this.responseText)) {
michael@0 280 // 0 or undefined seems to mean cached or local
michael@0 281 status = 304;
michael@0 282 }
michael@0 283 } catch (e) {
michael@0 284 // pass
michael@0 285 // MochiKit.Logging.logDebug('error getting status?', repr(items(e)));
michael@0 286 }
michael@0 287 // 200 is OK, 201 is CREATED, 204 is NO CONTENT
michael@0 288 // 304 is NOT MODIFIED, 1223 is apparently a bug in IE
michael@0 289 if (status == 200 || status == 201 || status == 204 ||
michael@0 290 status == 304 || status == 1223) {
michael@0 291 d.callback(this);
michael@0 292 } else {
michael@0 293 var err = new MochiKit.Async.XMLHttpRequestError(this, "Request failed");
michael@0 294 if (err.number) {
michael@0 295 // XXX: This seems to happen on page change
michael@0 296 d.errback(err);
michael@0 297 } else {
michael@0 298 // XXX: this seems to happen when the server is unreachable
michael@0 299 d.errback(err);
michael@0 300 }
michael@0 301 }
michael@0 302 }
michael@0 303 },
michael@0 304
michael@0 305 _xhr_canceller: function (req) {
michael@0 306 // IE SUCKS
michael@0 307 try {
michael@0 308 req.onreadystatechange = null;
michael@0 309 } catch (e) {
michael@0 310 try {
michael@0 311 req.onreadystatechange = MochiKit.Base.noop;
michael@0 312 } catch (e) {
michael@0 313 }
michael@0 314 }
michael@0 315 req.abort();
michael@0 316 },
michael@0 317
michael@0 318
michael@0 319 /** @id MochiKit.Async.sendXMLHttpRequest */
michael@0 320 sendXMLHttpRequest: function (req, /* optional */ sendContent) {
michael@0 321 if (typeof(sendContent) == "undefined" || sendContent === null) {
michael@0 322 sendContent = "";
michael@0 323 }
michael@0 324
michael@0 325 var m = MochiKit.Base;
michael@0 326 var self = MochiKit.Async;
michael@0 327 var d = new self.Deferred(m.partial(self._xhr_canceller, req));
michael@0 328
michael@0 329 try {
michael@0 330 req.onreadystatechange = m.bind(self._xhr_onreadystatechange,
michael@0 331 req, d);
michael@0 332 req.send(sendContent);
michael@0 333 } catch (e) {
michael@0 334 try {
michael@0 335 req.onreadystatechange = null;
michael@0 336 } catch (ignore) {
michael@0 337 // pass
michael@0 338 }
michael@0 339 d.errback(e);
michael@0 340 }
michael@0 341
michael@0 342 return d;
michael@0 343
michael@0 344 },
michael@0 345
michael@0 346 /** @id MochiKit.Async.doXHR */
michael@0 347 doXHR: function (url, opts) {
michael@0 348 /*
michael@0 349 Work around a Firefox bug by dealing with XHR during
michael@0 350 the next event loop iteration. Maybe it's this one:
michael@0 351 https://bugzilla.mozilla.org/show_bug.cgi?id=249843
michael@0 352 */
michael@0 353 var self = MochiKit.Async;
michael@0 354 return self.callLater(0, self._doXHR, url, opts);
michael@0 355 },
michael@0 356
michael@0 357 _doXHR: function (url, opts) {
michael@0 358 var m = MochiKit.Base;
michael@0 359 opts = m.update({
michael@0 360 method: 'GET',
michael@0 361 sendContent: ''
michael@0 362 /*
michael@0 363 queryString: undefined,
michael@0 364 username: undefined,
michael@0 365 password: undefined,
michael@0 366 headers: undefined,
michael@0 367 mimeType: undefined
michael@0 368 */
michael@0 369 }, opts);
michael@0 370 var self = MochiKit.Async;
michael@0 371 var req = self.getXMLHttpRequest();
michael@0 372 if (opts.queryString) {
michael@0 373 var qs = m.queryString(opts.queryString);
michael@0 374 if (qs) {
michael@0 375 url += "?" + qs;
michael@0 376 }
michael@0 377 }
michael@0 378 // Safari will send undefined:undefined, so we have to check.
michael@0 379 // We can't use apply, since the function is native.
michael@0 380 if ('username' in opts) {
michael@0 381 req.open(opts.method, url, true, opts.username, opts.password);
michael@0 382 } else {
michael@0 383 req.open(opts.method, url, true);
michael@0 384 }
michael@0 385 if (req.overrideMimeType && opts.mimeType) {
michael@0 386 req.overrideMimeType(opts.mimeType);
michael@0 387 }
michael@0 388 if (opts.headers) {
michael@0 389 var headers = opts.headers;
michael@0 390 if (!m.isArrayLike(headers)) {
michael@0 391 headers = m.items(headers);
michael@0 392 }
michael@0 393 for (var i = 0; i < headers.length; i++) {
michael@0 394 var header = headers[i];
michael@0 395 var name = header[0];
michael@0 396 var value = header[1];
michael@0 397 req.setRequestHeader(name, value);
michael@0 398 }
michael@0 399 }
michael@0 400 return self.sendXMLHttpRequest(req, opts.sendContent);
michael@0 401 },
michael@0 402
michael@0 403 _buildURL: function (url/*, ...*/) {
michael@0 404 if (arguments.length > 1) {
michael@0 405 var m = MochiKit.Base;
michael@0 406 var qs = m.queryString.apply(null, m.extend(null, arguments, 1));
michael@0 407 if (qs) {
michael@0 408 return url + "?" + qs;
michael@0 409 }
michael@0 410 }
michael@0 411 return url;
michael@0 412 },
michael@0 413
michael@0 414 /** @id MochiKit.Async.doSimpleXMLHttpRequest */
michael@0 415 doSimpleXMLHttpRequest: function (url/*, ...*/) {
michael@0 416 var self = MochiKit.Async;
michael@0 417 url = self._buildURL.apply(self, arguments);
michael@0 418 return self.doXHR(url);
michael@0 419 },
michael@0 420
michael@0 421 /** @id MochiKit.Async.loadJSONDoc */
michael@0 422 loadJSONDoc: function (url/*, ...*/) {
michael@0 423 var self = MochiKit.Async;
michael@0 424 url = self._buildURL.apply(self, arguments);
michael@0 425 var d = self.doXHR(url, {
michael@0 426 'mimeType': 'text/plain',
michael@0 427 'headers': [['Accept', 'application/json']]
michael@0 428 });
michael@0 429 d = d.addCallback(self.evalJSONRequest);
michael@0 430 return d;
michael@0 431 },
michael@0 432
michael@0 433 /** @id MochiKit.Async.wait */
michael@0 434 wait: function (seconds, /* optional */value) {
michael@0 435 var d = new MochiKit.Async.Deferred();
michael@0 436 var m = MochiKit.Base;
michael@0 437 if (typeof(value) != 'undefined') {
michael@0 438 d.addCallback(function () { return value; });
michael@0 439 }
michael@0 440 var timeout = setTimeout(
michael@0 441 m.bind("callback", d),
michael@0 442 Math.floor(seconds * 1000));
michael@0 443 d.canceller = function () {
michael@0 444 try {
michael@0 445 clearTimeout(timeout);
michael@0 446 } catch (e) {
michael@0 447 // pass
michael@0 448 }
michael@0 449 };
michael@0 450 return d;
michael@0 451 },
michael@0 452
michael@0 453 /** @id MochiKit.Async.callLater */
michael@0 454 callLater: function (seconds, func) {
michael@0 455 var m = MochiKit.Base;
michael@0 456 var pfunc = m.partial.apply(m, m.extend(null, arguments, 1));
michael@0 457 return MochiKit.Async.wait(seconds).addCallback(
michael@0 458 function (res) { return pfunc(); }
michael@0 459 );
michael@0 460 }
michael@0 461 });
michael@0 462
michael@0 463
michael@0 464 /** @id MochiKit.Async.DeferredLock */
michael@0 465 MochiKit.Async.DeferredLock = function () {
michael@0 466 this.waiting = [];
michael@0 467 this.locked = false;
michael@0 468 this.id = this._nextId();
michael@0 469 };
michael@0 470
michael@0 471 MochiKit.Async.DeferredLock.prototype = {
michael@0 472 __class__: MochiKit.Async.DeferredLock,
michael@0 473 /** @id MochiKit.Async.DeferredLock.prototype.acquire */
michael@0 474 acquire: function () {
michael@0 475 var d = new MochiKit.Async.Deferred();
michael@0 476 if (this.locked) {
michael@0 477 this.waiting.push(d);
michael@0 478 } else {
michael@0 479 this.locked = true;
michael@0 480 d.callback(this);
michael@0 481 }
michael@0 482 return d;
michael@0 483 },
michael@0 484 /** @id MochiKit.Async.DeferredLock.prototype.release */
michael@0 485 release: function () {
michael@0 486 if (!this.locked) {
michael@0 487 throw TypeError("Tried to release an unlocked DeferredLock");
michael@0 488 }
michael@0 489 this.locked = false;
michael@0 490 if (this.waiting.length > 0) {
michael@0 491 this.locked = true;
michael@0 492 this.waiting.shift().callback(this);
michael@0 493 }
michael@0 494 },
michael@0 495 _nextId: MochiKit.Base.counter(),
michael@0 496 repr: function () {
michael@0 497 var state;
michael@0 498 if (this.locked) {
michael@0 499 state = 'locked, ' + this.waiting.length + ' waiting';
michael@0 500 } else {
michael@0 501 state = 'unlocked';
michael@0 502 }
michael@0 503 return 'DeferredLock(' + this.id + ', ' + state + ')';
michael@0 504 },
michael@0 505 toString: MochiKit.Base.forwardCall("repr")
michael@0 506
michael@0 507 };
michael@0 508
michael@0 509 /** @id MochiKit.Async.DeferredList */
michael@0 510 MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, fireOnOneErrback, consumeErrors, canceller) {
michael@0 511
michael@0 512 // call parent constructor
michael@0 513 MochiKit.Async.Deferred.apply(this, [canceller]);
michael@0 514
michael@0 515 this.list = list;
michael@0 516 var resultList = [];
michael@0 517 this.resultList = resultList;
michael@0 518
michael@0 519 this.finishedCount = 0;
michael@0 520 this.fireOnOneCallback = fireOnOneCallback;
michael@0 521 this.fireOnOneErrback = fireOnOneErrback;
michael@0 522 this.consumeErrors = consumeErrors;
michael@0 523
michael@0 524 var cb = MochiKit.Base.bind(this._cbDeferred, this);
michael@0 525 for (var i = 0; i < list.length; i++) {
michael@0 526 var d = list[i];
michael@0 527 resultList.push(undefined);
michael@0 528 d.addCallback(cb, i, true);
michael@0 529 d.addErrback(cb, i, false);
michael@0 530 }
michael@0 531
michael@0 532 if (list.length === 0 && !fireOnOneCallback) {
michael@0 533 this.callback(this.resultList);
michael@0 534 }
michael@0 535
michael@0 536 };
michael@0 537
michael@0 538 MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred();
michael@0 539
michael@0 540 MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) {
michael@0 541 this.resultList[index] = [succeeded, result];
michael@0 542 this.finishedCount += 1;
michael@0 543 if (this.fired == -1) {
michael@0 544 if (succeeded && this.fireOnOneCallback) {
michael@0 545 this.callback([index, result]);
michael@0 546 } else if (!succeeded && this.fireOnOneErrback) {
michael@0 547 this.errback(result);
michael@0 548 } else if (this.finishedCount == this.list.length) {
michael@0 549 this.callback(this.resultList);
michael@0 550 }
michael@0 551 }
michael@0 552 if (!succeeded && this.consumeErrors) {
michael@0 553 result = null;
michael@0 554 }
michael@0 555 return result;
michael@0 556 };
michael@0 557
michael@0 558 /** @id MochiKit.Async.gatherResults */
michael@0 559 MochiKit.Async.gatherResults = function (deferredList) {
michael@0 560 var d = new MochiKit.Async.DeferredList(deferredList, false, true, false);
michael@0 561 d.addCallback(function (results) {
michael@0 562 var ret = [];
michael@0 563 for (var i = 0; i < results.length; i++) {
michael@0 564 ret.push(results[i][1]);
michael@0 565 }
michael@0 566 return ret;
michael@0 567 });
michael@0 568 return d;
michael@0 569 };
michael@0 570
michael@0 571 /** @id MochiKit.Async.maybeDeferred */
michael@0 572 MochiKit.Async.maybeDeferred = function (func) {
michael@0 573 var self = MochiKit.Async;
michael@0 574 var result;
michael@0 575 try {
michael@0 576 var r = func.apply(null, MochiKit.Base.extend([], arguments, 1));
michael@0 577 if (r instanceof self.Deferred) {
michael@0 578 result = r;
michael@0 579 } else if (r instanceof Error) {
michael@0 580 result = self.fail(r);
michael@0 581 } else {
michael@0 582 result = self.succeed(r);
michael@0 583 }
michael@0 584 } catch (e) {
michael@0 585 result = self.fail(e);
michael@0 586 }
michael@0 587 return result;
michael@0 588 };
michael@0 589
michael@0 590
michael@0 591 MochiKit.Async.EXPORT = [
michael@0 592 "AlreadyCalledError",
michael@0 593 "CancelledError",
michael@0 594 "BrowserComplianceError",
michael@0 595 "GenericError",
michael@0 596 "XMLHttpRequestError",
michael@0 597 "Deferred",
michael@0 598 "succeed",
michael@0 599 "fail",
michael@0 600 "getXMLHttpRequest",
michael@0 601 "doSimpleXMLHttpRequest",
michael@0 602 "loadJSONDoc",
michael@0 603 "wait",
michael@0 604 "callLater",
michael@0 605 "sendXMLHttpRequest",
michael@0 606 "DeferredLock",
michael@0 607 "DeferredList",
michael@0 608 "gatherResults",
michael@0 609 "maybeDeferred",
michael@0 610 "doXHR"
michael@0 611 ];
michael@0 612
michael@0 613 MochiKit.Async.EXPORT_OK = [
michael@0 614 "evalJSONRequest"
michael@0 615 ];
michael@0 616
michael@0 617 MochiKit.Async.__new__ = function () {
michael@0 618 var m = MochiKit.Base;
michael@0 619 var ne = m.partial(m._newNamedError, this);
michael@0 620
michael@0 621 ne("AlreadyCalledError",
michael@0 622 /** @id MochiKit.Async.AlreadyCalledError */
michael@0 623 function (deferred) {
michael@0 624 /***
michael@0 625
michael@0 626 Raised by the Deferred if callback or errback happens
michael@0 627 after it was already fired.
michael@0 628
michael@0 629 ***/
michael@0 630 this.deferred = deferred;
michael@0 631 }
michael@0 632 );
michael@0 633
michael@0 634 ne("CancelledError",
michael@0 635 /** @id MochiKit.Async.CancelledError */
michael@0 636 function (deferred) {
michael@0 637 /***
michael@0 638
michael@0 639 Raised by the Deferred cancellation mechanism.
michael@0 640
michael@0 641 ***/
michael@0 642 this.deferred = deferred;
michael@0 643 }
michael@0 644 );
michael@0 645
michael@0 646 ne("BrowserComplianceError",
michael@0 647 /** @id MochiKit.Async.BrowserComplianceError */
michael@0 648 function (msg) {
michael@0 649 /***
michael@0 650
michael@0 651 Raised when the JavaScript runtime is not capable of performing
michael@0 652 the given function. Technically, this should really never be
michael@0 653 raised because a non-conforming JavaScript runtime probably
michael@0 654 isn't going to support exceptions in the first place.
michael@0 655
michael@0 656 ***/
michael@0 657 this.message = msg;
michael@0 658 }
michael@0 659 );
michael@0 660
michael@0 661 ne("GenericError",
michael@0 662 /** @id MochiKit.Async.GenericError */
michael@0 663 function (msg) {
michael@0 664 this.message = msg;
michael@0 665 }
michael@0 666 );
michael@0 667
michael@0 668 ne("XMLHttpRequestError",
michael@0 669 /** @id MochiKit.Async.XMLHttpRequestError */
michael@0 670 function (req, msg) {
michael@0 671 /***
michael@0 672
michael@0 673 Raised when an XMLHttpRequest does not complete for any reason.
michael@0 674
michael@0 675 ***/
michael@0 676 this.req = req;
michael@0 677 this.message = msg;
michael@0 678 try {
michael@0 679 // Strange but true that this can raise in some cases.
michael@0 680 this.number = req.status;
michael@0 681 } catch (e) {
michael@0 682 // pass
michael@0 683 }
michael@0 684 }
michael@0 685 );
michael@0 686
michael@0 687
michael@0 688 this.EXPORT_TAGS = {
michael@0 689 ":common": this.EXPORT,
michael@0 690 ":all": m.concat(this.EXPORT, this.EXPORT_OK)
michael@0 691 };
michael@0 692
michael@0 693 m.nameFunctions(this);
michael@0 694
michael@0 695 };
michael@0 696
michael@0 697 MochiKit.Async.__new__();
michael@0 698
michael@0 699 MochiKit.Base._exportSymbols(this, MochiKit.Async);

mercurial