[]previous and next page

writeSignal

Stores a new value in a signal node and notifies its readers when the value differs by the signal's equality.

Stores a new value in a signal node and notifies its readers when the value differs by the signal's equality. Effects that read it run on the next flush; memos recompute on their next read.

Import#

ts
import { writeSignal } from "@shadow-js/core/extend";

Signature#

ts
function writeSignal<T>(signal: Signal<T>, next: T, options?: WriteOptions): boolean

Parameters#

Name Description
signal The node, from createSignalNode.
next The new value. A function is stored as is: there is no updater form.
options allowInMemo permits the write while a memo computes. Undefined uses the default.

Returns#

true when the value changed and was stored, false when the equality kept the old value (nobody is notified then).

Throws#

  • ShadowError WRITE_IN_MEMO, in development builds only, when a memo is computing and allowInMemo is not set.

Examples#

ts
import { createSignalNode, writeSignal } from "@shadow-js/core/extend";

const status = createSignalNode<string>("idle", Object.is);
console.log(writeSignal(status, "loading")); // true
console.log(writeSignal(status, "loading")); // false: an equal value notifies nobody