export type ForEachCallback = (data: T, key: string, cache: TinyMapCache) => void; export type SetEventPayload = { /** * - The key that was set. */ key: string; /** * - The data that was set. */ data: T; }; export type GetEventPayload = { /** * - The key that was accessed. */ key: string; /** * - True if the key was found and is valid, false otherwise. */ hit: boolean; }; export type DeleteEventPayload = { /** * - The key that was removed. */ key: string; }; export type ExpireEventPayload = { /** * - The key that expired. */ key: string; /** * - The data that was removed due to expiration. */ data: T; }; export type PurgeEventPayload = { /** * - The number of items removed during purge. */ count: number; }; export type CacheEntry = { /** * - The actual data being stored. */ data: T; /** * - The Unix timestamp (ms) when the data was stored. */ timestamp: number; }; export type CacheMap = Map>; export type CacheObject = Object>; export type TinyMapCacheEvents = { /** * - Emitted when an item is stored. */ set: (payload: SetEventPayload) => void; /** * - Emitted when an item is removed. */ delete: (payload: DeleteEventPayload) => void; /** * - Emitted when an item expires. */ expire: (payload: ExpireEventPayload) => void; /** * - Emitted after a purge cycle removes items. */ purge: (payload: PurgeEventPayload) => void; /** * - Emitted when the cache is cleared. */ clear: () => void; }; export type TinyMapCacheEmitter = import('../../jsdoc/EventEmitter.mjs').ICustomEventEmitter>; /** * @template T * @callback ForEachCallback * @param {T} data - The value of the current entry. * @param {string} key - The key of the current entry. * @param {TinyMapCache} cache - The cache instance that is being iterated. * @returns {void} */ /** * @template T * @typedef {Object} SetEventPayload * @property {string} key - The key that was set. * @property {T} data - The data that was set. */ /** * @template T * @typedef {Object} GetEventPayload * @property {string} key - The key that was accessed. * @property {boolean} hit - True if the key was found and is valid, false otherwise. */ /** * @template T * @typedef {Object} DeleteEventPayload * @property {string} key - The key that was removed. */ /** * @template T * @typedef {Object} ExpireEventPayload * @property {string} key - The key that expired. * @property {T} data - The data that was removed due to expiration. */ /** * @typedef {Object} PurgeEventPayload * @property {number} count - The number of items removed during purge. */ /** * @template T * @typedef {Object} CacheEntry * @property {T} data - The actual data being stored. * @property {number} timestamp - The Unix timestamp (ms) when the data was stored. */ /** * A Map collection where keys are strings and values are CacheEntry objects. * @template T * @typedef {Map>} CacheMap */ /** * A plain object representation of the cache, where keys are strings and values are CacheEntry objects. * @template T * @typedef {Object.>} CacheObject */ /** * Event map for {@link TinyMapCache}. * @template T * @typedef {Object} TinyMapCacheEvents * @property {(payload: SetEventPayload) => void} set - Emitted when an item is stored. * @property {(payload: DeleteEventPayload) => void} delete - Emitted when an item is removed. * @property {(payload: ExpireEventPayload) => void} expire - Emitted when an item expires. * @property {(payload: PurgeEventPayload) => void} purge - Emitted after a purge cycle removes items. * @property {() => void} clear - Emitted when the cache is cleared. */ /** * @template T * @typedef {import('../../jsdoc/EventEmitter.mjs').ICustomEventEmitter>} TinyMapCacheEmitter */ /** @type {new () => TinyMapCacheEmitter} */ declare const TypedEventEmitter: new () => TinyMapCacheEmitter; /** * @template {any} T * In-memory cache manager to prevent duplicate requests. */ declare class TinyMapCache extends TypedEventEmitter { #private; /** * A collection of all active `TinyMapCache` instances. * This collection is used to facilitate cascaded operations (like `purgeExpired`) across all existing cache instances. * @type {Array>} */ static get activeInstances(): Array>; /** * Returns the total number of active `TinyMapCache` instances currently being tracked. * @returns {number} The count of active instances. */ static get activeInstancesSize(): number; constructor(); /** * Gets the current Time-To-Live (TTL) in milliseconds. * @returns {number} */ get ttl(): number; /** * Sets the Time-To-Live (TTL) in milliseconds. * @param {number} value - The new TTL value. * @throws {TypeError} If the value is not a number. * @throws {RangeError} If the value is less than 0. */ set ttl(value: number); /** * Returns the number of items currently stored in the cache. * @returns {number} */ get size(): number; /** * Checks if a specific key exists in the cache. * * Note: This method triggers a full purge of all expired items in the cache. * @param {string} key - The identifier for the data. * @returns {boolean} True if the key exists, false otherwise. * @throws {TypeError} If the key is not a string. */ has(key: string): boolean; /** * Removes the item associated with the specified key from the cache. * * Note: This method triggers a full purge of all expired items in the cache. * @param {string} key - The identifier for the data. * @returns {boolean} True if an element in the Map existed and has been removed, false otherwise. * @throws {TypeError} If the key is not a string. */ delete(key: string): boolean; /** * Saves an item to the cache. * * Note: This method triggers a full purge of all expired items in the cache. * @param {string} key - The identifier for the data. * @param {T} data - The data to be stored. * @throws {TypeError} If the key is not a string. */ set(key: string, data: T): void; /** * Retrieves an item if it is still valid. * * Note: This method triggers a full purge of all expired items in the cache. * @param {string} key - The identifier for the data. * @returns {T | null} The data if valid, or null if expired/not found. * @throws {TypeError} If the key is not a string. */ get(key: string): T | null; /** * Returns the string tag used by `Object.prototype.toString`. * @returns {string} The string tag for this class. */ get [Symbol.toStringTag](): string; /** * Returns a new iterator that yields the keys of all valid (non-expired) entries. * * Note: This method triggers a full purge of all expired items in the cache. * @returns {IterableIterator} An iterator over the cache keys. */ keys(): IterableIterator; /** * Returns a new iterator that yields the values of all valid (non-expired) entries. * * Note: This method triggers a full purge of all expired items in the cache. * @returns {IterableIterator} An iterator over the cache values. */ values(): IterableIterator; /** * Returns a new iterator that yields the `[key, value]` pairs of all valid (non-expired) entries. * * Note: This method triggers a full purge of all expired items in the cache. * @returns {IterableIterator<[string, T]>} An iterator over the cache entries. */ entries(): IterableIterator<[string, T]>; /** * Returns a new iterator over the `[key, value]` pairs of all valid (non-expired) entries. * Alias for `entries()`, enabling `for...of` and spread syntax. * * Note: This method triggers a full purge of all expired items in the cache. * @returns {IterableIterator<[string, T]>} An iterator over the cache entries. */ [Symbol.iterator](): IterableIterator<[string, T]>; /** * Executes a provided function once for each valid (non-expired) entry. * * Note: This method triggers a full purge of all expired items in the cache. * @param {ForEachCallback} callbackFn - The function to execute for each entry. * @param {any} [thisArg] - The value to use as `this` when executing `callbackFn`. * @returns {void} * @throws {TypeError} If the callback is not a function. */ forEach(callbackFn: ForEachCallback, thisArg?: any): void; /** * Iterates through the current cache and removes all entries that have exceeded the TTL. * @param {boolean} [clearAll=true] - If true, triggers a purge on all other active cache instances. * @returns {void} */ purgeExpired(clearAll?: boolean): void; /** * Removes all items from the cache. * @returns {void} */ clear(): void; } export default TinyMapCache; //# sourceMappingURL=TinyMapCache.d.mts.map