uid-pool
Version:
High-performance UUID and unique ID pooling for Node.js. Pre-generate IDs in background worker threads for O(1) synchronous acquisition. Drop-in replacement for uuid.v4() and nanoid() with 10-100x better performance under load.
67 lines • 2.37 kB
TypeScript
import { EventEmitter } from "events";
import { CircularBuffer } from "./circular-buffer.js";
import type { IdPoolOptions, PoolStats, IdPoolEvents } from "./types.js";
export declare const ACQUIRE_TIMEOUT = 10000;
export interface IdPoolEventEmitter extends EventEmitter {
on<K extends keyof IdPoolEvents>(event: K, listener: IdPoolEvents[K]): this;
emit<K extends keyof IdPoolEvents>(event: K, ...args: Parameters<IdPoolEvents[K]>): boolean;
}
/**
* Base implementation of IdPool with core functionality.
* Extended by platform-specific implementations (Node, Edge).
*/
export declare abstract class BaseIdPool extends EventEmitter implements IdPoolEventEmitter {
protected readonly options: Required<IdPoolOptions>;
protected readonly buffer: CircularBuffer<string>;
protected isRunning: boolean;
protected stats: {
totalAcquired: number;
refillCount: number;
emptyHits: number;
refillTimes: number[];
};
protected constructor(options: IdPoolOptions);
/**
* Initialize the pool and wait for it to be ready.
* Called by the static create() method.
*/
protected initialize(): Promise<void>;
/**
* Synchronously acquire an ID from the pool.
* Returns undefined if pool is empty (unless throwOnEmpty is true).
*/
acquire(): string | undefined;
/**
* Asynchronously acquire an ID, waiting for refill if necessary.
*/
acquireAsync(): Promise<string>;
/**
* Stop the pool and clean up resources.
*/
stop(): Promise<void>;
/**
* Get current pool statistics.
*/
getStats(): PoolStats;
/**
* Platform-specific implementation for starting the pool.
*/
protected abstract startInternal(): void;
/**
* Platform-specific implementation for stopping the pool.
*/
protected abstract stopInternal(): Promise<void>;
/**
* Platform-specific implementation for triggering a refill.
*/
protected abstract triggerRefill(): Promise<void>;
/**
* Helper method to generate IDs using the configured generator.
*/
protected generateIds(count: number): Promise<string[]>;
/**
* Add generated IDs to the buffer and emit refill event.
*/
protected addToBuffer(ids: string[], duration: number): void;
}
//# sourceMappingURL=pool.d.ts.map