lightning-pool
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -10,2 +10,2 @@ import {Pool, IPoolFactory} from './Pool'; | ||
export function createPool<T = any>(factory: IPoolFactory<T>, options: PoolOptions): Pool<T>; | ||
export function createPool<T = any>(factory: IPoolFactory<T>, options?: PoolOptions): Pool<T>; |
@@ -11,2 +11,4 @@ import {PoolOptions} from "./PoolOptions"; | ||
declare type CallbackFunction = ()=>void; | ||
export interface IPoolFactory<T> { | ||
@@ -41,3 +43,3 @@ create(info: { tries: number, maxRetries: number }): Promise<T> | T; | ||
release(resource: T): Promise<void>; | ||
release(resource: T, callback: () => void): void; | ||
release(resource: T, callback: CallbackFunction): void; | ||
@@ -48,7 +50,8 @@ destroy(resource: T): void; | ||
close(force?: boolean): Promise<void>; | ||
close(force: boolean, callback: () => void): void; | ||
close(): Promise<void> | ||
close(callback: CallbackFunction): void | ||
close(terminateWait: number, callback?: CallbackFunction): Promise<void>; | ||
close(force: boolean, callback?: CallbackFunction): void; | ||
emitSafe(event: string, ...args); | ||
} | ||
@@ -146,3 +146,4 @@ /* lightning-pool | ||
try { | ||
this.start(); | ||
if (this._state === PoolState.IDLE) | ||
this.start(); | ||
} catch (e) { | ||
@@ -236,3 +237,3 @@ return callback(e); | ||
this._state = PoolState.STARTED; | ||
this._setHouseKeep(); | ||
this._setHouseKeep(this.options.houseKeepInterval); | ||
this._ensureMin(); | ||
@@ -245,26 +246,53 @@ this.emitSafe('start'); | ||
* | ||
* @param {Boolean} [force] | ||
* @param {Function} [callback] | ||
* @return {Promise|undefined} | ||
*/ | ||
close(force, callback) { | ||
close(arg0, arg1) { | ||
let terminateWait = Infinity; | ||
let callback; | ||
/* istanbul ignore next */ | ||
if (typeof force === 'function') { | ||
callback = force; | ||
force = false; | ||
if (typeof arg0 === 'function') { | ||
callback = arg0; | ||
} else { | ||
terminateWait = typeof arg0 === 'number' ? arg0 : | ||
(!!arg0 ? 0 : Infinity); | ||
callback = arg1; | ||
} | ||
if (!callback) | ||
return promisify.fromCallback(cb => this.close(force, cb)); | ||
if (this._state !== PoolState.STARTED) | ||
return promisify.fromCallback(cb => this.close(terminateWait, cb)); | ||
if (this._state === PoolState.CLOSED || this._state === PoolState.IDLE) | ||
return callback(); | ||
if (this._state === PoolState.CLOSING) { | ||
this.once('close', callback); | ||
return; | ||
} | ||
this.emitSafe('closing'); | ||
clearTimeout(this._houseKeepHandle); | ||
this._state = PoolState.CLOSING; | ||
this._requestQueue.forEach(t => t.cancel()); | ||
if (force) | ||
this._acquiredResources.forEach(t => this.release(t.resource)); | ||
this._requestQueue = new DoublyLinked(); | ||
this._requestsProcessing = 0; | ||
this.emitSafe('closing'); | ||
if (terminateWait <= 0) { | ||
this._acquiredResources.forEach(t => this.release(t.resource)); | ||
} else { | ||
const startTime = Date.now(); | ||
this._closeWaitTimer = setInterval(() => { | ||
if (!this._allResources.size) { | ||
clearInterval(this._closeWaitTimer); | ||
this._closeWaitTimer = undefined; | ||
return; | ||
} | ||
if (Date.now() > startTime + terminateWait) { | ||
clearInterval(this._closeWaitTimer); | ||
this._closeWaitTimer = undefined; | ||
this._acquiredResources.forEach(t => this.release(t.resource)); | ||
this.emitSafe('terminate'); | ||
} | ||
}, 50); | ||
} | ||
this._setHouseKeep(5); | ||
this.once('close', callback); | ||
this._houseKeep(); | ||
} | ||
@@ -407,12 +435,9 @@ | ||
*/ | ||
_setHouseKeep() { | ||
clearTimeout(this._houseKeepHandle); | ||
this._houseKeepHandle = null; | ||
if (this._state !== PoolState.STARTED || !this.options.houseKeepInterval) | ||
return; | ||
this._houseKeepHandle = setTimeout(() => { | ||
this._houseKeep(); | ||
this._setHouseKeep(); | ||
}, this.options.houseKeepInterval); | ||
this._houseKeepHandle.unref(); | ||
_setHouseKeep(ms) { | ||
clearInterval(this._houseKeepHandle); | ||
this._houseKeepHandle = undefined; | ||
if (this.state === PoolState.STARTED || this.state === PoolState.CLOSING) | ||
this._houseKeepHandle = setInterval(() => { | ||
this._houseKeep(); | ||
}, ms); | ||
}; | ||
@@ -425,3 +450,2 @@ | ||
_houseKeep() { | ||
clearTimeout(this._houseKeepHandle); | ||
const isClosing = this._state === PoolState.CLOSING; | ||
@@ -441,10 +465,9 @@ const now = Date.now(); | ||
if (isClosing) { | ||
/* Check again 5 ms later */ | ||
if (this._allResources.size) | ||
/* Check again 5 ms later */ | ||
setTimeout(() => this._houseKeep(), 5); | ||
else { | ||
this._state = PoolState.CLOSED; | ||
this._requestsProcessing = 0; | ||
this.emitSafe('close'); | ||
} | ||
return; | ||
clearInterval(this._houseKeepHandle); | ||
this._state = PoolState.CLOSED; | ||
this._requestsProcessing = 0; | ||
this.emitSafe('close'); | ||
} | ||
@@ -451,0 +474,0 @@ } |
{ | ||
"name": "lightning-pool", | ||
"description": "Fastest object pool implementation for JavaScript", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"author": "Panates Ltd.", | ||
@@ -16,3 +16,3 @@ "contributors": [ | ||
"dependencies": { | ||
"doublylinked": "^2.3.0", | ||
"doublylinked": "^2.4.0", | ||
"errorex": "^2.3.2", | ||
@@ -22,6 +22,6 @@ "putil-promisify": "^1.7.2" | ||
"devDependencies": { | ||
"@types/node": "^14.11.2", | ||
"eslint": "^7.10.0", | ||
"@types/node": "^14.14.7", | ||
"eslint": "^7.13.0", | ||
"eslint-config-google": "^0.14.0", | ||
"mocha": "^8.1.3", | ||
"mocha": "^8.2.1", | ||
"nyc": "^15.1.0", | ||
@@ -28,0 +28,0 @@ "rejected-or-not": "^2.0.0" |
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
38227
837
Updateddoublylinked@^2.4.0