12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 'use strict';
- const kDone = Symbol('kDone');
- const kRun = Symbol('kRun');
- class Limiter {
-
- constructor(concurrency) {
- this[kDone] = () => {
- this.pending--;
- this[kRun]();
- };
- this.concurrency = concurrency || Infinity;
- this.jobs = [];
- this.pending = 0;
- }
-
- add(job) {
- this.jobs.push(job);
- this[kRun]();
- }
-
- [kRun]() {
- if (this.pending === this.concurrency) return;
- if (this.jobs.length) {
- const job = this.jobs.shift();
- this.pending++;
- job(this[kDone]);
- }
- }
- }
- module.exports = Limiter;
|