# Updates

> Read when writing state. Write boxes and rows in one update, draft arrays and objects, await before an update, rely on an update being undone when it throws, and run effects after it ends.

## Example

A box has one way in, `Realm.update`, which hands out `read` and `write`.
Sending a message writes the message and clears the composer in one update, so
no watcher sees one without the other.

```ts
import { Realm } from "@frondruntime/sorus";
import { $messages } from "./messages.ts";
import { app } from "./realm.ts";
import { $composer, $roomId } from "./state.ts";

// One update writes the sent message and clears the composer: no watcher sees
// one without the other. The id and the time are made by the caller.
const send = (id: string, sentAt: number) => {
  Realm.update(app, (read, write) => {
    const { text } = read($composer);
    write.upsert(
      $messages,
      { id, roomId: read($roomId), author: "me", text, sentAt },
      { at: "end" },
    );
    write($composer, { text: "", replyTo: null });
  });
};
```

An array or a plain object is changed through a draft from `write.modify`.

```ts
import { Box, Realm } from "@frondruntime/sorus";
import { app } from "./realm.ts";
import { $composer } from "./state.ts";

const $typing = Box.make<Array<string>>(app, [], { name: "typing" });

const replyTo = (messageId: string) => {
  Realm.update(app, (_read, write) => {
    write.modify($composer, (draft) => {
      draft.replyTo = messageId;
    });
  });
};

const startTyping = (author: string) => {
  Realm.update(app, (_read, write) => {
    write.modify($typing, (authors) => {
      authors.push(author);
    });
  });
};
```

## Rules

- Pass `read` and `write` down as arguments, never keep them: outside an
  update a kept `write` throws; a kept `read` acts as another place's own.
- Change a value with `write` or `write.modify`, never in place: a value is
  not a proxy, so no watcher sees an in-place change.
- A box's value is a tree: an assigned object is copied on its first write
  through a draft. Refer to one object by a key, row, or box:
  a write through one key is not seen at the other.
- An update is synchronous: a recipe returning a promise throws. Await first,
  then update, from an event, a watch's effect, or an autorun.
- Run an effect that cannot be taken back, such as a request, after the
  update: a throw undoes the writes, not what the recipe sent.

## Answers

| Call | Answers |
| --- | --- |
| `Realm.update(realm, (read, write) => ..., { name? })` | Runs the recipe atomically: a throw restores every box it wrote, but not a `write.changed`. A nested call joins the outermost update, whose end runs watchers once; `name` names its record ([Changes](./changes.md)). |
| `write($box, value)` | Replaces the value: a number, a string, a boolean, or a whole new array or object. |
| `write.modify($box, (draft) => ...)` | Writes the drafted array or plain object back once. Any other value throws `sorus/modify-not-draftable`. |
| `Write` | The type of `write`, for a function an update hands it to. |
| `UpdateRecipe` | `(read, write) => undefined`: what `Realm.update` and `Stage.update` run. |

## See also

- [Collections](./collections.md): `write.upsert`, `write.remove`, and `write.replaceAll`.
- [Stages](./stages.md): writes held until asynchronous work succeeds.
- [Reacting](./reacting.md): watchers that run after an update.
- [Boxes and computed](./boxes.md): the boxes an update writes.
- [Error messages](../reference/errors.md): draft errors and `sorus/update-in-derivation`.
