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

Saving state

Example

A workspace keeps a theme and whether the sidebar is open across reloads, in localStorage. The storage is passed in, so a test hands the model one that keeps the saves in memory, or one that refuses them.

ts
interface SettingsStorage {
  readonly load: () => string | undefined;
  // Throws when the storage refuses the write, as a full localStorage does.
  readonly save: (text: string) => void;
}

const browserStorage: SettingsStorage = {
  load: () => localStorage.getItem("workspace") ?? undefined,
  save: (text) => localStorage.setItem("workspace", text),
};

The model saves a computed projection: $saved is the text to store, built from the boxes that are saved and from nothing else. $hovered is shown on screen and never saved: $saved does not read it, so a write to it neither recomputes $saved nor runs the watch.

The constructor and flush read $saved through untracked, which reads without subscribing.

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

type Theme = "light" | "dark";

class Workspace extends Model implements Disposable {
  readonly $theme = box<Theme>(this.realm, "light");
  readonly $sidebarOpen = box(this.realm, true);
  // Shown, never saved: a restart begins with nothing hovered.
  readonly $hovered = box<string | undefined>(this.realm, undefined);
  readonly $storageError = box<string | undefined>(this.realm, undefined);

  // The text to save, and nothing else: it never reads $hovered, so a write
  // to $hovered leaves it, and the watch below, untouched.
  private readonly $saved = computed(this.realm, (read) =>
    JSON.stringify({ theme: read(this.$theme), sidebarOpen: read(this.$sidebarOpen) }),
  );

  private readonly storage: SettingsStorage;
  private readonly stopSaving: () => void;
  private queued = false;
  private lastSaved: string;

  constructor(realm: Realm, storage: SettingsStorage) {
    super(realm);
    this.storage = storage;
    // Restored before the watch starts, so restoring saves nothing back.
    this.restore();
    this.lastSaved = untracked(realm, (read) => read(this.$saved));
    this.stopSaving = watch(
      realm,
      (read) => read(this.$saved),
      () => {
        this.queue();
      },
    );
  }

  private restore() {
    const text = this.storage.load();
    if (text === undefined) return;
    let saved: { readonly theme?: unknown; readonly sidebarOpen?: unknown } | null;
    try {
      saved = JSON.parse(text);
    } catch {
      return; // a damaged save starts from the defaults
    }
    // Each field is taken only when it is valid, so one bad field keeps the rest.
    update(this.realm, (_read, write) => {
      if (saved?.theme === "light" || saved?.theme === "dark") write(this.$theme, saved.theme);
      if (typeof saved?.sidebarOpen === "boolean") write(this.$sidebarOpen, saved.sidebarOpen);
    });
  }

  // A burst of updates in one task queues one save, which reads the end state.
  private queue() {
    if (this.queued) return;
    this.queued = true;
    queueMicrotask(() => this.flush());
  }

  flush() {
    if (!this.queued) return;
    this.queued = false;
    const text = untracked(this.realm, (read) => read(this.$saved));
    if (text === this.lastSaved) return; // the burst ended where it began
    try {
      this.storage.save(text);
      this.lastSaved = text;
      update(this.realm, (_read, write) => write(this.$storageError, undefined));
    } catch (error) {
      // Kept as state for the screen to show; the next change tries again.
      const message = error instanceof Error ? error.message : String(error);
      update(this.realm, (_read, write) => write(this.$storageError, message));
    }
  }

  [Symbol.dispose]() {
    this.stopSaving();
    this.flush(); // a save still queued is written now, not lost
  }
}

The workspace is made once, in module code.

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

const realm = createRealm();
const workspace = new Workspace(realm, browserStorage);

Rules

  • Save a computed that builds the stored text, not the boxes one by one. The watch runs only when that text changes, so state the screen shows but the save leaves out, such as a hover or a selection, writes nothing to the storage.
  • Restore before starting the watch, and set the last saved text from the restored state. A restore after the watch starts writes boxes the watch reads, which queues a save of what the restore loaded.
  • Take each restored field only when it is valid, and keep the default for the rest. A save written by an older version, or edited by hand, then loses one field, not the whole workspace; text that does not parse starts from the defaults.
  • Queue the save with queueMicrotask, read the text when it runs, and skip it when the text equals the last one written. A watch runs once per outermost update, so an event handler that makes several updates runs it several times; the queued save runs once, after the handler returns, and a burst that ends where it began, such as a toggle clicked twice, writes nothing.
  • Catch a storage error in flush and write it to a box. flush runs from a microtask, where a throw reaches no caller; as state, the screen shows it. The last saved text stays as it was, so the next change saves again.
  • Dispose with using or [Symbol.dispose](): it stops the watch, then writes a save still queued, at once. A workspace made right after the dispose loads what it saved, and a test realm’s dispose finds no watch still running.

With Effect

Stored text comes from outside the program: an older version wrote it, or a person edited it. With Effect, restore decodes it through a schema instead of checking each field by hand, so the check and the type come from one declaration and cannot drift apart. Each field recovers from a refusal with Schema.catchDecoding and decodes to no field at all, so one bad field keeps its default and the others still restore.

ts
import { Effect, Option, Schema } from "effect";

// A field the schema refuses decodes to no field at all, so it keeps its
// default and the other fields still restore.
const SavedSettings = Schema.fromJsonString(
  Schema.Struct({
    theme: Schema.optionalKey(
      Schema.Literals(["light", "dark"]).pipe(Schema.catchDecoding(() => Effect.succeedNone)),
    ),
    sidebarOpen: Schema.optionalKey(
      Schema.Boolean.pipe(Schema.catchDecoding(() => Effect.succeedNone)),
    ),
  }),
);

// Text that does not parse decodes to no fields, so the workspace keeps its defaults.
const readSaved = (text: string): typeof SavedSettings.Type =>
  Schema.decodeUnknownOption(SavedSettings)(text).pipe(Option.getOrElse(() => ({})));

restore writes each field readSaved returns, in one update. The boxes stay plain: the workspace writes them itself, so only the restore needs the check.

See also

  • Reacting: watch and when its effect runs.
  • Updates: an effect that cannot be taken back runs after the update.
  • Boxes and computed: a Model and its $ fields.
  • Testing: testRealm, and the dispose that finds a watch still running.
  • Errors: what a schema reports for a field it refuses.