Skip to content
asyncmux
English
Esc
↑↓navigate↵open⌘Jpreview
On this page

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;
PropType
method(this: any, ...args: any) => Promise<any>

The async class method to decorate. It must return a `Promise`.

Type(this: any, ...args: any) => Promise<any>
contextunknown

The Stage 3 decorator context. Throws `DecoratorSupportError` when it has no `addInitializer`.

Typeunknown

The 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 ReentrantLockError when reentrancy is detected.
  • Throws DecoratorSupportError without Stage 3 decorator support.

Function asyncmux()

Acquires the write lock of the given object.

function asyncmux(this_: object, signal?: AbortSignal): Promise<AsyncmuxLock>;
PropType
this_object

The object to lock.

Typeobject
signal?AbortSignal

A signal to abort the lock wait. On abort, the call rejects with `signal.reason`.

TypeAbortSignal

The return value is a Promise that resolves with the acquired AsyncmuxLock. For how to release it, see AsyncmuxLock.

  • Throws ReentrantLockError synchronously 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.

See also

Was this page helpful?