UNPKG

decorio

Version:

First-class ECMAScript decorators for caching, binding, and concurrency patterns

34 lines (33 loc) 1.47 kB
/** * 🎯 Decorator `@cached` caches every unique (args -> result) pair * on a per-instance basis. Subsequent calls with the same arguments * return the cached result (instant for sync, same Promise for async). * * A static `cached.invalidate(fn)` method allows clearing the cache * for a given method/field so that subsequent calls re-invoke the original. * * Usage: * ```typescript * class Example { * @cached sum(a: number, b: number): number { * return a + b; * } * } * * const e = new Example(); * e.sum(1, 2); // computes 3 * e.sum(1, 2); // returns 3 from cache * * // flush the cache for this method * cached.invalidate(e.sum); * ``` */ export declare function cached(ttl: number): { <A extends Array<unknown>, R>(value: unknown, context: ClassMethodDecoratorContext<object, (...args: A) => R>): (...args: A) => R; <A extends Array<unknown>, R>(value: unknown, context: ClassFieldDecoratorContext<object, (...args: A) => R>): (originalFn: (...args: A) => R) => (...args: A) => R; }; export declare function cached<A extends Array<unknown>, R>(value: unknown, context: ClassMethodDecoratorContext<object, (...args: A) => R>): (...args: A) => R; export declare function cached<A extends Array<unknown>, R>(value: unknown, context: ClassFieldDecoratorContext<object, (...args: A) => R>): (originalFn: (...args: A) => R) => (...args: A) => R; export declare namespace cached { var invalidate: (target: Function) => void; }