🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/25 09:12:36
🔄 下次更新时间:2026/01/25 10:12:36
手动刷新缓存

queue

Asynchronous function queue with adjustable concurrency.

Este script não deve ser instalado diretamente. Este script é uma biblioteca de outros scripts para incluir com o diretório meta // @require https://update.greasyfork.org/scripts/482311/1297431/queue.js

Você precisará instalar uma extensão como Tampermonkey, Greasemonkey ou Violentmonkey para instalar este script.

You will need to install an extension such as Tampermonkey to install this script.

Você precisará instalar uma extensão como Tampermonkey ou Violentmonkey para instalar este script.

Você precisará instalar uma extensão como Tampermonkey ou Userscripts para instalar este script.

Você precisará instalar uma extensão como o Tampermonkey para instalar este script.

Você precisará instalar um gerenciador de scripts de usuário para instalar este script.

(Eu já tenho um gerenciador de scripts de usuário, me deixe instalá-lo!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

(Eu já possuo um gerenciador de estilos de usuário, me deixar fazer a instalação!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name               queue
// @description        Asynchronous function queue with adjustable concurrency.
// @author             Jesse Tane
// @namespace          https://github.com/jessetane/queue
// @version            7.0.0
// @license            MIT
// ==/UserScript==

var { Queue, QueueEvent } = (function (exports) {
  'use strict';

  const has = Object.prototype.hasOwnProperty;

  /**
   * Since CustomEvent is only supported in nodejs since version 19,
   * you have to create your own class instead of using CustomEvent
   * @see https://github.com/nodejs/node/issues/40678
   * */
  class QueueEvent extends Event {
    constructor (name, detail) {
      super(name);
      this.detail = detail;
    }
  }

  class Queue extends EventTarget {
    constructor (options = {}) {
      super();
      const { concurrency = Infinity, timeout = 0, autostart = false, results = null } = options;
      this.concurrency = concurrency;
      this.timeout = timeout;
      this.autostart = autostart;
      this.results = results;
      this.pending = 0;
      this.session = 0;
      this.running = false;
      this.jobs = [];
      this.timers = [];
      this.addEventListener('error', this._errorHandler);
    }

    _errorHandler (evt) {
      this.end(evt.detail.error);
    }

    pop () {
      return this.jobs.pop()
    }

    shift () {
      return this.jobs.shift()
    }

    indexOf (searchElement, fromIndex) {
      return this.jobs.indexOf(searchElement, fromIndex)
    }

    lastIndexOf (searchElement, fromIndex) {
      if (fromIndex !== undefined) return this.jobs.lastIndexOf(searchElement, fromIndex)
      return this.jobs.lastIndexOf(searchElement)
    }

    slice (start, end) {
      this.jobs = this.jobs.slice(start, end);
      return this
    }

    reverse () {
      this.jobs.reverse();
      return this
    }

    push (...workers) {
      const methodResult = this.jobs.push(...workers);
      if (this.autostart) this._start();
      return methodResult
    }

    unshift (...workers) {
      const methodResult = this.jobs.unshift(...workers);
      if (this.autostart) this._start();
      return methodResult
    }

    splice (start, deleteCount, ...workers) {
      this.jobs.splice(start, deleteCount, ...workers);
      if (this.autostart) this._start();
      return this
    }

    get length () {
      return this.pending + this.jobs.length
    }

    start (callback) {
      if (this.running) throw new Error('already started')
      let awaiter;
      if (callback) {
        this._addCallbackToEndEvent(callback);
      } else {
        awaiter = this._createPromiseToEndEvent();
      }
      this._start();
      return awaiter
    }

    _start () {
      this.running = true;
      if (this.pending >= this.concurrency) {
        return
      }
      if (this.jobs.length === 0) {
        if (this.pending === 0) {
          this.done();
        }
        return
      }
      const job = this.jobs.shift();
      const session = this.session;
      const timeout = (job !== undefined) && has.call(job, 'timeout') ? job.timeout : this.timeout;
      let once = true;
      let timeoutId = null;
      let didTimeout = false;
      let resultIndex = null;
      const next = (error, ...result) => {
        if (once && this.session === session) {
          once = false;
          this.pending--;
          if (timeoutId !== null) {
            this.timers = this.timers.filter(tID => tID !== timeoutId);
            clearTimeout(timeoutId);
          }
          if (error) {
            this.dispatchEvent(new QueueEvent('error', { error, job }));
          } else if (!didTimeout) {
            if (resultIndex !== null && this.results !== null) {
              this.results[resultIndex] = [...result];
            }
            this.dispatchEvent(new QueueEvent('success', { result: [...result], job }));
          }
          if (this.session === session) {
            if (this.pending === 0 && this.jobs.length === 0) {
              this.done();
            } else if (this.running) {
              this._start();
            }
          }
        }
      };
      if (timeout) {
        timeoutId = setTimeout(() => {
          didTimeout = true;
          this.dispatchEvent(new QueueEvent('timeout', { next, job }));
          next();
        }, timeout);
        this.timers.push(timeoutId);
      }
      if (this.results != null) {
        resultIndex = this.results.length;
        this.results[resultIndex] = null;
      }
      this.pending++;
      this.dispatchEvent(new QueueEvent('start', { job }));
      job.promise = job(next);
      if (job.promise !== undefined && typeof job.promise.then === 'function') {
        job.promise.then(function (result) {
          return next(undefined, result)
        }).catch(function (err) {
          return next(err || true)
        });
      }
      if (this.running && this.jobs.length > 0) {
        this._start();
      }
    }

    stop () {
      this.running = false;
    }

    end (error) {
      this.clearTimers();
      this.jobs.length = 0;
      this.pending = 0;
      this.done(error);
    }

    clearTimers () {
      this.timers.forEach(timer => {
        clearTimeout(timer);
      });
      this.timers = [];
    }

    _addCallbackToEndEvent (cb) {
      const onend = evt => {
        this.removeEventListener('end', onend);
        cb(evt.detail.error, this.results);
      };
      this.addEventListener('end', onend);
    }

    _createPromiseToEndEvent () {
      return new Promise((resolve, reject) => {
        this._addCallbackToEndEvent((error, results) => {
          if (error) reject(error);
          else resolve(results);
        });
      })
    }

    done (error) {
      this.session++;
      this.running = false;
      this.dispatchEvent(new QueueEvent('end', { error }));
    }
  }

  exports.Queue = Queue;
  exports.QueueEvent = QueueEvent;

  return exports;

})({});