Start
Install
Each package has the same name under every package manager.
bun add @frondruntime/sorus
# or npm install, pnpm add, yarn add@frondruntime/sorus-react adds React hooks, with React 19 or later as a peer
dependency. @frondruntime/sorus-effect adds Effect Schema, with effect 4.1
or a later 4.x as a peer dependency; sorus and sorus-react never import
it, so an application without Effect never installs it.
bun add @frondruntime/sorus @frondruntime/sorus-react
bun add @frondruntime/sorus @frondruntime/sorus-effect effectThe rules for an agent install as skills, one per task: Agent skills.
Example
The samples on these pages build one chat: rooms, messages that stream in over a socket, a composer, and a message that shows as pending until the server takes it. Its boxes hold the open room, the composer’s fields, and the socket’s state.
import { Box } from "@frondruntime/sorus";
import { app } from "./realm.ts";
interface Composer {
readonly text: string;
readonly replyTo: string | null;
}
const $roomId = Box.make(app, "general", { name: "roomId" });
const $composer = Box.make<Composer>(app, { text: "", replyTo: null }, { name: "composer" });
const $connection = Box.make<"connecting" | "open" | "closed">(app, "connecting", {
name: "connection",
});An update writes the boxes; the throw undoes the first write.
import { Realm } from "@frondruntime/sorus";
import { app } from "./realm.ts";
const typeText = (text: string) => {
Realm.update(app, (read, write) => {
write($composer, { ...read($composer), text });
});
};
const openRoom = (roomId: string) => {
Realm.update(app, (_read, write) => {
write($composer, { text: "", replyTo: null });
// Throwing undoes the write above: the composer keeps its text.
if (roomId === "") throw new Error("a room id is not blank");
write($roomId, roomId);
});
};Rules
- Name an observable with a leading
$. Everything else is plain data, so a read of an observable is visible in the code. - Read and write through the place that hands them out:
readandwriteare not methods of an observable, so a misplaced one throws where it is made. - Pass the realm to a call that makes something or opens an update, and the observable to a call about one: there is no default realm to fall back on.
- Import from
@frondruntime/sorusand@frondruntime/sorus-reactfor Effect-free code, and from@frondruntime/sorus-effect, which re-exports every call ofsorus, where a schema checks writes.
Answers
| Call | Answers |
|---|---|
Realm.make() |
The realm that holds every observable it makes: Realms. |
Box.make(realm, initial) |
One value, written only in an update: Boxes and computed. |
Computed.make(realm, (read) => ...) |
A cached value derived from what the function reads: Boxes and computed. |
Realm.update(realm, (read, write) => ...) |
Runs the recipe atomically: Updates. |
useValue($x) |
The value of $x, rerendering when it changes: Hooks. |
See also
- Why Sorus: the principles, their costs, and the packages.
- Coming from MobX: the Sorus call for each MobX one.
- Realms: where state lives and when it ends.
- Boxes and computed: boxes, computed values, atoms, models.
- Schema-checked boxes: the same calls on Effect Schema.