lightning-pool
Advanced tools
Comparing version 4.4.3 to 4.5.0
@@ -1,2 +0,6 @@ | ||
export class AbortError extends Error { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AbortError = void 0; | ||
class AbortError extends Error { | ||
} | ||
exports.AbortError = AbortError; |
@@ -1,2 +0,5 @@ | ||
export var PoolState; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ResourceState = exports.PoolState = void 0; | ||
var PoolState; | ||
(function (PoolState) { | ||
@@ -7,4 +10,4 @@ PoolState[PoolState["IDLE"] = 0] = "IDLE"; | ||
PoolState[PoolState["CLOSED"] = 3] = "CLOSED"; | ||
})(PoolState || (PoolState = {})); | ||
export var ResourceState; | ||
})(PoolState || (exports.PoolState = PoolState = {})); | ||
var ResourceState; | ||
(function (ResourceState) { | ||
@@ -14,2 +17,2 @@ ResourceState[ResourceState["IDLE"] = 0] = "IDLE"; | ||
ResourceState[ResourceState["VALIDATION"] = 2] = "VALIDATION"; | ||
})(ResourceState || (ResourceState = {})); | ||
})(ResourceState || (exports.ResourceState = ResourceState = {})); |
@@ -1,7 +0,11 @@ | ||
import { Pool } from './pool.js'; | ||
export * from './abort-error.js'; | ||
export * from './definitions.js'; | ||
export * from './pool.js'; | ||
export function createPool(factory, config) { | ||
return new Pool(factory, config); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createPool = createPool; | ||
const tslib_1 = require("tslib"); | ||
const pool_js_1 = require("./pool.js"); | ||
tslib_1.__exportStar(require("./abort-error.js"), exports); | ||
tslib_1.__exportStar(require("./definitions.js"), exports); | ||
tslib_1.__exportStar(require("./pool.js"), exports); | ||
function createPool(factory, config) { | ||
return new pool_js_1.Pool(factory, config); | ||
} |
@@ -1,2 +0,5 @@ | ||
import { EventEmitter } from 'events'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PoolOptions = void 0; | ||
const events_1 = require("events"); | ||
const defaultValues = { | ||
@@ -15,3 +18,3 @@ acquireMaxRetries: 0, | ||
}; | ||
export class PoolOptions extends EventEmitter { | ||
class PoolOptions extends events_1.EventEmitter { | ||
constructor(pool) { | ||
@@ -126,1 +129,2 @@ super(); | ||
} | ||
exports.PoolOptions = PoolOptions; |
@@ -0,4 +1,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PoolRequest = void 0; | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
function noop() { } | ||
export class PoolRequest { | ||
class PoolRequest { | ||
constructor(pool, callback, options) { | ||
@@ -23,1 +26,2 @@ this.timedOut = false; | ||
} | ||
exports.PoolRequest = PoolRequest; |
103
esm/pool.js
@@ -1,19 +0,23 @@ | ||
import DoublyLinked from 'doublylinked'; | ||
import { EventEmitter } from 'events'; | ||
import promisify from 'putil-promisify'; | ||
import { AbortError } from './abort-error.js'; | ||
import { PoolState, ResourceState, } from './definitions.js'; | ||
import { PoolOptions } from './pool-options.js'; | ||
import { PoolRequest } from './pool-request.js'; | ||
import { ResourceItem } from './resource-item.js'; | ||
export class Pool extends EventEmitter { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Pool = void 0; | ||
const tslib_1 = require("tslib"); | ||
const doublylinked_1 = tslib_1.__importDefault(require("doublylinked")); | ||
const events_1 = require("events"); | ||
const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify")); | ||
const abort_error_js_1 = require("./abort-error.js"); | ||
const definitions_js_1 = require("./definitions.js"); | ||
const pool_options_js_1 = require("./pool-options.js"); | ||
const pool_request_js_1 = require("./pool-request.js"); | ||
const resource_item_js_1 = require("./resource-item.js"); | ||
class Pool extends events_1.EventEmitter { | ||
constructor(factory, config) { | ||
super(); | ||
this._requestQueue = new DoublyLinked(); | ||
this._requestQueue = new doublylinked_1.default(); | ||
this._allResources = new Map(); | ||
this._acquiredResources = new DoublyLinked(); | ||
this._idleResources = new DoublyLinked(); | ||
this._acquiredResources = new doublylinked_1.default(); | ||
this._idleResources = new doublylinked_1.default(); | ||
this._creating = 0; | ||
this._requestsProcessing = 0; | ||
this._state = PoolState.IDLE; | ||
this._state = definitions_js_1.PoolState.IDLE; | ||
if (typeof factory !== 'object') { | ||
@@ -34,3 +38,3 @@ throw new TypeError('You must provide `factory` object'); | ||
} | ||
const opts = (this._options = new PoolOptions(this)); | ||
const opts = (this._options = new pool_options_js_1.PoolOptions(this)); | ||
opts.on('change', (prop, val) => { | ||
@@ -94,8 +98,8 @@ if (prop === 'houseKeepInterval') | ||
start() { | ||
if (this._state === PoolState.STARTED) | ||
if (this._state === definitions_js_1.PoolState.STARTED) | ||
return; | ||
if (this._state >= PoolState.CLOSING) { | ||
if (this._state >= definitions_js_1.PoolState.CLOSING) { | ||
throw new Error('Closed pool can not be started again'); | ||
} | ||
this._state = PoolState.STARTED; | ||
this._state = definitions_js_1.PoolState.STARTED; | ||
this._setHouseKeep(this.options.houseKeepInterval); | ||
@@ -115,8 +119,8 @@ this._ensureMin(); | ||
if (!callback) { | ||
return promisify.fromCallback(cb => this.close(terminateWait, cb)); | ||
return putil_promisify_1.default.fromCallback(cb => this.close(terminateWait, cb)); | ||
} | ||
if (this._state === PoolState.CLOSED || this._state === PoolState.IDLE) { | ||
if (this._state === definitions_js_1.PoolState.CLOSED || this._state === definitions_js_1.PoolState.IDLE) { | ||
return callback(); | ||
} | ||
if (this._state === PoolState.CLOSING) { | ||
if (this._state === definitions_js_1.PoolState.CLOSING) { | ||
this.once('close', callback); | ||
@@ -128,5 +132,5 @@ return; | ||
clearTimeout(this._houseKeepTimer); | ||
this._state = PoolState.CLOSING; | ||
this._state = definitions_js_1.PoolState.CLOSING; | ||
this._requestQueue.forEach(t => t.stopTimout()); | ||
this._requestQueue = new DoublyLinked(); | ||
this._requestQueue = new doublylinked_1.default(); | ||
this._requestsProcessing = 0; | ||
@@ -156,7 +160,7 @@ if (terminateWait <= 0) { | ||
closeAsync(arg0) { | ||
return promisify.fromCallback(cb => this.close(arg0, cb)); | ||
return putil_promisify_1.default.fromCallback(cb => this.close(arg0, cb)); | ||
} | ||
acquire(callback) { | ||
if (!callback) | ||
return promisify.fromCallback(cb => this.acquire(cb)); | ||
return putil_promisify_1.default.fromCallback(cb => this.acquire(cb)); | ||
try { | ||
@@ -171,3 +175,3 @@ this.start(); | ||
} | ||
this._requestQueue.push(new PoolRequest(this, callback)); | ||
this._requestQueue.push(new pool_request_js_1.PoolRequest(this, callback)); | ||
this._processNextRequest(); | ||
@@ -180,3 +184,3 @@ } | ||
const item = this._allResources.get(resource); | ||
if (item && item.state !== ResourceState.IDLE) { | ||
if (item && item.state !== definitions_js_1.ResourceState.IDLE) { | ||
this._itemSetIdle(item, callback); | ||
@@ -190,3 +194,3 @@ } | ||
releaseAsync(resource) { | ||
return promisify.fromCallback(cb => this.release(resource, cb)); | ||
return putil_promisify_1.default.fromCallback(cb => this.release(resource, cb)); | ||
} | ||
@@ -212,3 +216,3 @@ /** | ||
destroyAsync(resource) { | ||
return promisify.fromCallback(cb => this.destroy(resource, cb)); | ||
return putil_promisify_1.default.fromCallback(cb => this.destroy(resource, cb)); | ||
} | ||
@@ -229,3 +233,3 @@ /** | ||
_processNextRequest() { | ||
if (this._state !== PoolState.STARTED || | ||
if (this._state !== definitions_js_1.PoolState.STARTED || | ||
this._requestsProcessing >= this.options.max - this.acquired) { | ||
@@ -244,3 +248,3 @@ return; | ||
/* istanbul ignore next : Hard to simulate */ | ||
if (this._state !== PoolState.STARTED) { | ||
if (this._state !== definitions_js_1.PoolState.STARTED) { | ||
this._itemDestroy(item); | ||
@@ -311,3 +315,3 @@ return; | ||
}); | ||
if (err instanceof AbortError || tries >= maxRetries) { | ||
if (err instanceof abort_error_js_1.AbortError || tries >= maxRetries) { | ||
this._creating--; | ||
@@ -323,3 +327,3 @@ return callback && callback(err); | ||
} | ||
const item = new ResourceItem(obj); | ||
const item = new resource_item_js_1.ResourceItem(obj); | ||
this._itemSetIdle(item); | ||
@@ -336,5 +340,5 @@ this._allResources.set(obj, item); | ||
if (!o) { | ||
return handleCallback(new AbortError('Factory returned no resource')); | ||
return handleCallback(new abort_error_js_1.AbortError('Factory returned no resource')); | ||
} | ||
promisify.await(o, handleCallback); | ||
putil_promisify_1.default.await(o, handleCallback); | ||
} | ||
@@ -351,4 +355,4 @@ catch (e) { | ||
this._houseKeepTimer = undefined; | ||
if ((ms > 0 && this.state === PoolState.STARTED) || | ||
this.state === PoolState.CLOSING) { | ||
if ((ms > 0 && this.state === definitions_js_1.PoolState.STARTED) || | ||
this.state === definitions_js_1.PoolState.CLOSING) { | ||
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms); | ||
@@ -358,3 +362,3 @@ } | ||
_houseKeep() { | ||
const isClosing = this._state === PoolState.CLOSING; | ||
const isClosing = this._state === definitions_js_1.PoolState.CLOSING; | ||
const now = Date.now(); | ||
@@ -377,3 +381,3 @@ let m = this._allResources.size - this.options.min; | ||
clearInterval(this._houseKeepTimer); | ||
this._state = PoolState.CLOSED; | ||
this._state = definitions_js_1.PoolState.CLOSED; | ||
this._requestsProcessing = 0; | ||
@@ -391,5 +395,5 @@ this.emit('close'); | ||
_itemSetAcquired(item) { | ||
if (item.state !== ResourceState.ACQUIRED) { | ||
if (item.state !== definitions_js_1.ResourceState.ACQUIRED) { | ||
this._itemDetach(item); | ||
item.state = ResourceState.ACQUIRED; | ||
item.state = definitions_js_1.ResourceState.ACQUIRED; | ||
this._acquiredResources.push(item); | ||
@@ -401,3 +405,3 @@ item.acquiredNode = this._acquiredResources.tail; | ||
switch (item.state) { | ||
case ResourceState.IDLE: | ||
case definitions_js_1.ResourceState.IDLE: | ||
item.idleTime = 0; | ||
@@ -409,4 +413,4 @@ /* istanbul ignore next*/ | ||
break; | ||
case ResourceState.ACQUIRED: | ||
case ResourceState.VALIDATION: | ||
case definitions_js_1.ResourceState.ACQUIRED: | ||
case definitions_js_1.ResourceState.VALIDATION: | ||
/* istanbul ignore next*/ | ||
@@ -422,3 +426,3 @@ if (item.acquiredNode) | ||
_itemSetIdle(item, callback) { | ||
const isAcquired = item.state === ResourceState.ACQUIRED; | ||
const isAcquired = item.state === definitions_js_1.ResourceState.ACQUIRED; | ||
const handleCallback = (err) => { | ||
@@ -429,3 +433,3 @@ if (err) | ||
item.idleTime = Date.now(); | ||
item.state = ResourceState.IDLE; | ||
item.state = definitions_js_1.ResourceState.IDLE; | ||
if (this.options.fifo) { | ||
@@ -449,3 +453,3 @@ this._idleResources.push(item); | ||
const o = this._factory.reset(item.resource); | ||
promisify.await(o, handleCallback); | ||
putil_promisify_1.default.await(o, handleCallback); | ||
} | ||
@@ -476,3 +480,3 @@ catch (e) { | ||
const o = this._factory.destroy(item.resource); | ||
promisify.await(o, handleCallback); | ||
putil_promisify_1.default.await(o, handleCallback); | ||
} | ||
@@ -484,7 +488,7 @@ catch (e) { | ||
_itemValidate(item, callback) { | ||
item.state = ResourceState.VALIDATION; | ||
item.state = definitions_js_1.ResourceState.VALIDATION; | ||
try { | ||
const o = this._factory.validate?.(item.resource); | ||
// @ts-ignore | ||
promisify.await(o, callback); | ||
putil_promisify_1.default.await(o, callback); | ||
} | ||
@@ -497,1 +501,2 @@ catch (e) { | ||
} | ||
exports.Pool = Pool; |
@@ -1,5 +0,8 @@ | ||
import { ResourceState } from './definitions.js'; | ||
export class ResourceItem { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ResourceItem = void 0; | ||
const definitions_js_1 = require("./definitions.js"); | ||
class ResourceItem { | ||
constructor(resource) { | ||
this.state = ResourceState.IDLE; | ||
this.state = definitions_js_1.ResourceState.IDLE; | ||
this.idleTime = 0; | ||
@@ -10,1 +13,2 @@ this.destroyed = false; | ||
} | ||
exports.ResourceItem = ResourceItem; |
{ | ||
"name": "lightning-pool", | ||
"description": "Fastest generic Pool written with TypeScript", | ||
"version": "4.4.3", | ||
"version": "4.5.0", | ||
"author": "Panates Ltd.", | ||
"contributors": [ | ||
"Eray Hanoglu <e.hanoglu@panates.com>" | ||
], | ||
"license": "MIT", | ||
"repository": "panates/lightning-pool", | ||
"keywords": [ | ||
"pool", | ||
"generic", | ||
"generic-pool" | ||
], | ||
"dependencies": { | ||
@@ -21,10 +13,24 @@ "doublylinked": "^2.5.4", | ||
}, | ||
"type": "module", | ||
"main": "./cjs/index.js", | ||
"module": "./esm/index.js", | ||
"types": "./typings/index.d.ts", | ||
"types": "./types/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./cjs/index.js", | ||
"import": "./esm/index.js", | ||
"types": "./types/index.d.ts" | ||
} | ||
}, | ||
"contributors": [ | ||
"Eray Hanoglu <e.hanoglu@panates.com>" | ||
], | ||
"keywords": [ | ||
"pool", | ||
"generic", | ||
"generic-pool" | ||
], | ||
"files": [ | ||
"cjs/", | ||
"esm/", | ||
"typings/", | ||
"types/", | ||
"LICENSE", | ||
@@ -31,0 +37,0 @@ "README.md" |
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
68094
1560
24
No