diff options
author | RaindropsSys <contact@minteck.org> | 2023-05-12 16:43:04 +0200 |
---|---|---|
committer | RaindropsSys <contact@minteck.org> | 2023-05-12 16:43:04 +0200 |
commit | 0e4f6cea6a4f8d1f860ded405a66f9fca9d93d96 (patch) | |
tree | f8dcad460d9120442e23c8c567ba246eed39f71a /includes/external/pair/node_modules/ws/lib/limiter.js | |
parent | 761c84c0c17c04113608a20e8401270023741c7e (diff) | |
download | pluralconnect-0e4f6cea6a4f8d1f860ded405a66f9fca9d93d96.tar.gz pluralconnect-0e4f6cea6a4f8d1f860ded405a66f9fca9d93d96.tar.bz2 pluralconnect-0e4f6cea6a4f8d1f860ded405a66f9fca9d93d96.zip |
Updated 5 files and added 29 files (automated)
Diffstat (limited to 'includes/external/pair/node_modules/ws/lib/limiter.js')
-rw-r--r-- | includes/external/pair/node_modules/ws/lib/limiter.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/includes/external/pair/node_modules/ws/lib/limiter.js b/includes/external/pair/node_modules/ws/lib/limiter.js new file mode 100644 index 0000000..3fd3578 --- /dev/null +++ b/includes/external/pair/node_modules/ws/lib/limiter.js @@ -0,0 +1,55 @@ +'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; |