Contents
- Welcome to Domma Reactive
- 01 Getting started
- 02 Reactive state
- 03 HTML and bindings
- 04 Components
- 05 Reference
Observables and computed values
Updated by Darryl Waterhouse on 17 October 2026 · 3 min read
observable
const count = observable(0);
count.value; // 0 - tracked: registers a dependency
count.value = 5; // write
count.set(5); // the same write, as a call
count.peek(); // read WITHOUT registering a dependency
count.subscribe(fn); // see below
count.extend({...}); // layer on behaviour - see Extenders
peek() and set() are closures, not methods, so they survive being destructured off the observable or handed straight
to a callback.
The equals comparator gates notification, not the write. A write always lands; the graph only hears about it when
the comparator reports a change, so a comparator that deliberately ignores part of the payload can never leave readers
serving stale data:
const user = observable({id: 1, seenAt: 0}, {equals: (a, b) => a.id === b.id});
The default is deep equality. The corollary of gating at all: mutating the held value in place and assigning it back is invisible, because old and new are the same reference. Derivations must produce new values, not edit old ones.
observableArray
const rows = observableArray([{id: 1}]);
rows.value; // the underlying array - tracked
rows.length; // tracked, so a rendered count updates
rows.peek(); // the live array, untracked
rows.value = [...]; // wholesale replacement - gated by `equals`
rows.push(item); // and pop, shift, unshift, splice, sort, reverse, fill, copyWithin
rows.remove(item); // every occurrence of that exact object
rows.remove(r => r.id > 2); // every item the test accepts
rows.removeAll();
rows.indexOf(item); // tracked, unlike peek().indexOf(item)
rows.replace(old, new); // swaps the first occurrence, in place
rows.destroy(item); // marks rather than removes - see below
rows.destroyAll();
The in-place mutators run the native method and return exactly what it returns, so pop() gives you the item and
splice() gives you the removed slice.
Two write paths, two rules, deliberately. Mutators notify unconditionally - an in-place mutation leaves the array
equal to itself, so an equality gate could only be implemented by holding a copy and diffing it, at a full pass over the list per push.
Wholesale assignment is gated, exactly as observable() is. The accepted cost is a spurious notification from a
mutator that changed nothing (a no-op sort()), which errs towards notifying too often - the safe direction.
The initial array, and any array assigned wholesale, is copied rather than adopted. Holding your reference would
alias it: a push through the original would change what .value returns without ever reaching the graph, and the data
and the DOM would disagree silently and permanently. If you genuinely want the live array, take it from peek().
remove() takes either a value or a test. A value matches by identity, which is what a reconciled list wants -
$parent.remove($data) hands over the very object the row was rendered from. A function is called with (item, index)
and everything it accepts goes.
The one case this gives up is an array of bare functions removing one of its own members by passing it; peek() plus
splice() still covers that.
destroy() marks an item _destroy: true and leaves it in the array. That is Knockout's behaviour, and it exists
for one reason: Rails' accepts_nested_attributes_for deletes a record when the payload it receives carries that flag,
so the array must still contain the item at submit time while no longer showing it. Both render paths - {{#each}} and
data-each - skip a marked item, so the two halves agree. Outside that server convention, remove() is the right
call: it says what it does.
computed
const total = computed(() => price.value * qty.value);
total.value; // recompute if stale, then return - and register a dependency
total.get(); // identical; `.value` is the form a template can use
Computeds are lazy: the body runs on first read and then only when something it read has changed. Dependencies are
re-collected on every run, so a computed whose branch changed (mode.value === 'a' ? x.value : y.value) stops depending
on the branch not taken.
Prefer .value. It is the only spelling a template can use - an expression cannot call a method - and it means an
observable and a computed look the same at the point of use.
A computed is read-only unless you say where a write should land:
const celsius = observable(100);
const fahrenheit = computed({
read: () => celsius.value * 9 / 5 + 32,
write: (f) => { celsius.value = (f - 32) * 5 / 9; }
});
fahrenheit.value; // 212
fahrenheit.value = 32; // → celsius.value === 0
That is what lets data-model="fahrenheit.value" bind a derived value. Without it the binding would assign onto the
cached read, the control would look wired up, and every keystroke would vanish on the next recompute - so assigning to a
computed with no write warns instead, naming it. The write runs untracked: a writer that reads a unit setting before
storing does not thereby depend on it.
On this page