Reacting
Example
The samples on this page use the realm and the box $tags of
Updates.
import { box, createRealm } from "@frondruntime/pinna";
const realm = createRealm();
const $tags = box<ReadonlyArray<string>>(realm, []);watch runs its effect when the select’s result changes, not on the first
run. The effect gets the value and the one before it.
import { watch } from "@frondruntime/pinna";
const stop = watch(
realm,
(read) => read($tags).length,
(count, previous) => {
console.log(`${previous} → ${count} tags`);
},
);autorun runs at once, and again when what its last run read changes.
import { autorun } from "@frondruntime/pinna";
const stopTitle = autorun(realm, (read) => {
document.title = `${read($tags).length} tags`;
});A function an autorun returns is its cleanup.
import { autorun, box, update } from "@frondruntime/pinna";
const $editing = box(realm, false);
const stopEscape = autorun(realm, (read) => {
if (!read($editing)) return undefined;
const onKey = (event: KeyboardEvent) => {
if (event.key === "Escape") {
update(realm, (_read, write) => write($editing, false));
}
};
window.addEventListener("keydown", onKey);
// Runs before the next run, and when the autorun stops.
return () => window.removeEventListener("keydown", onKey);
});An autorun’s body may call update. The recipe runs untracked, so its reads
of what it writes do not subscribe the autorun.
import { autorun, box, update } from "@frondruntime/pinna";
const $selected = box(realm, 0);
// Keeps the selection inside the list as it shrinks.
const stopClamp = autorun(realm, (read) => {
const last = Math.max(read($tags).length - 1, 0);
update(realm, (read, write) => {
if (read($selected) > last) {
write($selected, last);
}
});
});Rules
- Prefer a computed value to an autorun that writes a box: derived state is computed, not copied.
- A watch’s select only reads: an update there throws. Its effect runs
untracked and writes through
update. - An autorun that writes what it read runs again, until what it read stops
changing. One that never settles stops after 100 rounds, and
pinna: watchers did not settle after 100 roundsgoes toonError. - A watch’s effect returns
undefined, so an async function does not type-check. The refusal is in the types only: at run time the returned promise is dropped, and its rejection is unhandled, reaching neither the caller noronError. Start async work from the effect and handle its errors there. - An error from the first run of either is thrown to the caller, and nothing
is watched. A later one goes to the realm’s
onError. - Both return a stop function. Stopping twice does nothing, and once stopped, the effect never runs again.
- In React, read with the hooks instead: Hooks.
Answers
| Call | Answers |
|---|---|
watch(realm, (read) => ..., (value, previous) => ..., { name?, equals? }) |
The stop function. equals decides what counts as a change; Object.is by default. |
autorun(realm, (read) => ...) |
The stop function. |
See also
- Updates: writing from an effect.
- Lifecycles: what a watch or an autorun holds observed.
- Programs:
watchScopedandchangesas Effect programs. - Live data: a watch that refetches when its input changes.