# Snapshots

> Read when state goes out as plain data and comes back. Declare a shape of boxes and collections, read it with read.snapshot, and write it back with write.restore.

## Example

A snapshot is the state of a declared shape as plain data. The chat saves its
settings, its rooms, and its `Chat` model's open room and composer, and
restores them. The shape's keys are the snapshot's keys; the hovered room is
not in the shape, so it stays out. No call snapshots a whole realm, which lists
no boxes.

```ts
import { Box, Collection, Realm, type Snapshot } from "@frondruntime/sorus";
import { Chat } from "./model.ts";
import { app } from "./realm.ts";

interface Settings {
  readonly notifications: boolean;
  readonly theme: "light" | "dark";
}

interface Room {
  readonly id: string;
  readonly name: string;
}

const $settings = Box.make<Settings>(
  app,
  { notifications: true, theme: "light" },
  { name: "settings" },
);
const $rooms = Collection.make(app, {
  keyOf: (room: Room) => room.id,
  equals: (previous, next) => previous.name === next.name,
  name: "rooms",
});
// Shown on screen, never saved: it is not in the shape.
const $hoveredRoom = Box.make<string | undefined>(app, undefined, { name: "hoveredRoom" });

const chat = new Chat(app);

// The keys are the snapshot's keys: renaming a box leaves saved data as it was.
// A Model.Class instance declares no fields, so its boxes go in by name.
const shape = {
  settings: $settings,
  rooms: $rooms,
  chat: { roomId: chat.$roomId, composer: chat.$composer },
};

const saved: Snapshot<typeof shape> = Realm.untracked(app, (read) => read.snapshot(shape));
// {
//   settings: { notifications: true, theme: "light" },
//   rooms: [],
//   chat: { roomId: "general", composer: { text: "", replyTo: null } },
// }

const restore = (data: Snapshot<typeof shape>): void => {
  Realm.update(app, (_read, write) => {
    // Every box and row lands, or none: a throw puts the restore back.
    write.restore(shape, data);
  });
};
```

## Rules

- A shape holds boxes, collections, `Model.Schema` instances, and plain
  objects and arrays of them. A computed value, atom, `Model.Class` instance,
  or plain value throws `sorus/snapshot-not-state`; a restore
  writes the boxes a computed value reads, so the shape holds those.
- A snapshot reads as `read` does where it runs. In a computed value it
  tracks every box and collection in the shape, so the value recomputes on a
  write under the shape and on no other.
- The data matches the shape exactly: a missing or added key, or an array of
  another length, throws `sorus/restore-shape`. A partial restore is a restore
  of a smaller shape.
- A collection takes the data's rows by key: a key the data lacks is removed,
  and `equals` decides whether a held row is kept. Plain data for a model row the collection lacks throws
  `sorus/restore-cannot-make-row`.
- A restore writes boxes and rows, and runs no model method. What must happen
  on a change, such as closing a room's socket, belongs in a `Realm.watch`.

## Answers

| Call | Answers |
| --- | --- |
| `read.snapshot(shape)` | The shape as plain data: each box's value, each collection's rows in key order, each `Model.Schema` instance's data under its wire keys. |
| `write.restore(shape, data)` | Writes `data` across `shape` inside the update; returns `undefined`. |
| `Snapshot<Shape>` | The type `read.snapshot(shape)` returns. |
| `RestoreData<Shape>` | The type `write.restore` takes: a snapshot, where a `Model.Schema` instance may stand in for its data. |

## With Effect

`Model.restore` decodes an input and restores it into a held `Model.Schema`
instance in place: see [Classes](../effect/classes.md).

## See also

- [Updates](./updates.md): the update a restore lands with.
- [Stages](./stages.md): `Stage.read` snapshots a stage's state.
- [Classes](../effect/classes.md): `Model.Schema`, `Model.restore`, and the wire form.
- [Saving state](../guides/saving.md): saving a computed projection of what to keep.
- [Error messages](../reference/errors.md#snapshots): every message a snapshot and a restore throw.
