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

Subscribe to a single signal using the canonical one-shot watcher lifecycle.

The callback runs from a queued microtask after pending notifications are drained, then the watcher re-arms itself so future updates are not missed. The returned cleanup keeps teardown idempotent by tolerating already-detached watchers.

Memory safety (Phase 29):

  • disposed flag prevents post-cleanup callback execution: if cleanup is called before a pending microtask fires, the microtask returns early.
  • needsEnqueue guard dedups rapid synchronous signal changes: only one microtask is ever queued per batch of synchronous mutations.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
signal| State<T> | Computed<T>A Signal.State or Signal.Computed to subscribe to.
onValue(value) => voidCalled with the current signal value after each change.

Returns

A cleanup function that unregisters the watcher.

() => void