SPB Git forge

spb/admin-ka

Public
41commits 1branches 0releases
172.9 MBsize
maindefault branch
19 days agolast push
JavaScript 65.5% Python 17.8% CSS 13% HTML 3.7%
1.0 KB · 56 lines javascript
Raw Blame History
1'use strict';23const kDone = Symbol('kDone');4const kRun = Symbol('kRun');56/**7 * A very simple job queue with adjustable concurrency. Adapted from8 * https://github.com/STRML/async-limiter9 */10class Limiter {11  /**12   * Creates a new `Limiter`.13   *14   * @param {Number} [concurrency=Infinity] The maximum number of jobs allowed15   *     to run concurrently16   */17  constructor(concurrency) {18    this[kDone] = () => {19      this.pending--;20      this[kRun]();21    };22    this.concurrency = concurrency || Infinity;23    this.jobs = [];24    this.pending = 0;25  }2627  /**28   * Adds a job to the queue.29   *30   * @param {Function} job The job to run31   * @public32   */33  add(job) {34    this.jobs.push(job);35    this[kRun]();36  }3738  /**39   * Removes a job from the queue and runs it if possible.40   *41   * @private42   */43  [kRun]() {44    if (this.pending === this.concurrency) return;4546    if (this.jobs.length) {47      const job = this.jobs.shift();4849      this.pending++;50      job(this[kDone]);51    }52  }53}5455module.exports = Limiter;56