-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||||||
'use strict'; | ||||||||||
This comment has been minimized.
Sorry, something went wrong. |
||||||||||
|
||||||||||
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 The maximum number of jobs allowed to run | ||||||||||
* concurrently | ||||||||||
*/ | ||||||||||
constructor(concurrency) { | ||||||||||
this[kDone] = () => { | ||||||||||
this.pending--; | ||||||||||
this[kRun](); | ||||||||||
}; | ||||||||||
this.concurrency = concurrency; | ||||||||||
This comment has been minimized.
Sorry, something went wrong.
STRML
Contributor
|
const concurrency = | |
this._options.concurrencyLimit !== undefined | |
? this._options.concurrencyLimit | |
: 10; |
This comment has been minimized.
This comment has been minimized.
Sorry, something went wrong.
lpinca
Nov 30, 2019
Author
Member
I've ended up using concurrency || Infinity
as in the original code as it was easier to test: 6df06d9.
Thanks for the review.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
const Limiter = require('../lib/limiter'); | ||
|
||
describe('Limiter', () => { | ||
it('limits the number of jobs allowed to run concurrently', (done) => { | ||
const limiter = new Limiter(1); | ||
|
||
limiter.add((callback) => { | ||
setImmediate(() => { | ||
callback(); | ||
|
||
assert.strictEqual(limiter.jobs.length, 0); | ||
assert.strictEqual(limiter.pending, 1); | ||
}); | ||
}); | ||
|
||
limiter.add((callback) => { | ||
setImmediate(() => { | ||
callback(); | ||
|
||
assert.strictEqual(limiter.pending, 0); | ||
done(); | ||
}); | ||
}); | ||
|
||
assert.strictEqual(limiter.jobs.length, 1); | ||
}); | ||
}); |
:) Nice simple adaptation!