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.
68 lines • 2.32 kB
TypeScript
import { EventEmitter } from "events";
import { CircularBuffer } from "./circular-buffer.js";
import type { IdPoolEvents, IdPoolOptions, PoolStats } from "./types.js";
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;
}
/**
* Enhanced BaseIdPool with improved error handling and state management
*/
export declare abstract class BaseIdPoolFixed extends EventEmitter implements IdPoolEventEmitter {
protected readonly options: Required<IdPoolOptions>;
protected readonly buffer: CircularBuffer<string>;
isRunning: boolean;
isReady: boolean;
protected stats: {
totalAcquired: number;
refillCount: number;
emptyHits: number;
refillTimes: number[];
};
private pendingAcquires;
constructor(options: IdPoolOptions);
/**
* 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>;
/**
* Start the pool and begin filling it.
*/
start(): void;
/**
* 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.
* Ensures all IDs are strings.
*/
protected generateIds(count: number): Promise<string[]>;
/**
* Add generated IDs to the buffer and emit refill event.
* Also resolves any pending async acquires.
*/
protected addToBuffer(ids: string[], duration: number): void;
}
//# sourceMappingURL=pool-fixes.d.ts.map