Sha256: 2472a4b8a73e09cf53b98626db6df8485db5b923389f73131f71e25576d5551a

Contents?: true

Size: 1.06 KB

Versions: 22

Compression:

Stored size: 1.06 KB

Contents

'use strict';

const kDone = Symbol('kDone');
const kRun = Symbol('kRun');

/**
 * A very simple job queue with adjustable concurrency. Adapted from
 * https://github.com/STRML/async-limiter
 */
class Limiter {
  /**
   * Creates a new `Limiter`.
   *
   * @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
   *     to run concurrently
   */
  constructor(concurrency) {
    this[kDone] = () => {
      this.pending--;
      this[kRun]();
    };
    this.concurrency = concurrency || Infinity;
    this.jobs = [];
    this.pending = 0;
  }

  /**
   * Adds a job to the queue.
   *
   * @param {Function} job The job to run
   * @public
   */
  add(job) {
    this.jobs.push(job);
    this[kRun]();
  }

  /**
   * Removes a job from the queue and runs it if possible.
   *
   * @private
   */
  [kRun]() {
    if (this.pending === this.concurrency) return;

    if (this.jobs.length) {
      const job = this.jobs.shift();

      this.pending++;
      job(this[kDone]);
    }
  }
}

module.exports = Limiter;

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
isomorfeus-transport-2.0.9 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.8 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.7 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.6 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.5 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.4 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.3 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.2 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.1 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0.rc10 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0.rc9 node_modules/ws/lib/limiter.js
isomorfeus-puppetmaster-0.5.7 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0.rc8 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0.rc7 node_modules/ws/lib/limiter.js
isomorfeus-puppetmaster-0.5.6 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0.rc6 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0.rc5 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0.rc4 node_modules/ws/lib/limiter.js
isomorfeus-transport-2.0.0.rc3 node_modules/ws/lib/limiter.js