Skip to content

Function: watchSignal()

API / @xmachines/play-signals / watchSignal

function watchSignal<T>(signal, onValue): () => void;

Defined in: packages/play-signals/src/watch-signal.ts:21

Subscribes to one signal, with the canonical one-shot watcher lifecycle.

The callback runs from a microtask in the queue, after the watcher drains the notifications that wait. The watcher then arms itself again, so that it misses no later update. The cleanup function is idempotent, because it accepts a watcher that is detached already.

Memory safety (Phase 29):

  • The disposed flag stops a callback after the cleanup: the microtask returns at once when the cleanup runs before the microtask fires.
  • The needsEnqueue guard removes the duplicates of rapid synchronous signal changes: one batch of synchronous changes queues one microtask only.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
signal| State<T> | Computed<T>The Signal.State or Signal.Computed to subscribe to.
onValue(value) => voidThe watcher calls it with the current signal value after each change.

Returns

The cleanup function. It removes the watcher.

() => void