UNPKG

fast-typescript-memoize

Version:

Fast memoization decorator and other helpers with 1st class support for Promises.

46 lines 2.76 kB
/** * Additional options for `@Memoize()` decorator. */ export interface MemoizeOptions { /** Defaults to `true`. If true, rejected Promises returned from an async * method will be removed from the cache as soon as the method finishes. */ clearOnReject?: boolean; /** Defaults to `false`. If true, successfully resolved Promises returned from * an async method will be removed from the cache as soon as the method * finishes. This is a convenient mode for the cases when we want to coalesce * multiple parallel executions of some method (e.g. when there is a burst of * runs), but we don't want to prevent the method from further running. */ clearOnResolve?: boolean; } /** * Remembers the returned value of a decorated method or getter in a hidden * `this` object's property, so next time the method is called, the value will * be returned immediately, without re-executing the method. This also works for * async methods which return a Promise: in this case, multiple parallel calls * to that method will coalesce into one call. * * All `@Memoize()` calls, for both methods and getters, accept a hasher * function with the same list of arguments as the method itself (or, in case of * a getter, with no arguments). The slot for the saved value will depend on the * value returned by the hasher. */ export declare function Memoize<TThis, TValue>(hasher: TValue extends (...args: never[]) => unknown ? (this: TThis, ...args: Parameters<TValue>) => unknown : (this: TThis) => unknown, options?: MemoizeOptions): (target: TThis, propertyKey: string | symbol, descriptor: { value?: TValue; }) => void; /** * Remembers the returned value of a decorated method or getter in a hidden * `this` object's property, so next time the method is called, the value will * be returned immediately, without re-executing the method. This also works for * async methods which return a Promise: in this case, multiple parallel calls * to that method will coalesce into one call. * * Almost all `@Memoize()` calls may also omit the hasher function. Then, for * 0-argument methods or getters, the slot for the saved value will be fixed. * For 1-argument methods, the slot will be chosen based on that single * argument's value. For methods with 2+ arguments, you must provide your own * hasher function. */ export declare function Memoize<TThis, TValue>(options?: MemoizeOptions): (target: TThis, propertyKey: string | symbol, descriptor: { value?: TValue; }) => ((a1: unknown, a2: unknown, ...args: unknown[]) => never) extends TValue ? TValue extends (a1: never, a2: never, ...args: never[]) => unknown ? "provide-hasher-when-method-has-more-than-one-arg" : void : void; //# sourceMappingURL=Memoize.d.ts.map