UNPKG

decorio

Version:

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

28 lines (27 loc) 1.34 kB
/** * 🎯 Decorator `@mutex` ensures that an async method never runs concurrently. * * Every invocation of the decorated method is executed strictly one at a time. * If the method is called again while a previous call is still running, * the new call will wait in a queue until all earlier calls have finished. * * Unlike `@singleflight`, this decorator: * - Does NOT return the same Promise for concurrent calls; * - Runs every invocation independently with its own arguments and result; * - Guarantees that each call will eventually execute - queued behind previously scheduled ones. * * Usage: * ```typescript * class Example { * @mutex async save(data: string): Promise<void> { ... } * } * * const e = new Example(); * e.save('A'); // runs immediately * e.save('B'); // waits until A finishes * e.save('C'); // waits until B finishes * // start A → end A → start B → end B → start C → end C * ``` */ 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;