Comparing version 3.5.8 to 3.5.9
@@ -5,2 +5,7 @@ # Changelog | ||
## [3.5.9][] - 2021-07-27 | ||
- Optimize Semaphore | ||
- Add Semaphore properties: `concurrency: number` add `empty: boolean` | ||
## [3.5.8][] - 2021-07-22 | ||
@@ -87,3 +92,4 @@ | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.8...HEAD | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.9...HEAD | ||
[3.5.9]: https://github.com/metarhia/metautil/compare/v3.5.8...v3.5.9 | ||
[3.5.8]: https://github.com/metarhia/metautil/compare/v3.5.7...v3.5.8 | ||
@@ -90,0 +96,0 @@ [3.5.7]: https://github.com/metarhia/metautil/compare/v3.5.6...v3.5.7 |
@@ -5,2 +5,3 @@ 'use strict'; | ||
constructor(concurrency, size = 0, timeout = 0) { | ||
this.concurrency = concurrency; | ||
this.counter = concurrency; | ||
@@ -10,2 +11,3 @@ this.timeout = timeout; | ||
this.queue = []; | ||
this.empty = true; | ||
} | ||
@@ -17,2 +19,3 @@ | ||
this.counter--; | ||
this.empty = false; | ||
resolve(); | ||
@@ -25,9 +28,12 @@ return; | ||
} | ||
const waiting = { resolve }; | ||
const waiting = { resolve, timer: null }; | ||
waiting.timer = setTimeout(() => { | ||
waiting.resolve = null; | ||
this.queue.shift(); | ||
const { counter, concurrency } = this; | ||
this.empty = this.queue.length === 0 && counter === concurrency; | ||
reject(new Error('Semaphore timeout')); | ||
}, this.timeout); | ||
this.queue.push(waiting); | ||
this.empty = false; | ||
}); | ||
@@ -38,8 +44,11 @@ } | ||
this.counter++; | ||
if (this.queue.length === 0) return; | ||
if (this.queue.length === 0) { | ||
this.empty = this.counter === this.concurrency; | ||
return; | ||
} | ||
const { resolve, timer } = this.queue.shift(); | ||
clearTimeout(timer); | ||
setTimeout(() => { | ||
if (resolve) resolve(); | ||
}, 0); | ||
if (resolve) setTimeout(resolve, 0); | ||
const { counter, concurrency } = this; | ||
this.empty = this.queue.length === 0 && counter === concurrency; | ||
} | ||
@@ -46,0 +55,0 @@ } |
@@ -75,5 +75,7 @@ import { EventEmitter } from 'events'; | ||
constructor(concurrency: number, size?: number, timeout?: number); | ||
concurrency: number; | ||
counter: number; | ||
timeout: number; | ||
size: number; | ||
empty: boolean; | ||
queue: Array<QueueElement>; | ||
@@ -80,0 +82,0 @@ enter(): Promise<void>; |
{ | ||
"name": "metautil", | ||
"version": "3.5.8", | ||
"version": "3.5.9", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22767
504