⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 async.js

📁 Ajax下日志框架
💻 JS
📖 第 1 页 / 共 2 页
字号:
    _xhr_canceller: function (req) {        // IE SUCKS        try {            req.onreadystatechange = null;        } catch (e) {            try {                req.onreadystatechange = MochiKit.Async._nothing;            } catch (e) {            }        }        req.abort();    },        sendXMLHttpRequest: function (req, /* optional */ sendContent) {        if (typeof(sendContent) == "undefined" || sendContent === null) {            sendContent = "";        }        var m = MochiKit.Base;        var self = MochiKit.Async;        var d = new self.Deferred(m.partial(self._xhr_canceller, req));                try {            req.onreadystatechange = m.bind(self._xhr_onreadystatechange,                req, d);            req.send(sendContent);        } catch (e) {            try {                req.onreadystatechange = null;            } catch (ignore) {                // pass            }            d.errback(e);        }        return d;    },    doSimpleXMLHttpRequest: function (url/*, ...*/) {        var self = MochiKit.Async;        var req = self.getXMLHttpRequest();        if (arguments.length > 1) {            var m = MochiKit.Base;            var qs = m.queryString.apply(null, m.extend(null, arguments, 1));            if (qs) {                url += "?" + qs;            }        }        req.open("GET", url, true);        return self.sendXMLHttpRequest(req);    },    loadJSONDoc: function (url) {        var self = MochiKit.Async;        var d = self.doSimpleXMLHttpRequest.apply(self, arguments);        d = d.addCallback(self.evalJSONRequest);        return d;    },    wait: function (seconds, /* optional */value) {        var d = new MochiKit.Async.Deferred();        var m = MochiKit.Base;        if (typeof(value) != 'undefined') {            d.addCallback(function () { return value; });        }        var timeout = setTimeout(            m.bind("callback", d),            Math.floor(seconds * 1000));        d.canceller = function () {            try {                clearTimeout(timeout);            } catch (e) {                // pass            }        };        return d;    },    callLater: function (seconds, func) {        var m = MochiKit.Base;        var pfunc = m.partial.apply(m, m.extend(null, arguments, 1));        return MochiKit.Async.wait(seconds).addCallback(            function (res) { return pfunc(); }        );    }});MochiKit.Async.DeferredLock = function () {    this.waiting = [];    this.locked = false;    this.id = this._nextId();};MochiKit.Async.DeferredLock.prototype = {    __class__: MochiKit.Async.DeferredLock,    acquire: function () {        d = new MochiKit.Async.Deferred();        if (this.locked) {            this.waiting.push(d);        } else {            this.locked = true;            d.callback(this);        }        return d;    },    release: function () {        if (!this.locked) {            throw TypeError("Tried to release an unlocked DeferredLock");        }        this.locked = false;        if (this.waiting.length > 0) {            this.locked = true;            this.waiting.shift().callback(this);        }    },    _nextId: MochiKit.Base.counter(),    repr: function () {        var state;        if (this.locked) {            state = 'locked, ' + this.waiting.length + ' waiting';        } else {            state = 'unlocked';        }        return 'DeferredLock(' + this.id + ', ' + state + ')';    },    toString: MochiKit.Base.forwardCall("repr")};MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, fireOnOneErrback, consumeErrors, canceller) {    this.list = list;    this.resultList = new Array(this.list.length);    // Deferred init    this.chain = [];    this.id = this._nextId();    this.fired = -1;    this.paused = 0;    this.results = [null, null];    this.canceller = canceller;    this.silentlyCancelled = false;        if (this.list.length === 0 && !fireOnOneCallback) {        this.callback(this.resultList);    }        this.finishedCount = 0;    this.fireOnOneCallback = fireOnOneCallback;    this.fireOnOneErrback = fireOnOneErrback;    this.consumeErrors = consumeErrors;    var index = 0;    MochiKit.Base.map(MochiKit.Base.bind(function (d) {        d.addCallback(MochiKit.Base.bind(this._cbDeferred, this), index, true);        d.addErrback(MochiKit.Base.bind(this._cbDeferred, this), index, false);        index += 1;    }, this), this.list);};MochiKit.Base.update(MochiKit.Async.DeferredList.prototype,                     MochiKit.Async.Deferred.prototype);MochiKit.Base.update(MochiKit.Async.DeferredList.prototype, {    _cbDeferred: function (index, succeeded, result) {        this.resultList[index] = [succeeded, result];        this.finishedCount += 1;        if (this.fired !== 0) {            if (succeeded && this.fireOnOneCallback) {                this.callback([index, result]);            } else if (!succeeded && this.fireOnOneErrback) {                this.errback(result);            } else if (this.finishedCount == this.list.length) {                this.callback(this.resultList);            }        }        if (!succeeded && this.consumeErrors) {            result = null;        }        return result;    }});MochiKit.Async.gatherResults = function (deferredList) {    var d = new MochiKit.Async.DeferredList(deferredList, false, true, false);    d.addCallback(function (results) {        var ret = [];        for (var i = 0; i < results.length; i++) {            ret.push(results[i][1]);        }        return ret;    });    return d;};MochiKit.Async.maybeDeferred = function (func) {    var self = MochiKit.Async;    var result;    try {        var r = func.apply(null, MochiKit.Base.extend([], arguments, 1));        if (r instanceof self.Deferred) {            result = r;        } else if (r instanceof Error) {            result = self.fail(r);        } else {            result = self.succeed(r);        }    } catch (e) {        result = self.fail(e);    }    return result;};MochiKit.Async.EXPORT = [    "AlreadyCalledError",    "CancelledError",    "BrowserComplianceError",    "GenericError",    "XMLHttpRequestError",    "Deferred",    "succeed",    "fail",    "getXMLHttpRequest",    "doSimpleXMLHttpRequest",    "loadJSONDoc",    "wait",    "callLater",    "sendXMLHttpRequest",    "DeferredLock",    "DeferredList",    "gatherResults",    "maybeDeferred"];    MochiKit.Async.EXPORT_OK = [    "evalJSONRequest"];MochiKit.Async.__new__ = function () {    var m = MochiKit.Base;    var ne = m.partial(m._newNamedError, this);    ne("AlreadyCalledError",         function (deferred) {            /***            Raised by the Deferred if callback or errback happens            after it was already fired.            ***/            this.deferred = deferred;        }    );    ne("CancelledError",        function (deferred) {            /***            Raised by the Deferred cancellation mechanism.            ***/            this.deferred = deferred;        }    );    ne("BrowserComplianceError",        function (msg) {            /***            Raised when the JavaScript runtime is not capable of performing            the given function.  Technically, this should really never be            raised because a non-conforming JavaScript runtime probably            isn't going to support exceptions in the first place.            ***/            this.message = msg;        }    );    ne("GenericError",         function (msg) {            this.message = msg;        }    );    ne("XMLHttpRequestError",        function (req, msg) {            /***            Raised when an XMLHttpRequest does not complete for any reason.            ***/            this.req = req;            this.message = msg;            try {                // Strange but true that this can raise in some cases.                this.number = req.status;            } catch (e) {                // pass            }        }    );    this.EXPORT_TAGS = {        ":common": this.EXPORT,        ":all": m.concat(this.EXPORT, this.EXPORT_OK)    };    m.nameFunctions(this);};MochiKit.Async.__new__();MochiKit.Base._exportSymbols(this, MochiKit.Async);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -