decorio
Version:
First-class ECMAScript decorators for caching, binding, and concurrency patterns
22 lines (21 loc) • 807 B
TypeScript
/**
* 🎯 Decorator ensures a method is only executed once per unique argument list.
* Subsequent calls with the same arguments return the cached result.
*
* Usage:
* ```typescript
* class Example {
* @once compute(x: number): number {
* console.log('Computing', x);
*
* return x * 2;
* }
* }
*
* const e = new Example();
* e.compute(3); // logs 'Computing 3', returns 6
* e.compute(3); // returns cached 6, no log
* ```
*/
export declare function once<A extends Array<unknown>, R>(value: unknown, context: ClassMethodDecoratorContext<object, (...args: A) => R>): (...args: A) => R;
export declare function once<A extends Array<unknown>, R>(value: unknown, context: ClassFieldDecoratorContext<object, (...args: A) => R>): (originalFn: (...args: A) => R) => (...args: A) => R;