Start
Example
A realm holds every observable it makes. Make one for the app, in module code.
import { createRealm } from "@frondruntime/pinna";
const realm = createRealm();A box is observable state. A computed value is derived from what its function reads, and cached.
import { box, computed } from "@frondruntime/pinna";
const $count = box(realm, 0, { name: "count" });
const $double = computed(realm, (read) => read($count) * 2, { name: "double" });- A name that starts with
$is observable. Everything else is plain data. { name }names the value ininspectand in its errors.
A box is written only inside update, which hands out read and
write.
import { update } from "@frondruntime/pinna";
const increment = () => {
update(realm, (read, write) => {
write($count, read($count) + 1);
});
};A component reads with useValue and rerenders when the value changes. It
needs no provider: the value carries its realm.
import { useValue } from "@frondruntime/pinna-react";
function CounterButton() {
const double = useValue($double);
return (
<button type="button" onClick={increment}>
{double}
</button>
);
}State made more than once, such as once per test, is a model: a class that
extends Model, whose fields are made against this.realm and whose methods
write through update.
import { box, computed, Model, update } from "@frondruntime/pinna";
class Counter extends Model {
readonly $count = box(this.realm, 0, { name: "count" });
readonly $double = computed(this.realm, (read) => read(this.$count) * 2, { name: "double" });
increment(): void {
update(this.realm, (read, write) => {
write(this.$count, read(this.$count) + 1);
});
}
}A test makes the model in a realm of its own and reads as application code
does, inside untracked.
import { createRealm, untracked } from "@frondruntime/pinna";
const counter = new Counter(createRealm());
counter.increment();
const double = untracked(counter.realm, (read) => read(counter.$double)); // 2Rules
- An observable has no read or write methods. Every place that reads hands out
read;updatealone also hands outwrite. - An update that throws is undone: every write before the throw is taken
back, except a
write.changedon an atom, whose watchers still run. - There is no default realm. Batches, tracking, watchers, and errors stay inside the realm that made them, so a test with a realm of its own shares nothing with the next.
- The samples on these pages use one realm made in module code, for brevity.
- The pages under Core and React import from
@frondruntime/pinna, which needs no Effect, and@frondruntime/pinna-react. The pages under Effect import from@frondruntime/pinna-effect, which re-exports every call of@frondruntime/pinnaand adds boxes and models that check an Effect Schema on every write: see Schema-checked boxes.
Answers
| Call | Answers |
|---|---|
createRealm({ onError?, name? }) |
A realm. onError gets the errors with no caller, and defaults to the host’s reportError. name labels the realm in inspect and in an error about another realm; it is never compared. |
box(realm, initial, options?) |
A box. A write equal to the current value under Object.is notifies nobody. |
computed(realm, (read) => ..., options?) |
A cached value derived from what the function reads. |
update(realm, (read, write) => ...) |
Runs the recipe atomically. Watchers see its end state once. |
useValue($x) |
The value of $x; the component rerenders when it changes. |
class X extends Model |
A model that checks nothing. Field initializers read this.realm; new X(realm) makes one. |
Plain or schema boxes
The choice is per box, not per application. A plain box and a
collection check nothing, and neither does Model, the base class of a
model with no schema. A schema box, a schemaBox or a SchemaModel’s
BoxField, checks every write against its schema, and a CollectionField
checks each row against its row schema when the row lands. A value the
schema rejects throws and undoes the update.
- Data from outside the program, such as a response from the wire or user
input, goes through a schema.
decode(realm, schema)(input)checks it once at the boundary, whatever boxes then hold it. - A value whose writes need a runtime check takes a schema box: the check runs on every write.
- Trusted data that is written often takes a plain box, which runs no check.
- A schema box’s check is never weakened for speed. The plain box is the way to check nothing; there is no option that turns a schema box’s check off.
A SchemaModel, from @frondruntime/pinna-effect, can hold a plain box in
its class body beside its BoxFields. The plain box checks nothing, and the
BoxField keeps checking.
import { BoxField, box, decode, SchemaModel, update } from "@frondruntime/pinna-effect";
import { Schema } from "effect";
class Ticker extends SchemaModel<Ticker>("Ticker")({
symbol: Schema.String,
$volume: BoxField(Schema.Number.check(Schema.isGreaterThanOrEqualTo(0))), // checked on every write
price: Schema.Number.check(Schema.isGreaterThan(0)), // checked once, by decode
}) {
// Starts at the decoded price; checks nothing, and is not on the wire.
readonly $lastPrice = box(this.realm, this.price);
tick(price: number): void {
update(this.realm, (_read, write) => {
write(this.$lastPrice, price);
});
}
}
const ticker = decode(realm, Ticker)({ symbol: "ACME", volume: 0, price: 12 });
ticker.tick(12.5);- A box in the class body is not a schema field, so it is not on the wire:
decodedoes not fill it, and encoding does not write it. - A body box seeded from a decoded field, as
readonly $lastPrice = box(this.realm, this.price), starts from a value thatdecodechecked once. Later writes to the box check nothing, and the fieldpriceand the encoded output stay at the decoded value. - No
$field is both on the wire and unchecked. A$field on the wire is aBoxField, which checks every write, or aCollectionField, which checks every row written to it.
See also
- Coming from MobX: the Pinna call for each MobX one.
- Updates: drafts, atomic updates, and
reconcile. - Reacting: watchers and autoruns.
- Schema-checked boxes: the same calls on Effect Schema, with models decoded from the wire.
- Hooks: every way a component reads.