decorio
Version:
First-class ECMAScript decorators for caching, binding, and concurrency patterns
27 lines (26 loc) • 1.21 kB
TypeScript
/**
* 🎯 Decorator `@mutex` ensures that at most one invocation of an async
* method is in flight at any time. While the decorated method is still
* running, all subsequent calls - regardless of their arguments - will
* return the same Promise as the first call, until it settles.
*
* ⚠️ Arguments are ignored when determining whether to reuse the in-flight
* call: every new invocation during the active period will simply
* reuse that single Promise.
*
* If you need to dedupe calls by their arguments, consider using
* `@singleflight` instead.
*
* Usage:
* ```typescript
* class Example {
* @mutex async reload(): Promise<void> { ... }
* }
*
* const e = new Example();
* e.reload(); // fires immediately
* e.reload(); // returns same Promise, does not re-fetch
* ```
*/
export declare function mutex<A extends Array<unknown>, R extends Promise<unknown>>(value: unknown, context: ClassMethodDecoratorContext<object, (...args: A) => R>): (...args: A) => R;
export declare function mutex<A extends Array<unknown>, R extends Promise<unknown>>(value: unknown, context: ClassFieldDecoratorContext<object, (...args: A) => R>): (originalFn: (...args: A) => R) => (...args: A) => R;