lightning-pool 4.10.1 → 4.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/index.js CHANGED
@@ -4,8 +4,9 @@ exports.createPool = createPool;
4
4
  const tslib_1 = require("tslib");
5
5
  const pool_js_1 = require("./pool.js");
6
6
  tslib_1.__exportStar(require("./abort-error.js"), exports);
7
- tslib_1.__exportStar(require("./definitions.js"), exports);
7
+ tslib_1.__exportStar(require("./constants.js"), exports);
8
8
  tslib_1.__exportStar(require("./pool.js"), exports);
9
+ tslib_1.__exportStar(require("./types.js"), exports);
9
10
  function createPool(factory, config) {
10
11
  return new pool_js_1.Pool(factory, config);
11
12
  }
package/cjs/pool.js CHANGED
@@ -6,7 +6,7 @@ const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
6
6
  const events_1 = require("events");
7
7
  const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify"));
8
8
  const abort_error_js_1 = require("./abort-error.js");
9
- const definitions_js_1 = require("./definitions.js");
9
+ const constants_js_1 = require("./constants.js");
10
10
  const pool_options_js_1 = require("./pool-options.js");
11
11
  const pool_request_js_1 = require("./pool-request.js");
12
12
  const resource_item_js_1 = require("./resource-item.js");
@@ -19,7 +19,7 @@ class Pool extends events_1.EventEmitter {
19
19
  this._idleResources = new doublylinked_1.default();
20
20
  this._creating = 0;
21
21
  this._requestsProcessing = 0;
22
- this._state = definitions_js_1.PoolState.IDLE;
22
+ this._state = constants_js_1.PoolState.IDLE;
23
23
  if (typeof factory !== 'object') {
24
24
  throw new TypeError('You must provide `factory` object');
25
25
  }
@@ -94,12 +94,12 @@ class Pool extends events_1.EventEmitter {
94
94
  * Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called
95
95
  */
96
96
  start() {
97
- if (this._state === definitions_js_1.PoolState.STARTED)
97
+ if (this._state === constants_js_1.PoolState.STARTED)
98
98
  return;
99
- if (this._state >= definitions_js_1.PoolState.CLOSING) {
99
+ if (this._state >= constants_js_1.PoolState.CLOSING) {
100
100
  throw new Error('Closed pool can not be started again');
101
101
  }
102
- this._state = definitions_js_1.PoolState.STARTED;
102
+ this._state = constants_js_1.PoolState.STARTED;
103
103
  this._setHouseKeep(this.options.houseKeepInterval);
104
104
  this._ensureMin();
105
105
  this.emit('start');
@@ -116,17 +116,17 @@ class Pool extends events_1.EventEmitter {
116
116
  if (!callback) {
117
117
  return putil_promisify_1.default.fromCallback(cb => this.close(terminateWait, cb));
118
118
  }
119
- if (this._state === definitions_js_1.PoolState.CLOSED || this._state === definitions_js_1.PoolState.IDLE) {
119
+ if (this._state === constants_js_1.PoolState.CLOSED || this._state === constants_js_1.PoolState.IDLE) {
120
120
  return callback();
121
121
  }
122
- if (this._state === definitions_js_1.PoolState.CLOSING) {
122
+ if (this._state === constants_js_1.PoolState.CLOSING) {
123
123
  this.once('close', callback);
124
124
  return;
125
125
  }
126
126
  this.emit('closing');
127
127
  if (this._houseKeepTimer)
128
128
  clearTimeout(this._houseKeepTimer);
129
- this._state = definitions_js_1.PoolState.CLOSING;
129
+ this._state = constants_js_1.PoolState.CLOSING;
130
130
  this._requestQueue.forEach(t => t.stopTimout());
131
131
  this._requestQueue = new doublylinked_1.default();
132
132
  this._requestsProcessing = 0;
@@ -175,7 +175,7 @@ class Pool extends events_1.EventEmitter {
175
175
  */
176
176
  release(resource, callback) {
177
177
  const item = this._allResources.get(resource);
178
- if (item && item.state !== definitions_js_1.ResourceState.IDLE) {
178
+ if (item && item.state !== constants_js_1.ResourceState.IDLE) {
179
179
  this._itemSetIdle(item, callback);
180
180
  }
181
181
  this._processNextRequest();
@@ -221,7 +221,7 @@ class Pool extends events_1.EventEmitter {
221
221
  return this._allResources.has(resource);
222
222
  }
223
223
  _processNextRequest() {
224
- if (this._state !== definitions_js_1.PoolState.STARTED ||
224
+ if (this._state !== constants_js_1.PoolState.STARTED ||
225
225
  this._requestsProcessing >= this.options.max - this.acquired) {
226
226
  return;
227
227
  }
@@ -235,7 +235,7 @@ class Pool extends events_1.EventEmitter {
235
235
  try {
236
236
  if (item) {
237
237
  /* istanbul ignore next : Hard to simulate */
238
- if (this._state !== definitions_js_1.PoolState.STARTED) {
238
+ if (this._state !== constants_js_1.PoolState.STARTED) {
239
239
  this._itemDestroy(item);
240
240
  return;
241
241
  }
@@ -256,9 +256,9 @@ class Pool extends events_1.EventEmitter {
256
256
  if (item) {
257
257
  /* Validate resource */
258
258
  if (this.options.validation && this._factory.validate) {
259
- this._itemValidate(item, (err) => {
259
+ this._itemValidate(item, (err, result) => {
260
260
  /* Destroy resource on validation error */
261
- if (err) {
261
+ if (err || result === false) {
262
262
  this._itemDestroy(item);
263
263
  this.emit('validate-error', err, item.resource);
264
264
  this._requestsProcessing--;
@@ -338,13 +338,13 @@ class Pool extends events_1.EventEmitter {
338
338
  if (this._houseKeepTimer)
339
339
  clearInterval(this._houseKeepTimer);
340
340
  this._houseKeepTimer = undefined;
341
- if ((ms > 0 && this.state === definitions_js_1.PoolState.STARTED) ||
342
- this.state === definitions_js_1.PoolState.CLOSING) {
341
+ if ((ms > 0 && this.state === constants_js_1.PoolState.STARTED) ||
342
+ this.state === constants_js_1.PoolState.CLOSING) {
343
343
  this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
344
344
  }
345
345
  }
346
346
  _houseKeep() {
347
- const isClosing = this._state === definitions_js_1.PoolState.CLOSING;
347
+ const isClosing = this._state === constants_js_1.PoolState.CLOSING;
348
348
  const now = Date.now();
349
349
  let m = this._allResources.size - this.options.min;
350
350
  let n = this._idleResources.length - this.options.minIdle;
@@ -362,7 +362,7 @@ class Pool extends events_1.EventEmitter {
362
362
  if (this._allResources.size)
363
363
  return;
364
364
  clearInterval(this._houseKeepTimer);
365
- this._state = definitions_js_1.PoolState.CLOSED;
365
+ this._state = constants_js_1.PoolState.CLOSED;
366
366
  this._requestsProcessing = 0;
367
367
  this.emit('close');
368
368
  }
@@ -375,24 +375,24 @@ class Pool extends events_1.EventEmitter {
375
375
  });
376
376
  }
377
377
  _itemSetAcquired(item) {
378
- if (item.state !== definitions_js_1.ResourceState.ACQUIRED) {
378
+ if (item.state !== constants_js_1.ResourceState.ACQUIRED) {
379
379
  this._itemDetach(item);
380
- item.state = definitions_js_1.ResourceState.ACQUIRED;
380
+ item.state = constants_js_1.ResourceState.ACQUIRED;
381
381
  this._acquiredResources.push(item);
382
382
  item.acquiredNode = this._acquiredResources.tail;
383
383
  }
384
384
  }
385
385
  _itemDetach(item) {
386
386
  switch (item.state) {
387
- case definitions_js_1.ResourceState.IDLE:
387
+ case constants_js_1.ResourceState.IDLE:
388
388
  item.idleTime = 0;
389
389
  /* istanbul ignore next*/
390
390
  if (item.idleNode)
391
391
  item.idleNode.remove();
392
392
  item.idleNode = undefined;
393
393
  break;
394
- case definitions_js_1.ResourceState.ACQUIRED:
395
- case definitions_js_1.ResourceState.VALIDATION:
394
+ case constants_js_1.ResourceState.ACQUIRED:
395
+ case constants_js_1.ResourceState.VALIDATION:
396
396
  /* istanbul ignore next*/
397
397
  if (item.acquiredNode)
398
398
  item.acquiredNode.remove();
@@ -403,13 +403,13 @@ class Pool extends events_1.EventEmitter {
403
403
  }
404
404
  }
405
405
  _itemSetIdle(item, callback) {
406
- const isAcquired = item.state === definitions_js_1.ResourceState.ACQUIRED;
406
+ const isAcquired = item.state === constants_js_1.ResourceState.ACQUIRED;
407
407
  const handleCallback = (err) => {
408
408
  if (err)
409
409
  return this._itemDestroy(item, callback);
410
410
  this._itemDetach(item);
411
411
  item.idleTime = Date.now();
412
- item.state = definitions_js_1.ResourceState.IDLE;
412
+ item.state = constants_js_1.ResourceState.IDLE;
413
413
  if (this.options.fifo) {
414
414
  this._idleResources.push(item);
415
415
  item.idleNode = this._idleResources.tail;
@@ -461,15 +461,14 @@ class Pool extends events_1.EventEmitter {
461
461
  }
462
462
  }
463
463
  _itemValidate(item, callback) {
464
- item.state = definitions_js_1.ResourceState.VALIDATION;
464
+ item.state = constants_js_1.ResourceState.VALIDATION;
465
465
  try {
466
466
  const o = this._factory.validate?.(item.resource);
467
467
  // @ts-ignore
468
468
  putil_promisify_1.default.await(o, callback);
469
469
  }
470
470
  catch (e) {
471
- if (callback)
472
- callback(e);
471
+ callback?.(e);
473
472
  }
474
473
  }
475
474
  }
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ResourceItem = void 0;
4
- const definitions_js_1 = require("./definitions.js");
4
+ const constants_js_1 = require("./constants.js");
5
5
  class ResourceItem {
6
6
  constructor(resource) {
7
- this.state = definitions_js_1.ResourceState.IDLE;
7
+ this.state = constants_js_1.ResourceState.IDLE;
8
8
  this.idleTime = 0;
9
9
  this.destroyed = false;
10
10
  this.resource = resource;
@@ -1 +1 @@
1
- {"root":["../../src/abort-error.ts","../../src/definitions.ts","../../src/index.ts","../../src/pool-options.ts","../../src/pool-request.ts","../../src/pool.ts","../../src/resource-item.ts"],"version":"5.7.3"}
1
+ {"root":["../../src/abort-error.ts","../../src/constants.ts","../../src/index.ts","../../src/pool-options.ts","../../src/pool-request.ts","../../src/pool.ts","../../src/resource-item.ts","../../src/types.ts"],"version":"5.8.3"}
package/cjs/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/esm/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { Pool } from './pool.js';
2
2
  export * from './abort-error.js';
3
- export * from './definitions.js';
3
+ export * from './constants.js';
4
4
  export * from './pool.js';
5
+ export * from './types.js';
5
6
  export function createPool(factory, config) {
6
7
  return new Pool(factory, config);
7
8
  }
package/esm/pool.js CHANGED
@@ -2,7 +2,7 @@ import DoublyLinked from 'doublylinked';
2
2
  import { EventEmitter } from 'events';
3
3
  import promisify from 'putil-promisify';
4
4
  import { AbortError } from './abort-error.js';
5
- import { PoolState, ResourceState, } from './definitions.js';
5
+ import { PoolState, ResourceState } from './constants.js';
6
6
  import { PoolOptions } from './pool-options.js';
7
7
  import { PoolRequest } from './pool-request.js';
8
8
  import { ResourceItem } from './resource-item.js';
@@ -252,9 +252,9 @@ export class Pool extends EventEmitter {
252
252
  if (item) {
253
253
  /* Validate resource */
254
254
  if (this.options.validation && this._factory.validate) {
255
- this._itemValidate(item, (err) => {
255
+ this._itemValidate(item, (err, result) => {
256
256
  /* Destroy resource on validation error */
257
- if (err) {
257
+ if (err || result === false) {
258
258
  this._itemDestroy(item);
259
259
  this.emit('validate-error', err, item.resource);
260
260
  this._requestsProcessing--;
@@ -464,8 +464,7 @@ export class Pool extends EventEmitter {
464
464
  promisify.await(o, callback);
465
465
  }
466
466
  catch (e) {
467
- if (callback)
468
- callback(e);
467
+ callback?.(e);
469
468
  }
470
469
  }
471
470
  }
@@ -1,4 +1,4 @@
1
- import { ResourceState } from './definitions.js';
1
+ import { ResourceState } from './constants.js';
2
2
  export class ResourceItem {
3
3
  constructor(resource) {
4
4
  this.state = ResourceState.IDLE;
@@ -1 +1 @@
1
- {"root":["../../src/abort-error.ts","../../src/definitions.ts","../../src/index.ts","../../src/pool-options.ts","../../src/pool-request.ts","../../src/pool.ts","../../src/resource-item.ts"],"version":"5.7.3"}
1
+ {"root":["../../src/abort-error.ts","../../src/constants.ts","../../src/index.ts","../../src/pool-options.ts","../../src/pool-request.ts","../../src/pool.ts","../../src/resource-item.ts","../../src/types.ts"],"version":"5.8.3"}
package/esm/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "lightning-pool",
3
3
  "description": "Fastest generic Pool written with TypeScript",
4
- "version": "4.10.1",
4
+ "version": "4.11.0",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
@@ -29,7 +29,7 @@
29
29
  "types": "./types/index.d.ts",
30
30
  "repository": {
31
31
  "type": "git",
32
- "url": "https://github.com/panates/lightning-pool.git"
32
+ "url": "git+https://github.com/panates/lightning-pool.git"
33
33
  },
34
34
  "contributors": [
35
35
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -0,0 +1,11 @@
1
+ export declare enum PoolState {
2
+ IDLE = 0,
3
+ STARTED = 1,
4
+ CLOSING = 2,
5
+ CLOSED = 3
6
+ }
7
+ export declare enum ResourceState {
8
+ IDLE = 0,
9
+ ACQUIRED = 1,
10
+ VALIDATION = 2
11
+ }
package/types/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
- import { PoolConfiguration, PoolFactory } from './definitions.js';
2
1
  import { Pool } from './pool.js';
2
+ import { PoolConfiguration, PoolFactory } from './types.js';
3
3
  export * from './abort-error.js';
4
- export * from './definitions.js';
4
+ export * from './constants.js';
5
5
  export * from './pool.js';
6
+ export * from './types.js';
6
7
  export declare function createPool<T = any>(factory: PoolFactory<T>, config?: PoolConfiguration): Pool<T>;
package/types/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { PoolConfiguration, PoolFactory } from './definitions.js';
2
1
  import { Pool } from './pool.js';
2
+ import { PoolConfiguration, PoolFactory } from './types.js';
3
3
  export * from './abort-error.js';
4
- export * from './definitions.js';
4
+ export * from './constants.js';
5
5
  export * from './pool.js';
6
+ export * from './types.js';
6
7
  export declare function createPool<T = any>(factory: PoolFactory<T>, config?: PoolConfiguration): Pool<T>;
@@ -1,6 +1,6 @@
1
1
  import { EventEmitter } from 'events';
2
- import { PoolConfiguration } from './definitions.js';
3
- import { Pool } from './pool.js';
2
+ import type { Pool } from './pool.js';
3
+ import type { PoolConfiguration } from './types.js';
4
4
  export declare class PoolOptions extends EventEmitter {
5
5
  readonly pool: Pool;
6
6
  private _acquireMaxRetries;
@@ -1,5 +1,5 @@
1
- import { Callback } from './definitions.js';
2
- import { Pool } from './pool.js';
1
+ import type { Pool } from './pool.js';
2
+ import type { Callback } from './types.js';
3
3
  export declare class PoolRequest {
4
4
  created: number;
5
5
  callback: Callback;
package/types/pool.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { EventEmitter } from 'events';
2
- import { Callback, PoolConfiguration, PoolFactory, PoolState } from './definitions.js';
2
+ import { PoolState } from './constants.js';
3
3
  import { PoolOptions } from './pool-options.js';
4
+ import type { Callback, PoolConfiguration, PoolFactory } from './types.js';
4
5
  export declare class Pool<T = any> extends EventEmitter {
5
6
  private readonly _options;
6
7
  private readonly _factory;
@@ -1,5 +1,5 @@
1
- import { DoublyLinked } from 'doublylinked';
2
- import { ResourceState } from './definitions.js';
1
+ import type { DoublyLinked } from 'doublylinked';
2
+ import { ResourceState } from './constants.js';
3
3
  export declare class ResourceItem<T> {
4
4
  state: ResourceState;
5
5
  acquiredNode?: DoublyLinked.Node<ResourceItem<T>>;
@@ -1,15 +1,4 @@
1
1
  export type Callback = (e?: unknown, ...args: any[]) => void;
2
- export declare enum PoolState {
3
- IDLE = 0,
4
- STARTED = 1,
5
- CLOSING = 2,
6
- CLOSED = 3
7
- }
8
- export declare enum ResourceState {
9
- IDLE = 0,
10
- ACQUIRED = 1,
11
- VALIDATION = 2
12
- }
13
2
  export interface PoolFactory<T = any> {
14
3
  create(info?: {
15
4
  tries: number;
@@ -17,7 +6,7 @@ export interface PoolFactory<T = any> {
17
6
  }): Promise<T> | T;
18
7
  destroy(resource: T): Promise<void> | void;
19
8
  reset?(resource: T): Promise<void> | void;
20
- validate?(resource: T): Promise<void> | void;
9
+ validate?(resource: T): Promise<void | boolean> | void;
21
10
  }
22
11
  export interface PoolConfiguration {
23
12
  acquireMaxRetries?: number;
File without changes
File without changes