Skip to content
module@warp-drive/alien-signals/primitives

Class Watcher sincev5.10.0 ​

Defined in: primitives.ts:418

Observes signals and memos from outside the graph. The notify callback passed to the constructor runs, at most once, as soon as anything watched may have changed: after a notifySignal of a watched signal, or of a signal a watched memo read. Call Watcher.rearm once you have handled the notification to be notified of the next change.

notify runs synchronously, inside the notifySignal call that caused it, so it should only schedule work. Reading or notifying signals from within it is not supported.

A watched memo should be read after it is watched, since the watcher only hears about changes to what the memo read during its last run.

Example ​

ts
import { Watcher, readMemo } from '@warp-drive/alien-signals/primitives';

const watcher = new Watcher(() => {
  queueMicrotask(() => {
    render();
    watcher.rearm();
  });
});

watcher.watch(memo);
readMemo(memo);

// when the view goes away
watcher.unwatchAll();

Implements ​

  • ReactiveNode

Constructors ​

Constructor ​

ts
new Watcher(notify: () => void): Watcher;

Defined in: primitives.ts:443

Parameters ​

notify ​

() => void

called once when anything watched may have changed, until Watcher.rearm is called

Returns ​

Watcher

Methods ​

getPending() ​

ts
getPending(): (
  | SignalNode
  | MemoNode<unknown>)[];

Defined in: primitives.ts:492

Lists what this watcher is watching that may have changed since it was created or last re-armed: memos that are due to recompute, and, in development builds only, signals that have been notified. Useful for debug logging; it walks everything watched.

Returns ​

( | SignalNode | MemoNode<unknown>)[]

the watched signals and memos that may have changed


rearm() ​

ts
rearm(): void;

Defined in: primitives.ts:478

Re-enables notify after a notification has been handled, so that the next change to anything watched calls it again.

Returns ​

void


unwatchAll() ​

ts
unwatchAll(): void;

Defined in: primitives.ts:507

Stops watching everything and re-arms the watcher. A memo that nothing else depends on releases its own dependencies and recomputes on its next read.

Returns ​

void


watch() ​

ts
watch(node: 
  | SignalNode
  | MemoNode<unknown>): void;

Defined in: primitives.ts:466

Starts watching a signal or memo. Watching something already watched does nothing.

Parameters ​

node ​

| SignalNode | MemoNode<unknown>

the signal or memo to watch

Returns ​

void

Properties ​

isNotified ​

Get Signature ​

ts
get isNotified(): boolean;

Defined in: primitives.ts:457

Whether notify has been called since the watcher was created or last re-armed.

Returns ​

boolean


watched ​

Get Signature ​

ts
get watched(): ReadonlySet<
  | SignalNode
| MemoNode<unknown>>;

Defined in: primitives.ts:450

Everything this watcher is watching. Useful for debug logging.

Returns ​

ReadonlySet< | SignalNode | MemoNode<unknown>>

Released under the MIT License.