type ListenerMap any> = Partial< Record>> >; declare class CacheEventEmitter any> { /** * The list of [l]isteners for the given [t]ype. */ l: ListenerMap; /** * The [c]ache the emitter is associated with. */ private c; constructor(cache: Cache); /** * Method to [a]dd a listener for the given cache change event. */ a(type: Type, listener: CacheEventListener): void; /** * Method to [n]otify all listeners for the given cache change event. */ n(type: CacheEventType, node: CacheNode, reason?: string): void; /** * Method to [r]emove a listener for the given cache change event. */ r(type: Type, listener: CacheEventListener): void; } declare class Cache any> { /** * The current [c]ount of entries in the cache. */ c: number; /** * Whether the entire key is [e]qual to an existing key in cache. */ e: IsKeyEqual; /** * The [h]ead of the cache linked list. */ h: CacheNode | undefined; /** * The transformer for the [k]ey stored in cache. */ k: Options['transformKey'] | undefined; /** * Event emitter for `[o]`n events. */ o: CacheEventEmitter | undefined; /** * Whether to await the [p]romise returned by the function. */ p: Options['async']; /** * The maximum [s]ize of the cache. */ s: number; /** * The [t]ail of the cache linked list. */ t: CacheNode | undefined; constructor(options: Options); /** * The size of the populated cache. */ get size(): number; /** * The [key, value] pairs for the existing entries in cache. */ get snapshot(): CacheSnapshot; /** * Clear the cache. */ clear(reason?: string): void; /** * Delete the entry for the key based on the given `args` in cache. */ delete(args: Parameters, reason?: string): boolean; /** * Get the value in cache based on the given `args`. */ get(args: Parameters, reason?: string): ReturnType | undefined; /** * Determine whether the given `args` have a related entry in the cache. */ has(args: Parameters): boolean; /** * Remove the given `listener` for the given `type` of cache event. */ off(type: Type, listener: CacheEventListener): void; /** * Add the given `listener` for the given `type` of cache event. */ on(type: Type, listener: CacheEventListener): void; /** * Add or update the cache entry for the given `key`. */ set(key: Parameters, value: ReturnType, reason?: string): void; /** * Method to [d]elete the given `node` from the cache. */ d(node: CacheNode, reason: string): void; /** * Method to [g]et an existing node from cache based on the given `key`. */ g(key: Key): CacheNode | undefined; /** * Method to create a new [n]ode and set it at the head of the linked list. */ n(key: Key, value: ReturnType, reason?: string): CacheNode; /** * Method to [u]date the location of the given `node` in cache. */ u(node: CacheNode, reason: string | undefined, hit: boolean): void; /** * Method to [w]rap the promise in a handler to automatically delete the * entry if it rejects. */ w(node: CacheNode): ReturnType; } declare class ExpirationManager any> { /** * The [c]ache being monitored. */ c: Cache; /** * Map of [e]xpiration timeouts. */ e: Map; /** * Whether the entry in cache should [p]ersist, and therefore not * have any expiration. */ p: ShouldPersist | undefined; /** * Whether the entry in cache should be [r]emoved on expiration. */ r: ShouldRemoveOnExpire | undefined; /** * The [t]ime to wait before expiring, or a method that determines that time. */ t: number | GetExpires; /** * Whether the expiration should [u]pdate when the cache entry is hit. */ u: boolean; constructor(cache: Cache, expires: Required>['expires']); get size(): number; /** * Whether the cache expiration should be set [a]gain, generally after some cache change. */ a(key: Key, value: ReturnType): boolean; /** * Method to [d]elete the expiration. */ d(key: Key): void; /** * Method to [s]et the new expiration. If one is present for the given `key`, it will delete * the existing expiration before creating the new one. */ s(key: Key, value: ReturnType): void; } interface ProfileCounts { c: number; h: number; } declare class StatsManager any> { /** * The [c]ache listened to when collecting counts. */ c: Cache; /** * Method to [d]elete existing cache listeners. */ d: (() => void) | undefined; /** * The [n]ame of the profile to manage in stats. */ n: string; /** * The counts for the stats [p]rofile. */ p: ProfileCounts; constructor(cache: Cache, statsName: string); /** * Method to compute the [m]etrics for the profile stats. */ m(): ProfileStats; /** * Method to [r]eset the counts. */ r(): void; /** * Method to [s]tart the collection of stats for the given profile. */ s(): void; } /** * Clear all existing stats stored, either of the specific profile whose name is passed, * or globally if no name is passed. */ declare function clearStats(statsName?: string): void; /** * Get the stats of a given profile, or global stats if no `statsName` is given. */ declare function getStats( statsName?: Name, ): undefined extends Name ? GlobalStats | undefined : ProfileStats | undefined; /** * Whether stats are currently being collected. */ declare function isCollectingStats(): boolean; /** * Start collecting stats. */ declare function startCollectingStats(): void; /** * Stop collecting stats. */ declare function stopCollectingStats(): void; /** * Key used for cache entries. */ type Key = any[]; /** * A single argument in a cache key. */ type Arg = Key[number]; /** * The internal cache node used in the cache's linked list. */ interface CacheNode any> { /** * The [n]ext node in the cache order. */ n: CacheNode | undefined; /** * The [p]revious node in the cache order. */ p: CacheNode | undefined; /** * If present, the node has been [r]emovd from cache. */ r?: true; /** * The [k]ey for the given node in cache. */ k: Key; /** * The cached [v]alue returned from the function call. */ v: ReturnType; } /** * The type of cache event fired. */ type CacheEventType = 'add' | 'delete' | 'hit' | 'update'; interface CacheEventBase any> { /** * The cache associated with the given memoized function. */ cache: Cache; /** * The key of the affected node. */ key: Key; /** * The reason (if any) the operation was performed on the node. */ reason?: string; /** * The value of the affected node. */ value: ReturnType; /** * The type of operation performed on the node. */ type: CacheEventType; } /** * Cache event fired when a new entry is added. */ interface OnAddEvent any> extends CacheEventBase { type: 'add'; } /** * Cache event fired when an existing entry is deleted. */ interface OnDeleteEvent any> extends CacheEventBase { type: 'delete'; } /** * Cache event fired when the topmost entry in cache is retrieved. */ interface OnHitEvent any> extends CacheEventBase { type: 'hit'; } /** * Cache event fired when an existing entry in cache is updated. This * can be either updating the recency of an older entry in cache or * the resolution / rejection of an async entry. */ interface OnUpdateEvent any> extends CacheEventBase { type: 'update'; } /** * Cache event fired when a cache change occurs. */ type CacheEvent any> = Type extends 'add' ? OnAddEvent : Type extends 'delete' ? OnDeleteEvent : Type extends 'hit' ? OnHitEvent : Type extends 'update' ? OnUpdateEvent : never; /** * A listener for the given type of cache event. */ type CacheEventListener any> = ( event: CacheEvent, ) => void; /** * Method use to trigger a forced update of cache. */ type ForceUpdate any> = (args: Parameters) => boolean; /** * Method to retrieve the expiration duration in milliseconds based on * the values in cache. */ type GetExpires any> = (key: Key, value: ReturnType, cache: Cache) => number; /** * Method to determine if two complete keys are equal. */ type IsKeyEqual = (cachedKey: Key, nextKey: Key) => boolean; /** * Method to determine if individual key items are equal. */ type IsKeyItemEqual = (cachedKeyItem: Arg, nextKeyItem: Arg, index: number) => boolean; /** * Method to serialize the key into a stringified key. */ type Serializer = (key: Key) => [string]; /** * Method to determine whether the cache entry should not expire. */ type ShouldPersist any> = (key: Key, value: ReturnType, cache: Cache) => boolean; /** * Method to determine whether the cache entry should be removed on expire, or * start a new expiration period. */ type ShouldRemoveOnExpire any> = ( key: Key, value: ReturnType, time: number, cache: Cache, ) => boolean; /** * Method to transform the arguments passed into a custom key format. */ type TransformKey any> = (args: Parameters) => Key; /** * Advanced configuration for the `expires` option. */ interface ExpiresConfig any> { /** * The amount of time before the cache entry is automatically removed. */ after: number | GetExpires; /** * Determine whether the cache entry should never expire. */ shouldPersist?: ShouldPersist; /** * Determine whether the cache entry should be removed upon expiration. * If `false` is returned, a new expiration is generated (not persistent). */ shouldRemove?: ShouldRemoveOnExpire; /** * Whether the cache entry expiration should be reset upon being hit. */ update?: boolean; } /** * Statistics object for a specific `statsName` profile. */ interface ProfileStats { calls: number; hits: number; name: string; usage: string; } /** * Statistics for all possible profiles who have stats collected. */ interface GlobalStats { calls: number; hits: number; profiles: Record; usage: string; } interface OptionsBase any> { /** * Whether the result of calling the function is a promise. This * will automatically remove the entry from cache if the promise is * rejected to avoid caching error states. */ async?: boolean; /** * Whether the entry in cache should automatically remove itself * after a period of time. */ expires?: number | GetExpires | ExpiresConfig; /** * Method to determine whether to bypass the cache to force an update * of the underlying entry based on new results. * * This should only be necessary if the memoized function is not * deterministic due to side-effects. */ forceUpdate?: ForceUpdate; /** * Whether the two keys are equal in value. This is used to compare * the key the function is called with against a given cache key to * determine whether the cached entry can be used. * * @note * If provided, the `isKeyItemEqual` option will be ignored. */ isKeyEqual?: IsKeyEqual; /** * Whether the two args are equal in value. This is used to compare * specific arguments in order for a cached key versus the key the * function is called with to determine whether the cached entry * can be used. * * @default `Object.is` * * @note * This option will be ignored if the `isKeyEqual` option is provided. */ isKeyItemEqual?: 'deep' | 'shallow' | IsKeyItemEqual; /** * The maximum number of args to consider for caching. */ maxArgs?: number; /** * The maximum number of entries to store in cache. * @default 1 */ maxSize?: number; /** * Whether to serialize the arguments into a string value for cache * purposes. A custom serializer can also be provided, if the default * one is insufficient. * * This can potentially be faster than `isKeyItemEqual: 'deep'` in rare * cases, but can also be used to provide a deep equal check that handles * circular references. */ serialize?: boolean | Serializer; /** * The name to give this method when recording profiling stats. */ statsName?: string; /** * Transform the parameters passed into a custom key for storage in * cache. */ transformKey?: TransformKey; } interface OptionsNoCustomEqual any> extends OptionsBase { isKeyEqual?: never; isKeyItemEqual?: never; } interface OptionsKeyEqual any> extends OptionsBase { isKeyEqual: IsKeyEqual; isKeyItemEqual?: never; } interface OptionsKeyItemEqual any> extends OptionsBase { isKeyEqual?: never; isKeyItemEqual?: 'deep' | 'shallow' | IsKeyItemEqual; } /** * Configuration options to drive how entries are stored, checked for cache breakage, * and evicted from cache. */ type Options any> = | OptionsNoCustomEqual | OptionsKeyEqual | OptionsKeyItemEqual; /** * [key, value] pair for a given entry in cache. */ type CacheEntry any> = [Key, ReturnType]; /** * Snapshot of the current cache state as a set of [key, value] entries. */ interface CacheSnapshot any> { entries: Array>; keys: Key[]; size: number; values: Array>; } /** * Method that has been memoized via `micro-memoize`. */ type Memoized any, Opts extends Options> = Fn & { /** * The cache used for the memoized method. */ cache: Cache; /** * Manager for the expirations cache. This is only populated when * `options.expires` is set. */ expirationManager: ExpirationManager | null; /** * The original method that is memoized. */ fn: Fn; /** * Simple identifier that the function has been memoized. */ isMemoized: true; /** * Options passed for the memoized method. */ options: Opts; /** * Manager for the stats cache. This is only populated when `options.statsName` * is set. */ statsManager: StatsManager | null; }; interface Memoize { any, Options<(...args: any[]) => any>>>(fn: Fn): Memoized; any, Options<(...args: any[]) => any>>, Opts extends Options>( fn: Fn, passedOptions: Opts, ): Memoized; any>(fn: Fn): Memoized; any, Opts extends Options>(fn: Fn, passedOptions: Opts): Memoized; } declare const memoize: Memoize; export { Cache, CacheEventEmitter, clearStats, getStats, isCollectingStats, memoize, startCollectingStats, stopCollectingStats, }; export type { Arg, CacheEntry, CacheEvent, CacheEventListener, CacheEventType, CacheNode, CacheSnapshot, ExpiresConfig, ForceUpdate, GetExpires, GlobalStats, IsKeyEqual, IsKeyItemEqual, Key, Memoize, Memoized, OnAddEvent, OnDeleteEvent, OnHitEvent, OnUpdateEvent, Options, OptionsKeyEqual, OptionsKeyItemEqual, OptionsNoCustomEqual, ProfileStats, Serializer, ShouldPersist, ShouldRemoveOnExpire, TransformKey, };