Signals State & Computed
State<T> is the writable signal primitive. Computed<T> is the lazy derived signal primitive.
State
Use State<T> when a value should be set directly.
import { State } from '@ecopages/signals';
const count = new State(0);
count.set(1);
count.update((value) => value + 1);Computed
Use Computed<T> when a value should derive from other signals.
import { Computed, State } from '@ecopages/signals';
const count = new State(0);
const parity = new Computed(() => ((count.get() & 1) === 0 ? 'even' : 'odd'));
console.log(parity.get());Dependencies are discovered automatically from the signals read during evaluation.
Derived Lists
mapItems(source, project) returns a computed that maps each item of a list signal once and reuses that item's result while it stays in the list. Results are kept by item identity, so moving, removing or adding items projects only the new ones; the others keep the same result object. project runs untracked: the list depends on the source alone.
import { mapItems, state } from '@ecopages/signals';
const rows = state([{ id: 1, label: state('first') }]);
const labels = mapItems(rows, (row) => row.label);In JSX, <MapItems each={rows}> from @ecopages/jsx is the same idea for rows; see Signals.
Equality And Caching
Both writable and computed signals support custom equality functions through options. Computed signals cache their last successful value and only publish when the exposed value actually changes under that equality function.
Advanced Helpers
currentComputed()returns the computed currently being evaluated, if anypeek(signal)reads without trackinguntrack(read)runs a block without collecting dependencies