MediaWiki:Gadget-libAsyncLock.js

来自兽耳维基
一位史蒂夫讨论 | 贡献2024年1月2日 (二) 23:26的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
"use strict";
(function () {
    var _$browser_3 = {};
    var process = _$browser_3 = {};
    var cachedSetTimeout;
    var cachedClearTimeout;
    function defaultSetTimout() {
        throw new Error('setTimeout has not been defined');
    }
    function defaultClearTimeout() {
        throw new Error('clearTimeout has not been defined');
    }
    (function () {
        try {
            if (typeof setTimeout === 'function') {
                cachedSetTimeout = setTimeout;
            }
            else {
                cachedSetTimeout = defaultSetTimout;
            }
        }
        catch (e) {
            cachedSetTimeout = defaultSetTimout;
        }
        try {
            if (typeof clearTimeout === 'function') {
                cachedClearTimeout = clearTimeout;
            }
            else {
                cachedClearTimeout = defaultClearTimeout;
            }
        }
        catch (e) {
            cachedClearTimeout = defaultClearTimeout;
        }
    }());
    function runTimeout(fun) {
        if (cachedSetTimeout === setTimeout) {
            return setTimeout(fun, 0);
        }
        if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
            cachedSetTimeout = setTimeout;
            return setTimeout(fun, 0);
        }
        try {
            return cachedSetTimeout(fun, 0);
        }
        catch (e) {
            try {
                return cachedSetTimeout.call(null, fun, 0);
            }
            catch (e) {
                return cachedSetTimeout.call(this, fun, 0);
            }
        }
    }
    function runClearTimeout(marker) {
        if (cachedClearTimeout === clearTimeout) {
            return clearTimeout(marker);
        }
        if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
            cachedClearTimeout = clearTimeout;
            return clearTimeout(marker);
        }
        try {
            return cachedClearTimeout(marker);
        }
        catch (e) {
            try {
                return cachedClearTimeout.call(null, marker);
            }
            catch (e) {
                return cachedClearTimeout.call(this, marker);
            }
        }
    }
    var queue = [];
    var draining = false;
    var currentQueue;
    var queueIndex = -1;
    function cleanUpNextTick() {
        if (!draining || !currentQueue) {
            return;
        }
        draining = false;
        if (currentQueue.length) {
            queue = currentQueue.concat(queue);
        }
        else {
            queueIndex = -1;
        }
        if (queue.length) {
            drainQueue();
        }
    }
    function drainQueue() {
        if (draining) {
            return;
        }
        var timeout = runTimeout(cleanUpNextTick);
        draining = true;
        var len = queue.length;
        while (len) {
            currentQueue = queue;
            queue = [];
            while (++queueIndex < len) {
                if (currentQueue) {
                    currentQueue[queueIndex].run();
                }
            }
            queueIndex = -1;
            len = queue.length;
        }
        currentQueue = null;
        draining = false;
        runClearTimeout(timeout);
    }
    process.nextTick = function (fun) {
        var args = new Array(arguments.length - 1);
        if (arguments.length > 1) {
            for (var i = 1; i < arguments.length; i++) {
                args[i - 1] = arguments[i];
            }
        }
        queue.push(new Item(fun, args));
        if (queue.length === 1 && !draining) {
            runTimeout(drainQueue);
        }
    };
    function Item(fun, array) {
        this.fun = fun;
        this.array = array;
    }
    Item.prototype.run = function () {
        this.fun.apply(null, this.array);
    };
    process.title = 'browser';
    process.browser = true;
    process.env = {};
    process.argv = [];
    process.version = '';
    process.versions = {};
    function noop() { }
    process.on = noop;
    process.addListener = noop;
    process.once = noop;
    process.off = noop;
    process.removeListener = noop;
    process.removeAllListeners = noop;
    process.emit = noop;
    process.prependListener = noop;
    process.prependOnceListener = noop;
    process.listeners = function (name) { return []; };
    process.binding = function (name) {
        throw new Error('process.binding is not supported');
    };
    process.cwd = function () { return '/'; };
    process.chdir = function (dir) {
        throw new Error('process.chdir is not supported');
    };
    process.umask = function () { return 0; };
    var _$AsyncLock_2 = {};
    (function (process) {
        (function () {
            'use strict';
            var AsyncLock = function (opts) {
                opts = opts || {};
                this.Promise = opts.Promise || Promise;
                this.queues = Object.create(null);
                this.domainReentrant = opts.domainReentrant || false;
                if (this.domainReentrant) {
                    if (typeof process === 'undefined' || typeof process.domain === 'undefined') {
                        throw new Error('Domain-reentrant locks require `process.domain` to exist. Please flip `opts.domainReentrant = false`, ' +
                            'use a NodeJS version that still implements Domain, or install a browser polyfill.');
                    }
                    this.domains = Object.create(null);
                }
                this.timeout = opts.timeout || AsyncLock.DEFAULT_TIMEOUT;
                this.maxOccupationTime = opts.maxOccupationTime || AsyncLock.DEFAULT_MAX_OCCUPATION_TIME;
                if (opts.maxPending === Infinity || (Number.isInteger(opts.maxPending) && opts.maxPending >= 0)) {
                    this.maxPending = opts.maxPending;
                }
                else {
                    this.maxPending = AsyncLock.DEFAULT_MAX_PENDING;
                }
            };
            AsyncLock.DEFAULT_TIMEOUT = 0;
            AsyncLock.DEFAULT_MAX_OCCUPATION_TIME = 0;
            AsyncLock.DEFAULT_MAX_PENDING = 1000;
            AsyncLock.prototype.acquire = function (key, fn, cb, opts) {
                if (Array.isArray(key)) {
                    return this._acquireBatch(key, fn, cb, opts);
                }
                if (typeof (fn) !== 'function') {
                    throw new Error('You must pass a function to execute');
                }
                var deferredResolve = null;
                var deferredReject = null;
                var deferred = null;
                if (typeof (cb) !== 'function') {
                    opts = cb;
                    cb = null;
                    deferred = new this.Promise(function (resolve, reject) {
                        deferredResolve = resolve;
                        deferredReject = reject;
                    });
                }
                opts = opts || {};
                var resolved = false;
                var timer = null;
                var occupationTimer = null;
                var self = this;
                var done = function (locked, err, ret) {
                    if (occupationTimer) {
                        clearTimeout(occupationTimer);
                        occupationTimer = null;
                    }
                    if (locked) {
                        if (!!self.queues[key] && self.queues[key].length === 0) {
                            delete self.queues[key];
                        }
                        if (self.domainReentrant) {
                            delete self.domains[key];
                        }
                    }
                    if (!resolved) {
                        if (!deferred) {
                            if (typeof (cb) === 'function') {
                                cb(err, ret);
                            }
                        }
                        else {
                            if (err) {
                                deferredReject(err);
                            }
                            else {
                                deferredResolve(ret);
                            }
                        }
                        resolved = true;
                    }
                    if (locked) {
                        if (!!self.queues[key] && self.queues[key].length > 0) {
                            self.queues[key].shift()();
                        }
                    }
                };
                var exec = function (locked) {
                    if (resolved) {
                        return done(locked);
                    }
                    if (timer) {
                        clearTimeout(timer);
                        timer = null;
                    }
                    if (self.domainReentrant && locked) {
                        self.domains[key] = process.domain;
                    }
                    if (fn.length === 1) {
                        var called = false;
                        try {
                            fn(function (err, ret) {
                                if (!called) {
                                    called = true;
                                    done(locked, err, ret);
                                }
                            });
                        }
                        catch (err) {
                            if (!called) {
                                called = true;
                                done(locked, err);
                            }
                        }
                    }
                    else {
                        self._promiseTry(function () {
                            return fn();
                        })
                            .then(function (ret) {
                            done(locked, undefined, ret);
                        }, function (error) {
                            done(locked, error);
                        });
                    }
                };
                if (self.domainReentrant && !!process.domain) {
                    exec = process.domain.bind(exec);
                }
                if (!self.queues[key]) {
                    self.queues[key] = [];
                    exec(true);
                }
                else if (self.domainReentrant && !!process.domain && process.domain === self.domains[key]) {
                    exec(false);
                }
                else if (self.queues[key].length >= self.maxPending) {
                    done(false, new Error('Too many pending tasks in queue ' + key));
                }
                else {
                    var taskFn = function () {
                        exec(true);
                    };
                    if (opts.skipQueue) {
                        self.queues[key].unshift(taskFn);
                    }
                    else {
                        self.queues[key].push(taskFn);
                    }
                    var timeout = opts.timeout || self.timeout;
                    if (timeout) {
                        timer = setTimeout(function () {
                            timer = null;
                            done(false, new Error('async-lock timed out in queue ' + key));
                        }, timeout);
                    }
                }
                var maxOccupationTime = opts.maxOccupationTime || self.maxOccupationTime;
                if (maxOccupationTime) {
                    occupationTimer = setTimeout(function () {
                        if (!!self.queues[key]) {
                            done(false, new Error('Maximum occupation time is exceeded in queue ' + key));
                        }
                    }, maxOccupationTime);
                }
                if (deferred) {
                    return deferred;
                }
            };
            AsyncLock.prototype._acquireBatch = function (keys, fn, cb, opts) {
                if (typeof (cb) !== 'function') {
                    opts = cb;
                    cb = null;
                }
                var self = this;
                var getFn = function (key, fn) {
                    return function (cb) {
                        self.acquire(key, fn, cb, opts);
                    };
                };
                var fnx = keys.reduceRight(function (prev, key) {
                    return getFn(key, prev);
                }, fn);
                if (typeof (cb) === 'function') {
                    fnx(cb);
                }
                else {
                    return new this.Promise(function (resolve, reject) {
                        if (fnx.length === 1) {
                            fnx(function (err, ret) {
                                if (err) {
                                    reject(err);
                                }
                                else {
                                    resolve(ret);
                                }
                            });
                        }
                        else {
                            resolve(fnx());
                        }
                    });
                }
            };
            AsyncLock.prototype.isBusy = function (key) {
                if (!key) {
                    return Object.keys(this.queues).length > 0;
                }
                else {
                    return !!this.queues[key];
                }
            };
            AsyncLock.prototype._promiseTry = function (fn) {
                try {
                    return this.Promise.resolve(fn());
                }
                catch (e) {
                    return this.Promise.reject(e);
                }
            };
            _$AsyncLock_2 = AsyncLock;
        }).call(this);
    }).call(this, _$browser_3);
    'use strict';
    var _$asyncLock_1 = _$AsyncLock_2;
    var _$input_4 = {};
    (function (global) {
        (function () {
            "use strict";
            var _asyncLock = _interopRequireDefault(_$asyncLock_1);
            function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
            global["AsyncLock"] = _asyncLock["default"];
        }).call(this);
    }).call(this, typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {});
}());