UNPKG

lightning-pool

Version:

Fastest generic Pool written with TypeScript

105 lines (104 loc) 3.21 kB
import { EventEmitter } from 'events'; import { PoolState } from './constants.js'; import { PoolOptions } from './pool-options.js'; import type { Callback, PoolConfiguration, PoolFactory } from './types.js'; export declare class Pool<T = any> extends EventEmitter { private readonly _options; private readonly _factory; private _requestQueue; private _allResources; private _acquiredResources; private _idleResources; private _creating; private _requestsProcessing; private _state; private _houseKeepTimer; private _closeWaitTimer; constructor(factory: PoolFactory<T>, config?: PoolConfiguration); /** * Returns Pool options */ get options(): PoolOptions; /** * Returns number of resources that are currently acquired */ get acquired(): number; /** * Returns number of unused resources in the pool */ get available(): number; /** * Returns number of resources currently creating */ get creating(): number; /** * Returns number of callers waiting to acquire a resource */ get pending(): number; /** * Returns number of resources in the pool * regardless of whether they are idle or in use */ get size(): number; /** * Returns state of the pool */ get state(): PoolState; /** * Starts the pool and begins creating of resources, starts house keeping and any other internal logic. * Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called */ start(): void; /** * Shuts down the pool and destroys all resources. */ close(callback?: Callback): void; close(terminateWait: number, callback?: Callback): void; close(force: boolean, callback?: Callback): void; closeAsync(): Promise<void>; closeAsync(terminateWait: number): Promise<void>; closeAsync(force: boolean): Promise<void>; /** * Acquires `resource` from the pool or create a new one */ acquire(): Promise<T>; acquire(callback: Callback): void; /** * Releases an allocated `resource` and let it back to pool. */ release(resource: T, callback?: Callback): void; /** * Async version of release(). */ releaseAsync(resource: T): Promise<void>; /** * Releases, destroys and removes any `resource` from `Pool`. */ destroy(resource: T, callback?: Callback): any; /** * Async version of destroy(). */ destroyAsync(resource: T): Promise<void>; /** * Returns if a `resource` has been acquired from the pool and not yet released or destroyed. */ isAcquired(resource: T): boolean; /** * Returns if the pool contains a `resource` */ includes(resource: T): boolean; private _processNextRequest; emit(event: string | symbol, ...args: any[]): boolean; /** * Creates new resource */ private _createResource; private _setHouseKeep; private _houseKeep; private _ensureMin; private _itemSetAcquired; private _itemDetach; private _itemSetIdle; private _itemDestroy; private _itemValidate; }