UNPKG

@openally/timestore

Version:

An abstract class designed to manage the Time To Live (TTL) of a given list of identifiers.

69 lines (65 loc) 2.18 kB
import { EventEmitter } from 'node:events'; declare const TSV_SYMBOL: unique symbol; type tSvResponse<T = any> = { value: T; ttl: number | undefined; [TSV_SYMBOL]: boolean; }; declare function tSv<T = any>(options?: TimeStoreAddOptions): (value: T) => tSvResponse<T>; interface TimeStoreConstructorOptions { /** * Time To Live (Lifetime of stored identifiers). */ ttl?: number; /** * Automatically expire identifiers when Node.js process "exit" event is triggered. * * @see https://nodejs.org/api/process.html#event-exit * @default false */ expireIdentifiersOnProcessExit?: boolean; /** * Provide an additional EventEmitter to use for broadcasting events */ eventEmitter?: EventEmitter; /** * If enabled the internal timer will not be unreferenced * * @see https://nodejs.org/dist/latest-v18.x/docs/api/timers.html#timeoutunref * @default false */ keepEventLoopAlive?: boolean; } interface TimeStoreAddOptions { /** * Time To Live for the given identifier. * If no value provided it will take the class TTL value. */ ttl?: number; /** * If identifier exist then keep is original timestamp and ttl. * * @default false */ keepIdentifierBirthTTL?: boolean; } type TimeStoreIdentifier = string | symbol | number | boolean | bigint | object | null; type TimeStoreMapValue = { timestamp: number; ttl: number; }; declare class TimeStore extends EventEmitter { #private; static Expired: symbol; static Renewed: symbol; constructor(options?: TimeStoreConstructorOptions); get ttl(): number; get size(): number; addTsv(data: tSvResponse): this; add(identifier: TimeStoreIdentifier, options?: TimeStoreAddOptions): this; delete(identifier: TimeStoreIdentifier): this; clear(): this; has(identifier: TimeStoreIdentifier): boolean; get(identifier: TimeStoreIdentifier): TimeStoreMapValue | null; } export { TSV_SYMBOL, TimeStore, type TimeStoreAddOptions, type TimeStoreConstructorOptions, type TimeStoreIdentifier, type TimeStoreMapValue, tSv, type tSvResponse };