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