v0 · Developer PreviewPinna is under active development. APIs may change between releases.

Boxes and computed

Example

A box is observable state. Its name starts with $.

ts
import { box, createRealm } from "@frondruntime/pinna";

const realm = createRealm();

const $price = box(realm, 10, { name: "price" });
const $tags = box(realm, ["sale"], { name: "tags" });

A write equal to the current value notifies nobody. A box compares with Object.is, so a new array or object counts as a change. { equals } replaces the comparison. To check every write against a schema, use Schema-checked boxes.

ts
import { box } from "@frondruntime/pinna";

// A write within a cent of the current value notifies nobody.
const $total = box(realm, 0, {
  equals: (previous, next) => Math.abs(previous - next) < 0.01,
});

A computed value is derived from what its function reads. It recomputes only when one of those changes.

ts
import { computed } from "@frondruntime/pinna";

const $count = computed(realm, (read) => read($tags).length, { name: "count" });

// A computed value reads other computed values the same way.
const $label = computed(realm, (read) => `${read($count)} tags at ${read($price)}`);

computedFunction makes a computed value per argument, for a value that depends on an argument.

ts
import { computedFunction } from "@frondruntime/pinna";

const hasTag = computedFunction(realm, (read, tag: string) => read($tags).includes(tag));

// While something observes hasTag("sale"), a call with "sale" returns that same
// computed value; nothing observes this one, so a later call makes a new one.
const $onSale = hasTag("sale");

A model groups boxes, computed values, and the methods that write them. A class that extends Model checks nothing, takes its realm in the constructor, and gives field initializers this.realm. A write goes through a method that calls update.

ts
import { box, computed, Model, update } from "@frondruntime/pinna";

// A model groups boxes and the methods that write them. `Model` checks
// nothing and gives field initializers `this.realm`.
class Basket extends Model {
  readonly $quantity = box(this.realm, 1, { name: "quantity" });
  readonly $unitPrice = box(this.realm, 10, { name: "unitPrice" });
  readonly $total = computed(this.realm, (read) => read(this.$quantity) * read(this.$unitPrice));

  add(count: number): void {
    update(this.realm, (read, write) => write(this.$quantity, read(this.$quantity) + count));
  }
}

const basket = new Basket(realm);
basket.add(2);

Rules

  • A box holds plain data: numbers, strings, arrays, and plain objects. Its type is the value’s own, not made readonly, so a change in place type-checks and reaches no watcher. A box is written whole, so a finer-grained update is more boxes, or a collection of rows by key.
  • A computed value only reads. A write, a watch, an autorun, an observe, or a release throws while it computes. Its equals is part of the compute and never does any of them either.
  • A computed value that throws keeps the error and throws it to each read, until a source it read changes.
  • While observed, a computed value stays linked to what it read. Unobserved, it holds no links, and a read checks its sources first.
  • A computedFunction compares arguments by identity, one per position. While nothing observes an entry, it is dropped, and the next call makes a new one.
  • A model with no schema extends Model, from @frondruntime/pinna. With Effect, a model is a SchemaModel: see Classes. There is no third form, such as a function that returns boxes: a draft leaves an instance of either class read-only, and a plain object of boxes it would draft.
  • { name } is for tools: inspect reports it, and a named computed value’s errors name it. An empty name throws.

Answers

Call Answers
box(realm, initial, { name?, equals? }) A box. equals defaults to Object.is.
computed(realm, (read) => ..., { name?, equals? }) A computed value. equals defaults to Object.is.
computedFunction(realm, (read, ...args) => ..., options?) A function from arguments to a computed value, shared while observed.
class X extends Model A model that checks nothing. new X(realm) makes one; field initializers read this.realm.
read($x) The value, inside a computed value, a computed function, a watch’s select, an autorun, useSelect, an update, or untracked. An atom reads as undefined.
read.row($collection, key) The row under key, or undefined: see Collections.
Read The type of read, for a function that takes it: (read: Read) => ....
isTracking(realm) true while reads are tracked: in a computed value, a watch’s select, or an autorun. false in an update, untracked, a watch’s effect, and a hook.

Constructor inputs

A model’s field initializers run when Model’s constructor returns, before the subclass’s own constructor sets its parameter properties. An initializer reads this.realm, which Model has set. A computed value’s function runs on its first read, which comes after the constructor unless the constructor itself reads the value, so it reads a parameter property such as this.title too.

What matters is where this.title is read. Inside a function, such as the computed value’s own or an equals written in place, it is read when the function runs, after the constructor. As a value passed when the field is made, such as a computed value’s name, a box’s initial value, or an equals that is itself a parameter property, it is read at once. A field initializer that reads it there does not compile, with TS2729: Property 'title' is used before its initialization, and at run time it reads undefined. That field is declared with its type and made in the constructor body, after super, where the parameter is set.

ts
import { box, type Computed, computed, Model, type Realm } from "@frondruntime/pinna";

class Shelf extends Model {
  readonly $count = box(this.realm, 0);
  // An option is read as the computed value is made. A field initializer runs
  // before `title` is set, so this field is made in the constructor body.
  readonly $label: Computed<string>;

  constructor(
    realm: Realm,
    readonly title: string,
  ) {
    super(realm);
    // The function runs on the first read, after the constructor, so a field
    // initializer's function may read `this.title` too.
    this.$label = computed(realm, (read) => `${this.title}: ${read(this.$count)}`, {
      name: `${title}.label`,
    });
  }
}

const shelf = new Shelf(realm, "Books");

See also