export interface Dictionary { [key: string]: Type; [index: number]: Type; } export type AnyFn = (...args: any[]) => any; export type Key = any[]; export type RawKey = Key | IArguments; export type Value = any; export interface CacheSnapshot { keys: Key[]; size: number; values: Value[]; } export class Cache { readonly canTransformKey: boolean; readonly getKeyIndex: KeyIndexGetter; readonly options: NormalizedOptions; readonly shouldCloneArguments: boolean; readonly shouldUpdateOnAdd: boolean; readonly shouldUpdateOnChange: boolean; readonly shouldUpdateOnHit: boolean; /** * The prevents call arguments which have cached results. */ keys: Key[]; /** * The results of previous cached calls. */ values: Value[]; constructor(options: NormalizedOptions); /** * The number of cached [key,value] results. */ get size(): number; /** * A copy of the cache at a moment in time. This is useful * to compare changes over time, since the cache mutates * internally for performance reasons. */ get snapshot(): CacheSnapshot; /** * Order the array based on a Least-Recently-Used basis. */ orderByLru(key: Key, value: Value, startingIndex: number): void; /** * Update the promise method to auto-remove from cache if rejected, and * if resolved then fire cache hit / changed. */ updateAsyncCache(memoized: Memoized): void; } export type EqualityComparator = (object1: any, object2: any) => boolean; export type MatchingKeyComparator = (key1: Key, key2: RawKey) => boolean; export type CacheModifiedHandler = ( cache: Cache, options: NormalizedOptions, memoized: Memoized, ) => void; export type KeyTransformer = (args: Key) => Key; export type KeyIndexGetter = (keyToMatch: RawKey) => number; export interface StandardOptions { isEqual?: EqualityComparator; isMatchingKey?: MatchingKeyComparator; isPromise?: boolean; maxSize?: number; onCacheAdd?: CacheModifiedHandler; onCacheChange?: CacheModifiedHandler; onCacheHit?: CacheModifiedHandler; transformKey?: KeyTransformer; } export interface Options extends StandardOptions, Dictionary {} export interface NormalizedOptions extends Options { isEqual: EqualityComparator; isPromise: boolean; maxSize: number; } export type Memoized = Fn & Dictionary & { cache: Cache; fn: Fn; isMemoized: true; options: NormalizedOptions; }; /** * @deprecated * All types declared on this namespace are available for direct import * from the package. In a future release, this namespace will be removed * in favor of those direct imports. */ export declare namespace MicroMemoize { export type { Cache, CacheModifiedHandler, EqualityComparator, Key, KeyIndexGetter, KeyTransformer, MatchingKeyComparator, Memoized, NormalizedOptions, Options, StandardOptions, RawKey, Value, }; } export default function memoize( fn: Fn | Memoized, options?: Options, ): Memoized;