Skip to content

Signal

Defined in: packages/async/src/synchro/signal.ts:56

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

new Signal(autoReset, initialState?): Signal

Defined in: packages/async/src/synchro/signal.ts:69

Creates a new Signal instance.

boolean

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

boolean = false

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

Signal

get signaled(): boolean

Defined in: packages/async/src/synchro/signal.ts:77

Indicates whether the signal is in the signaled state.

boolean

reset(): void

Defined in: packages/async/src/synchro/signal.ts:97

Reset the signal to non-signaled state

void


signal(): void

Defined in: packages/async/src/synchro/signal.ts:90

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.

void


wait(signal?): Promise<void>

Defined in: packages/async/src/synchro/signal.ts:116

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.

AbortSignal | null

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

Promise<void>

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