📄 async.js
字号:
/***MochiKit.Async 1.3.1See <http://mochikit.com/> for documentation, downloads, license, etc.(c) 2005 Bob Ippolito. All rights Reserved.***/if (typeof(dojo) != 'undefined') { dojo.provide("MochiKit.Async"); dojo.require("MochiKit.Base");}if (typeof(JSAN) != 'undefined') { JSAN.use("MochiKit.Base", []);}try { if (typeof(MochiKit.Base) == 'undefined') { throw ""; }} catch (e) { throw "MochiKit.Async depends on MochiKit.Base!";}if (typeof(MochiKit.Async) == 'undefined') { MochiKit.Async = {};}MochiKit.Async.NAME = "MochiKit.Async";MochiKit.Async.VERSION = "1.3.1";MochiKit.Async.__repr__ = function () { return "[" + this.NAME + " " + this.VERSION + "]";};MochiKit.Async.toString = function () { return this.__repr__();};MochiKit.Async.Deferred = function (/* optional */ canceller) { this.chain = []; this.id = this._nextId(); this.fired = -1; this.paused = 0; this.results = [null, null]; this.canceller = canceller; this.silentlyCancelled = false; this.chained = false;};MochiKit.Async.Deferred.prototype = { 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 + ')'; }, toString: MochiKit.Base.forwardCall("repr"), _nextId: MochiKit.Base.counter(), cancel: function () { var self = MochiKit.Async; if (this.fired == -1) { if (this.canceller) { this.canceller(this); } else { this.silentlyCancelled = true; } if (this.fired == -1) { this.errback(new self.CancelledError(this)); } } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) { this.results[0].cancel(); } }, _pause: function () { /*** Used internally to signal that it's waiting on another Deferred ***/ this.paused++; }, _unpause: function () { /*** Used internally to signal that it's no longer waiting on another Deferred. ***/ this.paused--; if ((this.paused === 0) && (this.fired >= 0)) { this._fire(); } }, _continue: function (res) { /*** Used internally when a dependent deferred fires. ***/ this._resback(res); this._unpause(); }, _resback: function (res) { /*** The primitive that means either callback or errback ***/ this.fired = ((res instanceof Error) ? 1 : 0); this.results[this.fired] = res; this._fire(); }, _check: function () { if (this.fired != -1) { if (!this.silentlyCancelled) { throw new MochiKit.Async.AlreadyCalledError(this); } this.silentlyCancelled = false; return; } }, callback: function (res) { this._check(); if (res instanceof MochiKit.Async.Deferred) { throw new Error("Deferred instances can only be chained if they are the result of a callback"); } this._resback(res); }, errback: function (res) { this._check(); var self = MochiKit.Async; if (res instanceof self.Deferred) { throw new Error("Deferred instances can only be chained if they are the result of a callback"); } if (!(res instanceof Error)) { res = new self.GenericError(res); } this._resback(res); }, addBoth: function (fn) { if (arguments.length > 1) { fn = MochiKit.Base.partial.apply(null, arguments); } return this.addCallbacks(fn, fn); }, addCallback: function (fn) { if (arguments.length > 1) { fn = MochiKit.Base.partial.apply(null, arguments); } return this.addCallbacks(fn, null); }, addErrback: function (fn) { if (arguments.length > 1) { fn = MochiKit.Base.partial.apply(null, arguments); } return this.addCallbacks(null, fn); }, addCallbacks: function (cb, eb) { if (this.chained) { throw new Error("Chained Deferreds can not be re-used"); } this.chain.push([cb, eb]); if (this.fired >= 0) { this._fire(); } return this; }, _fire: function () { /*** Used internally to exhaust the callback sequence when a result is available. ***/ var chain = this.chain; var fired = this.fired; var res = this.results[fired]; var self = this; var cb = null; while (chain.length > 0 && this.paused === 0) { // Array var pair = chain.shift(); var f = pair[fired]; if (f === null) { continue; } try { res = f(res); fired = ((res instanceof Error) ? 1 : 0); if (res instanceof MochiKit.Async.Deferred) { cb = function (res) { self._continue(res); }; this._pause(); } } catch (err) { fired = 1; if (!(err instanceof Error)) { err = new MochiKit.Async.GenericError(err); } res = err; } } this.fired = fired; this.results[fired] = res; if (cb && this.paused) { // this is for "tail recursion" in case the dependent deferred // is already fired res.addBoth(cb); res.chained = true; } }};MochiKit.Base.update(MochiKit.Async, { evalJSONRequest: function (/* req */) { return eval('(' + arguments[0].responseText + ')'); }, succeed: function (/* optional */result) { var d = new MochiKit.Async.Deferred(); d.callback.apply(d, arguments); return d; }, fail: function (/* optional */result) { var d = new MochiKit.Async.Deferred(); d.errback.apply(d, arguments); return d; }, getXMLHttpRequest: function () { var self = arguments.callee; if (!self.XMLHttpRequest) { var tryThese = [ function () { return new XMLHttpRequest(); }, function () { return new ActiveXObject('Msxml2.XMLHTTP'); }, function () { return new ActiveXObject('Microsoft.XMLHTTP'); }, function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); }, function () { throw new MochiKit.Async.BrowserComplianceError("Browser does not support XMLHttpRequest"); } ]; for (var i = 0; i < tryThese.length; i++) { var func = tryThese[i]; try { self.XMLHttpRequest = func; return func(); } catch (e) { // pass } } } return self.XMLHttpRequest(); }, _nothing: function () {}, _xhr_onreadystatechange: function (d) { // MochiKit.Logging.logDebug('this.readyState', this.readyState); if (this.readyState == 4) { // IE SUCKS try { this.onreadystatechange = null; } catch (e) { try { this.onreadystatechange = MochiKit.Async._nothing; } catch (e) { } } var status = null; try { status = this.status; if (!status && MochiKit.Base.isNotEmpty(this.responseText)) { // 0 or undefined seems to mean cached or local status = 304; } } catch (e) { // pass // MochiKit.Logging.logDebug('error getting status?', repr(items(e))); } // 200 is OK, 304 is NOT_MODIFIED if (status == 200 || status == 304) { // OK d.callback(this); } else { var err = new MochiKit.Async.XMLHttpRequestError(this, "Request failed"); if (err.number) { // XXX: This seems to happen on page change d.errback(err); } else { // XXX: this seems to happen when the server is unreachable d.errback(err); } } } },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -