@ac-essentials/misc-util
    Preparing search index...

    Class Signal

    A signal primitive for signaling between async tasks.

    When the signal is in the signaled state, calls to wait will resolve immediately. When the signal is in the non-signaled state, calls to wait will block until the signal is signaled. Waiters are released in FIFO order.

    The signal can be configured to auto-reset or manual-reset.

    If autoReset is true, the signal will automatically reset to the non-signaled state after a single waiter is released, acting like a binary semaphore. If autoReset is false, the signal will remain in the signaled state until it is manually reset.

    const signal = new Signal(true); // autoReset = true

    // Task 1
    async function task1() {
    console.log("Waiting for signal...");
    await signal.wait();
    console.log("Signal received!");
    }

    // Task 2
    async function task2() {
    console.log("Signaling...");
    signal.signal();
    }

    task1();
    task2();
    const signal = new Signal(false); // autoReset = false

    // Task 1
    async function task1() {
    console.log("Waiting for signal...");
    await signal.wait();
    console.log("Signal received!");
    }

    // Task 2
    async function task2() {
    console.log("Signaling...");
    signal.signal();
    }

    task1();
    task2();
    task1(); // This will also proceed immediately since autoReset is false
    Index

    Constructors

    Accessors

    Methods

    Constructors

    • Creates a new Signal instance.

      Parameters

      • autoReset: boolean

        If true, the signal will automatically reset after releasing a waiter. If false, the signal will remain signaled until manually reset.

      • initialState: boolean = false

        The initial state of the signal. Default is false (non-signaled).

      Returns Signal

    Accessors

    Methods

    • Set the signal to signaled state

      Notifies waiting listeners. If autoReset is true, only one listener will be notified and the signal will reset to non-signaled state. If autoReset is false, all listeners will be notified.

      Listeners are notified asynchronously.

      Returns void

    • Wait for the signal to be signaled.

      If the signal is already signaled, the listener is called immediately. If autoReset is true, the signal is reset to non-signaled state after notifying a listener.

      Note: If multiple listeners are waiting and the signal is signaled, only one listener will be notified if autoReset is true. If autoReset is false, all listeners will be notified.

      Parameters

      • Optionalsignal: AbortSignal | null

        An AbortSignal that can be used to cancel the wait operation.

      Returns Promise<void>

      A promise that resolves when the signal is signaled, or rejects when aborted.