Class method utilities
Signatures, arguments, and exceptions for the decorators and functions provided by the asyncmux default export.
asyncmux is the default export. It can be used both as a class method decorator and as a function.
import { asyncmux } from "asyncmux";
Decorator @asyncmux
Applies a write lock to the target async class method.
function asyncmux<TMethod extends (this: any, ...args: any) => Promise<any>>(
method: TMethod,
context: unknown,
): TMethod;
method(this: any, ...args: any) => Promise<any>
The async class method to decorate. It must return a `Promise`.
(this: any, ...args: any) => Promise<any>contextunknown
The Stage 3 decorator context. Throws `DecoratorSupportError` when it has no `addInitializer`.
unknownThe return value is the method with lock acquisition and release built in. Each call acquires the write lock of this and releases it when the method finishes.
- Throws
ReentrantLockErrorwhen reentrancy is detected. - Throws
DecoratorSupportErrorwithout Stage 3 decorator support.
Function asyncmux()
Acquires the write lock of the given object.
function asyncmux(this_: object, signal?: AbortSignal): Promise<AsyncmuxLock>;
this_object
The object to lock.
objectsignal?AbortSignal
A signal to abort the lock wait. On abort, the call rejects with `signal.reason`.
AbortSignalThe return value is a Promise that resolves with the acquired AsyncmuxLock. For how to release it, see AsyncmuxLock.
- Throws
ReentrantLockErrorsynchronously when reentrancy is detected. No detection occurs outside a lock context established by a decorator. - Functions cannot be lock targets. When the first argument is a function, it is treated as a decorator.
Decorator @asyncmux.readonly
Applies a read lock to the target async class method. The signature and arguments are the same as @asyncmux.
function asyncmux.readonly<
TMethod extends (this: any, ...args: any) => Promise<any>,
>(
method: TMethod,
context: unknown,
): TMethod;
Read locks can run concurrently with each other. They wait while a write lock is held. The return value and exceptions are the same as @asyncmux.
Function asyncmux.readonly()
Acquires the read lock of the given object.
function asyncmux.readonly(
this_: object,
signal?: AbortSignal,
): Promise<AsyncmuxLock>;
The arguments and return value are the same as asyncmux(); only the lock kind differs.