UNPKG

@maximai/maxim-js

Version:

Maxim AI JS SDK. Visit https://getmaxim.ai for more info.

33 lines (32 loc) 1.11 kB
export declare class Mutex { private semaphore; private static mutexes; /** * Creates a new Mutex instance with a given key * @param key - Unique identifier for the mutex */ private constructor(); /** * Acquires the lock. If the lock is already held, waits until it's released * @returns Promise that resolves when the lock is acquired */ lock(): Promise<void>; /** * Releases the held lock */ release(): void; /** * Gets or creates a Mutex instance for the given key * @param key - Unique identifier for the mutex * @returns Mutex instance */ static get(key: string): Mutex; /** * Executes a critical section with automatic lock handling * @param key - Unique identifier for the mutex * @param criticalSection - Async function to execute within the mutex * @returns Promise that resolves with the result of the critical section */ static withLock<T>(key: string, criticalSection: () => Promise<T>): Promise<T>; withLock<T>(criticalSection: () => Promise<T>): Promise<T>; }