UNPKG

chrono-forge

Version:

A comprehensive framework for building resilient Temporal workflows, advanced state management, and real-time streaming activities in TypeScript. Designed for a seamless developer experience with powerful abstractions, dynamic orchestration, and full cont

45 lines (44 loc) 1.57 kB
/** * Decorator that ensures exclusive execution of a method using a mutex lock. * This prevents concurrent execution of the decorated method within the same instance. * * ## Parameters * @param {string} [mutexName='execute'] - Name of the mutex lock. Methods sharing the same * mutexName will be mutually exclusive. Defaults to 'execute'. * * ## Features * - **Thread Safety**: Ensures only one execution of the method at a time * - **Instance-Level Locking**: Mutex locks are scoped to class instances * - **Named Locks**: Support for multiple named mutexes within the same instance * - **Async Support**: Works with both async and sync methods * * ## Common Use Cases * 1. Protecting shared resource access * 2. Preventing race conditions in state updates * 3. Ensuring sequential execution of critical sections * 4. Coordinating workflow state changes * * ## Usage Examples * ```typescript * class WorkflowExample { * @Mutex('stateUpdate') * async updateState(): Promise<void> { * // This code will run exclusively * } * * @Mutex() // Uses default 'execute' mutex name * async execute(): Promise<void> { * // This code will run exclusively * } * } * ``` * * ## Notes * - Different methods can share the same mutex by using the same mutexName * - The decorator automatically handles async/await operations * - Locks are released automatically after method completion or error * * @decorator * @see {@link MutexLock} from 'async-mutex' */ export declare function Mutex(mutexName?: string): MethodDecorator;