@frondruntime/sorus
v0.3.1
Published
Glitch-free reactive state: boxes, computed values, keyed collections, and updates.
Maintainers
Readme
@frondruntime/sorus
Sorus is a glitch-free reactive state library for applications. It emphasizes:
- Granular reads and updates. Only what read a changed value runs again.
- Cached derived values. A computed value runs once and recomputes only after a source it read changes.
- Observation-driven lifecycles. A source is live while something
observes it, and
Source.onObservedstarts and stops its work. - Hard edges. One way to declare a model, one way to write, and errors instead of silent misuse.
- Code an agent writes and a person reviews. Every read is a
read($x)call and every write sits insideRealm.update, so a mistake is a type error or an error that names the call, not a value that silently stops updating. The cost is more code than implicit tracking.
This package is plain Sorus and imports nothing from Effect. Sorus is before 1.0: any release may change the API. It was called Pinna through 0.2.1.
Packages
| Package | Purpose |
| --- | --- |
| @frondruntime/sorus | The main package: realms, boxes, computed values, atoms, keyed collections, models, views, stages, atomic updates, and watchers. No Effect. |
| @frondruntime/sorus-react | React hooks: useValue, useValues, useSelect. React 19 or later. |
| @frondruntime/sorus-effect | Opt-in, for an application already on Effect: boxes that check an Effect Schema on every write, models decoded from the wire, and Effect programs that react to state. effect 4.1 or a later 4.x. |
| @frondruntime/sorus-core | The raw reactive graph (cells, derived values, atoms, watchers, reactions) with no runtime dependencies, for code that builds on the graph itself. |
Quick start
| Package manager | Command |
| --- | --- |
| Bun | bun add @frondruntime/sorus |
| npm | npm install @frondruntime/sorus |
| pnpm | pnpm add @frondruntime/sorus |
| Yarn | yarn add @frondruntime/sorus |
import { Box, Computed, Realm, Source } from "@frondruntime/sorus";
const realm = Realm.make();
const $count = Box.make(realm, 0);
const $double = Computed.make(realm, (read) => read($count) * 2);
Realm.update(realm, (read, write) => {
write($count, read($count) + 1);
});
Source.peek($double); // 2A box changes only inside Realm.update, through the write it hands out.
A write anywhere else, or a write kept past its update, throws and says
where to write instead. The recipe is atomic: watchers see its end state
once, and a recipe that throws is undone, except a write.changed on an
atom, whose watchers still run. For React, add
@frondruntime/sorus-react; for boxes that check a schema,
@frondruntime/sorus-effect.
Building blocks
| Concept | Made with | What it is |
| --- | --- | --- |
| Realm | Realm.make() | Holds every observable it makes, with its updates, tracking, watchers, and errors. There is no default realm. |
| Box | Box.make(realm, initial) | One value, written only inside Realm.update. A write equal to the current value under Object.is notifies nobody. |
| Computed value | Computed.make(realm, (read) => ...) | A cached value derived from what its function reads. |
| Atom | Atom.make(realm) | A change signal for state kept outside Sorus: read($atom) subscribes, and write.changed($atom) reruns what observed it. |
| Collection | Collection.make(realm, { keyOf }) | Rows by key. $items.keys and $items.rows are computed values of the keys, and of every row, in order. |
| Model | class Counter extends Model.Class | A class whose fields are made against this.realm and whose methods write through Realm.update. |
| View | View.ordered, View.grouped, View.totals | A sorted order, the keys of each group, or totals per group over a collection, kept a row at a time. |
| Stage | Stage.make(realm) | Holds the writes of asynchronous work: Stage.apply lands them in one update, Stage.drop discards them. |
Costs
- Adding or removing rows in a large collection is the weak spot, not
table size or update rate. A collection changes its key order in place
while nothing holds it. A read of
keys(an autorun, a computed value, a view), an open visible stage, or aRealm.onUpdatelistener holds it, and the first add or remove after that read copies the order: about 15 µs at 1,000 rows and 120 µs at 10,000 on Hermes, measured with the order held. A remove that copies nothing still finds and splices the key, linear in the number of rows. Many new rows go in through onewrite.replaceAll, which copies once. - Explicit reads cost more code per read and write, by design.
Exports
| Entry | Exports |
| --- | --- |
| @frondruntime/sorus | Realm (make, update, untracked, watch, autorun, onUpdate, isTracking, status, dispose), Box.make, Computed (make, fn, fromPromise), Atom.make, Collection (make, reconcile, onRowChange), Model.Class, View (ordered, grouped, totals, status, dispose), Stage (make, update, read, realmRead, apply, drop, status, inspect), Source (peek, observe, onObserved, inspect); the errors SorusError, SorusUsageError, ValueRejectedError, StageClosedError, StageConflictError, and isSorusError; and their types |
| @frondruntime/sorus/testing | testRealm, record, held, retainedKeys |
| @frondruntime/sorus/internal | What sorus-effect and sorus-react build on; not for applications |
AGENTS.md, shipped in this package, holds the rules for agents writing
applications on Sorus. The agent skills carry them one task at a time:
npx skills add frondruntime/sorus.
Documentation
- Start: https://sorus.frondruntime.dev/docs/start
- Why Sorus, with what each rule costs: https://sorus.frondruntime.dev/docs/start/why
- Every error message: https://sorus.frondruntime.dev/docs/reference/errors
