Creating one
import { computed } from 'atemno';
const count = atom('count', 1);
const double = computed<number>('double', $ => $.get(count) * 2);
domain.get(double); // 2
domain.set(count, 5);
domain.get(double); // 10$ is the tracker context. Reading through $.get is what makes a source a dependency. Pass the type argument, because a computed cannot infer it. TypeScript explains why.
initials is derived from full, which is derived from two atoms. Type in either field.Lazy and cached
Writing to count marks double stale. It does not run it. double reruns on its next read, or during the write if something observes it. Reading twice with no change in between runs it once.
Propagation shows this on a live graph.
The tracker context
| Member | Does |
|---|---|
$.get(source) | Reads a source and records it as a dependency. |
$.peek(source) | Reads a source without making it a dependency. |
$.set(atom, value) | Writes an atom. |
$.reset(source) | Resets a source. |
$.onCleanup(fn) | Registers work to run before the next run. |
$.previous | The last successful result, as { value }, or undefined on the first run. |
The previous value
const total = computed<number>('total', $ => {
const next = $.get(count);
return $.previous ? $.previous.value + next : next;
});Cleanups
$.onCleanup runs before the computed runs again. Use it to undo whatever the last run set up.
const interval = atom('interval', 1000);
const timer = computed<number>('timer', $ => {
const id = setInterval(tick, $.get(interval));
$.onCleanup(() => clearInterval(id));
return id;
});Forcing a rerun
domain.reset(double) runs a computed again even if nothing it read has changed. Use it when the computation reads something atemno does not track, such as the clock.
const now = computed<number>('now', () => Date.now());
domain.reset(now);- If something observes the computed, it reruns straight away, and readers are updated if the value changed.
- Otherwise it is only marked stale, and reruns on its next read.
- A computed this domain has not evaluated yet is left alone.
Errors
A computed that throws stores the error and rethrows it on every read. See errors.