Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 eval('(' + arguments[0].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, 304 is NOT_MODIFIED |
michael@0 | 288 | if (status == 200 || status == 304) { // OK |
michael@0 | 289 | d.callback(this); |
michael@0 | 290 | } else { |
michael@0 | 291 | var err = new MochiKit.Async.XMLHttpRequestError(this, "Request failed"); |
michael@0 | 292 | if (err.number) { |
michael@0 | 293 | // XXX: This seems to happen on page change |
michael@0 | 294 | d.errback(err); |
michael@0 | 295 | } else { |
michael@0 | 296 | // XXX: this seems to happen when the server is unreachable |
michael@0 | 297 | d.errback(err); |
michael@0 | 298 | } |
michael@0 | 299 | } |
michael@0 | 300 | } |
michael@0 | 301 | }, |
michael@0 | 302 | |
michael@0 | 303 | _xhr_canceller: function (req) { |
michael@0 | 304 | // IE SUCKS |
michael@0 | 305 | try { |
michael@0 | 306 | req.onreadystatechange = null; |
michael@0 | 307 | } catch (e) { |
michael@0 | 308 | try { |
michael@0 | 309 | req.onreadystatechange = MochiKit.Base.noop; |
michael@0 | 310 | } catch (e) { |
michael@0 | 311 | } |
michael@0 | 312 | } |
michael@0 | 313 | req.abort(); |
michael@0 | 314 | }, |
michael@0 | 315 | |
michael@0 | 316 | |
michael@0 | 317 | /** @id MochiKit.Async.sendXMLHttpRequest */ |
michael@0 | 318 | sendXMLHttpRequest: function (req, /* optional */ sendContent) { |
michael@0 | 319 | if (typeof(sendContent) == "undefined" || sendContent === null) { |
michael@0 | 320 | sendContent = ""; |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | var m = MochiKit.Base; |
michael@0 | 324 | var self = MochiKit.Async; |
michael@0 | 325 | var d = new self.Deferred(m.partial(self._xhr_canceller, req)); |
michael@0 | 326 | |
michael@0 | 327 | try { |
michael@0 | 328 | req.onreadystatechange = m.bind(self._xhr_onreadystatechange, |
michael@0 | 329 | req, d); |
michael@0 | 330 | req.send(sendContent); |
michael@0 | 331 | } catch (e) { |
michael@0 | 332 | try { |
michael@0 | 333 | req.onreadystatechange = null; |
michael@0 | 334 | } catch (ignore) { |
michael@0 | 335 | // pass |
michael@0 | 336 | } |
michael@0 | 337 | d.errback(e); |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | return d; |
michael@0 | 341 | |
michael@0 | 342 | }, |
michael@0 | 343 | |
michael@0 | 344 | /** @id MochiKit.Async.doXHR */ |
michael@0 | 345 | doXHR: function (url, opts) { |
michael@0 | 346 | var m = MochiKit.Base; |
michael@0 | 347 | opts = m.update({ |
michael@0 | 348 | method: 'GET', |
michael@0 | 349 | sendContent: '' |
michael@0 | 350 | /* |
michael@0 | 351 | queryString: undefined, |
michael@0 | 352 | username: undefined, |
michael@0 | 353 | password: undefined, |
michael@0 | 354 | headers: undefined, |
michael@0 | 355 | mimeType: undefined |
michael@0 | 356 | */ |
michael@0 | 357 | }, opts); |
michael@0 | 358 | var self = MochiKit.Async; |
michael@0 | 359 | var req = self.getXMLHttpRequest(); |
michael@0 | 360 | if (opts.queryString) { |
michael@0 | 361 | var qs = m.queryString(opts.queryString); |
michael@0 | 362 | if (qs) { |
michael@0 | 363 | url += "?" + qs; |
michael@0 | 364 | } |
michael@0 | 365 | } |
michael@0 | 366 | req.open(opts.method, url, true, opts.username, opts.password); |
michael@0 | 367 | if (req.overrideMimeType && opts.mimeType) { |
michael@0 | 368 | req.overrideMimeType(opts.mimeType); |
michael@0 | 369 | } |
michael@0 | 370 | if (opts.headers) { |
michael@0 | 371 | var headers = opts.headers; |
michael@0 | 372 | if (!m.isArrayLike(headers)) { |
michael@0 | 373 | headers = m.items(headers); |
michael@0 | 374 | } |
michael@0 | 375 | for (var i = 0; i < headers.length; i++) { |
michael@0 | 376 | var header = headers[i]; |
michael@0 | 377 | var name = header[0]; |
michael@0 | 378 | var value = header[1]; |
michael@0 | 379 | req.setRequestHeader(name, value); |
michael@0 | 380 | } |
michael@0 | 381 | } |
michael@0 | 382 | return self.sendXMLHttpRequest(req, opts.sendContent); |
michael@0 | 383 | }, |
michael@0 | 384 | |
michael@0 | 385 | _buildURL: function (url/*, ...*/) { |
michael@0 | 386 | if (arguments.length > 1) { |
michael@0 | 387 | var m = MochiKit.Base; |
michael@0 | 388 | var qs = m.queryString.apply(null, m.extend(null, arguments, 1)); |
michael@0 | 389 | if (qs) { |
michael@0 | 390 | return url + "?" + qs; |
michael@0 | 391 | } |
michael@0 | 392 | } |
michael@0 | 393 | return url; |
michael@0 | 394 | }, |
michael@0 | 395 | |
michael@0 | 396 | /** @id MochiKit.Async.doSimpleXMLHttpRequest */ |
michael@0 | 397 | doSimpleXMLHttpRequest: function (url/*, ...*/) { |
michael@0 | 398 | var self = MochiKit.Async; |
michael@0 | 399 | url = self._buildURL.apply(self, arguments); |
michael@0 | 400 | return self.doXHR(url); |
michael@0 | 401 | }, |
michael@0 | 402 | |
michael@0 | 403 | /** @id MochiKit.Async.loadJSONDoc */ |
michael@0 | 404 | loadJSONDoc: function (url/*, ...*/) { |
michael@0 | 405 | var self = MochiKit.Async; |
michael@0 | 406 | url = self._buildURL.apply(self, arguments); |
michael@0 | 407 | var d = self.doXHR(url, { |
michael@0 | 408 | 'mimeType': 'text/plain', |
michael@0 | 409 | 'headers': [['Accept', 'application/json']] |
michael@0 | 410 | }); |
michael@0 | 411 | d = d.addCallback(self.evalJSONRequest); |
michael@0 | 412 | return d; |
michael@0 | 413 | }, |
michael@0 | 414 | |
michael@0 | 415 | /** @id MochiKit.Async.wait */ |
michael@0 | 416 | wait: function (seconds, /* optional */value) { |
michael@0 | 417 | var d = new MochiKit.Async.Deferred(); |
michael@0 | 418 | var m = MochiKit.Base; |
michael@0 | 419 | if (typeof(value) != 'undefined') { |
michael@0 | 420 | d.addCallback(function () { return value; }); |
michael@0 | 421 | } |
michael@0 | 422 | var timeout = setTimeout( |
michael@0 | 423 | m.bind("callback", d), |
michael@0 | 424 | Math.floor(seconds * 1000)); |
michael@0 | 425 | d.canceller = function () { |
michael@0 | 426 | try { |
michael@0 | 427 | clearTimeout(timeout); |
michael@0 | 428 | } catch (e) { |
michael@0 | 429 | // pass |
michael@0 | 430 | } |
michael@0 | 431 | }; |
michael@0 | 432 | return d; |
michael@0 | 433 | }, |
michael@0 | 434 | |
michael@0 | 435 | /** @id MochiKit.Async.callLater */ |
michael@0 | 436 | callLater: function (seconds, func) { |
michael@0 | 437 | var m = MochiKit.Base; |
michael@0 | 438 | var pfunc = m.partial.apply(m, m.extend(null, arguments, 1)); |
michael@0 | 439 | return MochiKit.Async.wait(seconds).addCallback( |
michael@0 | 440 | function (res) { return pfunc(); } |
michael@0 | 441 | ); |
michael@0 | 442 | } |
michael@0 | 443 | }); |
michael@0 | 444 | |
michael@0 | 445 | |
michael@0 | 446 | /** @id MochiKit.Async.DeferredLock */ |
michael@0 | 447 | MochiKit.Async.DeferredLock = function () { |
michael@0 | 448 | this.waiting = []; |
michael@0 | 449 | this.locked = false; |
michael@0 | 450 | this.id = this._nextId(); |
michael@0 | 451 | }; |
michael@0 | 452 | |
michael@0 | 453 | MochiKit.Async.DeferredLock.prototype = { |
michael@0 | 454 | __class__: MochiKit.Async.DeferredLock, |
michael@0 | 455 | /** @id MochiKit.Async.DeferredLock.prototype.acquire */ |
michael@0 | 456 | acquire: function () { |
michael@0 | 457 | var d = new MochiKit.Async.Deferred(); |
michael@0 | 458 | if (this.locked) { |
michael@0 | 459 | this.waiting.push(d); |
michael@0 | 460 | } else { |
michael@0 | 461 | this.locked = true; |
michael@0 | 462 | d.callback(this); |
michael@0 | 463 | } |
michael@0 | 464 | return d; |
michael@0 | 465 | }, |
michael@0 | 466 | /** @id MochiKit.Async.DeferredLock.prototype.release */ |
michael@0 | 467 | release: function () { |
michael@0 | 468 | if (!this.locked) { |
michael@0 | 469 | throw TypeError("Tried to release an unlocked DeferredLock"); |
michael@0 | 470 | } |
michael@0 | 471 | this.locked = false; |
michael@0 | 472 | if (this.waiting.length > 0) { |
michael@0 | 473 | this.locked = true; |
michael@0 | 474 | this.waiting.shift().callback(this); |
michael@0 | 475 | } |
michael@0 | 476 | }, |
michael@0 | 477 | _nextId: MochiKit.Base.counter(), |
michael@0 | 478 | repr: function () { |
michael@0 | 479 | var state; |
michael@0 | 480 | if (this.locked) { |
michael@0 | 481 | state = 'locked, ' + this.waiting.length + ' waiting'; |
michael@0 | 482 | } else { |
michael@0 | 483 | state = 'unlocked'; |
michael@0 | 484 | } |
michael@0 | 485 | return 'DeferredLock(' + this.id + ', ' + state + ')'; |
michael@0 | 486 | }, |
michael@0 | 487 | toString: MochiKit.Base.forwardCall("repr") |
michael@0 | 488 | |
michael@0 | 489 | }; |
michael@0 | 490 | |
michael@0 | 491 | /** @id MochiKit.Async.DeferredList */ |
michael@0 | 492 | MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, fireOnOneErrback, consumeErrors, canceller) { |
michael@0 | 493 | |
michael@0 | 494 | // call parent constructor |
michael@0 | 495 | MochiKit.Async.Deferred.apply(this, [canceller]); |
michael@0 | 496 | |
michael@0 | 497 | this.list = list; |
michael@0 | 498 | var resultList = []; |
michael@0 | 499 | this.resultList = resultList; |
michael@0 | 500 | |
michael@0 | 501 | this.finishedCount = 0; |
michael@0 | 502 | this.fireOnOneCallback = fireOnOneCallback; |
michael@0 | 503 | this.fireOnOneErrback = fireOnOneErrback; |
michael@0 | 504 | this.consumeErrors = consumeErrors; |
michael@0 | 505 | |
michael@0 | 506 | var cb = MochiKit.Base.bind(this._cbDeferred, this); |
michael@0 | 507 | for (var i = 0; i < list.length; i++) { |
michael@0 | 508 | var d = list[i]; |
michael@0 | 509 | resultList.push(undefined); |
michael@0 | 510 | d.addCallback(cb, i, true); |
michael@0 | 511 | d.addErrback(cb, i, false); |
michael@0 | 512 | } |
michael@0 | 513 | |
michael@0 | 514 | if (list.length === 0 && !fireOnOneCallback) { |
michael@0 | 515 | this.callback(this.resultList); |
michael@0 | 516 | } |
michael@0 | 517 | |
michael@0 | 518 | }; |
michael@0 | 519 | |
michael@0 | 520 | MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred(); |
michael@0 | 521 | |
michael@0 | 522 | MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) { |
michael@0 | 523 | this.resultList[index] = [succeeded, result]; |
michael@0 | 524 | this.finishedCount += 1; |
michael@0 | 525 | if (this.fired == -1) { |
michael@0 | 526 | if (succeeded && this.fireOnOneCallback) { |
michael@0 | 527 | this.callback([index, result]); |
michael@0 | 528 | } else if (!succeeded && this.fireOnOneErrback) { |
michael@0 | 529 | this.errback(result); |
michael@0 | 530 | } else if (this.finishedCount == this.list.length) { |
michael@0 | 531 | this.callback(this.resultList); |
michael@0 | 532 | } |
michael@0 | 533 | } |
michael@0 | 534 | if (!succeeded && this.consumeErrors) { |
michael@0 | 535 | result = null; |
michael@0 | 536 | } |
michael@0 | 537 | return result; |
michael@0 | 538 | }; |
michael@0 | 539 | |
michael@0 | 540 | /** @id MochiKit.Async.gatherResults */ |
michael@0 | 541 | MochiKit.Async.gatherResults = function (deferredList) { |
michael@0 | 542 | var d = new MochiKit.Async.DeferredList(deferredList, false, true, false); |
michael@0 | 543 | d.addCallback(function (results) { |
michael@0 | 544 | var ret = []; |
michael@0 | 545 | for (var i = 0; i < results.length; i++) { |
michael@0 | 546 | ret.push(results[i][1]); |
michael@0 | 547 | } |
michael@0 | 548 | return ret; |
michael@0 | 549 | }); |
michael@0 | 550 | return d; |
michael@0 | 551 | }; |
michael@0 | 552 | |
michael@0 | 553 | /** @id MochiKit.Async.maybeDeferred */ |
michael@0 | 554 | MochiKit.Async.maybeDeferred = function (func) { |
michael@0 | 555 | var self = MochiKit.Async; |
michael@0 | 556 | var result; |
michael@0 | 557 | try { |
michael@0 | 558 | var r = func.apply(null, MochiKit.Base.extend([], arguments, 1)); |
michael@0 | 559 | if (r instanceof self.Deferred) { |
michael@0 | 560 | result = r; |
michael@0 | 561 | } else if (r instanceof Error) { |
michael@0 | 562 | result = self.fail(r); |
michael@0 | 563 | } else { |
michael@0 | 564 | result = self.succeed(r); |
michael@0 | 565 | } |
michael@0 | 566 | } catch (e) { |
michael@0 | 567 | result = self.fail(e); |
michael@0 | 568 | } |
michael@0 | 569 | return result; |
michael@0 | 570 | }; |
michael@0 | 571 | |
michael@0 | 572 | |
michael@0 | 573 | MochiKit.Async.EXPORT = [ |
michael@0 | 574 | "AlreadyCalledError", |
michael@0 | 575 | "CancelledError", |
michael@0 | 576 | "BrowserComplianceError", |
michael@0 | 577 | "GenericError", |
michael@0 | 578 | "XMLHttpRequestError", |
michael@0 | 579 | "Deferred", |
michael@0 | 580 | "succeed", |
michael@0 | 581 | "fail", |
michael@0 | 582 | "getXMLHttpRequest", |
michael@0 | 583 | "doSimpleXMLHttpRequest", |
michael@0 | 584 | "loadJSONDoc", |
michael@0 | 585 | "wait", |
michael@0 | 586 | "callLater", |
michael@0 | 587 | "sendXMLHttpRequest", |
michael@0 | 588 | "DeferredLock", |
michael@0 | 589 | "DeferredList", |
michael@0 | 590 | "gatherResults", |
michael@0 | 591 | "maybeDeferred", |
michael@0 | 592 | "doXHR" |
michael@0 | 593 | ]; |
michael@0 | 594 | |
michael@0 | 595 | MochiKit.Async.EXPORT_OK = [ |
michael@0 | 596 | "evalJSONRequest" |
michael@0 | 597 | ]; |
michael@0 | 598 | |
michael@0 | 599 | MochiKit.Async.__new__ = function () { |
michael@0 | 600 | var m = MochiKit.Base; |
michael@0 | 601 | var ne = m.partial(m._newNamedError, this); |
michael@0 | 602 | |
michael@0 | 603 | ne("AlreadyCalledError", |
michael@0 | 604 | /** @id MochiKit.Async.AlreadyCalledError */ |
michael@0 | 605 | function (deferred) { |
michael@0 | 606 | /*** |
michael@0 | 607 | |
michael@0 | 608 | Raised by the Deferred if callback or errback happens |
michael@0 | 609 | after it was already fired. |
michael@0 | 610 | |
michael@0 | 611 | ***/ |
michael@0 | 612 | this.deferred = deferred; |
michael@0 | 613 | } |
michael@0 | 614 | ); |
michael@0 | 615 | |
michael@0 | 616 | ne("CancelledError", |
michael@0 | 617 | /** @id MochiKit.Async.CancelledError */ |
michael@0 | 618 | function (deferred) { |
michael@0 | 619 | /*** |
michael@0 | 620 | |
michael@0 | 621 | Raised by the Deferred cancellation mechanism. |
michael@0 | 622 | |
michael@0 | 623 | ***/ |
michael@0 | 624 | this.deferred = deferred; |
michael@0 | 625 | } |
michael@0 | 626 | ); |
michael@0 | 627 | |
michael@0 | 628 | ne("BrowserComplianceError", |
michael@0 | 629 | /** @id MochiKit.Async.BrowserComplianceError */ |
michael@0 | 630 | function (msg) { |
michael@0 | 631 | /*** |
michael@0 | 632 | |
michael@0 | 633 | Raised when the JavaScript runtime is not capable of performing |
michael@0 | 634 | the given function. Technically, this should really never be |
michael@0 | 635 | raised because a non-conforming JavaScript runtime probably |
michael@0 | 636 | isn't going to support exceptions in the first place. |
michael@0 | 637 | |
michael@0 | 638 | ***/ |
michael@0 | 639 | this.message = msg; |
michael@0 | 640 | } |
michael@0 | 641 | ); |
michael@0 | 642 | |
michael@0 | 643 | ne("GenericError", |
michael@0 | 644 | /** @id MochiKit.Async.GenericError */ |
michael@0 | 645 | function (msg) { |
michael@0 | 646 | this.message = msg; |
michael@0 | 647 | } |
michael@0 | 648 | ); |
michael@0 | 649 | |
michael@0 | 650 | ne("XMLHttpRequestError", |
michael@0 | 651 | /** @id MochiKit.Async.XMLHttpRequestError */ |
michael@0 | 652 | function (req, msg) { |
michael@0 | 653 | /*** |
michael@0 | 654 | |
michael@0 | 655 | Raised when an XMLHttpRequest does not complete for any reason. |
michael@0 | 656 | |
michael@0 | 657 | ***/ |
michael@0 | 658 | this.req = req; |
michael@0 | 659 | this.message = msg; |
michael@0 | 660 | try { |
michael@0 | 661 | // Strange but true that this can raise in some cases. |
michael@0 | 662 | this.number = req.status; |
michael@0 | 663 | } catch (e) { |
michael@0 | 664 | // pass |
michael@0 | 665 | } |
michael@0 | 666 | } |
michael@0 | 667 | ); |
michael@0 | 668 | |
michael@0 | 669 | |
michael@0 | 670 | this.EXPORT_TAGS = { |
michael@0 | 671 | ":common": this.EXPORT, |
michael@0 | 672 | ":all": m.concat(this.EXPORT, this.EXPORT_OK) |
michael@0 | 673 | }; |
michael@0 | 674 | |
michael@0 | 675 | m.nameFunctions(this); |
michael@0 | 676 | |
michael@0 | 677 | }; |
michael@0 | 678 | |
michael@0 | 679 | MochiKit.Async.__new__(); |
michael@0 | 680 | |
michael@0 | 681 | MochiKit.Base._exportSymbols(this, MochiKit.Async); |