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.
Examples
Section titled “Examples”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 falseConstructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Signal(
autoReset,initialState?):Signal
Defined in: packages/async/src/synchro/signal.ts:69
Creates a new Signal instance.
Parameters
Section titled “Parameters”autoReset
Section titled “autoReset”boolean
If true, the signal will automatically reset after releasing a waiter. If false, the signal will remain signaled until manually reset.
initialState?
Section titled “initialState?”boolean = false
The initial state of the signal. Default is false (non-signaled).
Returns
Section titled “Returns”Signal
Accessors
Section titled “Accessors”signaled
Section titled “signaled”Get Signature
Section titled “Get Signature”get signaled():
boolean
Defined in: packages/async/src/synchro/signal.ts:77
Indicates whether the signal is in the signaled state.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”reset()
Section titled “reset()”reset():
void
Defined in: packages/async/src/synchro/signal.ts:97
Reset the signal to non-signaled state
Returns
Section titled “Returns”void
signal()
Section titled “signal()”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.
Returns
Section titled “Returns”void
wait()
Section titled “wait()”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.
Parameters
Section titled “Parameters”signal?
Section titled “signal?”AbortSignal | null
An AbortSignal that can be used to cancel the wait operation.
Returns
Section titled “Returns”Promise<void>
A promise that resolves when the signal is signaled, or rejects when aborted.