@frondruntime/sorus-effect
v0.3.1
Published
Sorus on Effect Schema: checked boxes, model classes, decoding, and Effect programs.
Maintainers
Readme
@frondruntime/sorus-effect
Sorus on Effect Schema, for an application already on Effect. Sorus is a glitch-free reactive state library for applications; this package adds checked data. It emphasizes:
- Checked writes. A
Box.schemaorModel.boxFieldchecks every write against its schema, and a value the schema rejects throws. When that error leaves the recipe, the update is undone; a recipe that catches it keeps its other writes. - Models from the wire.
Model.decode(realm, schema)(input)makes aModel.Schemaclass, with boxes and keyed collections as fields, from wire data, and throws aDecodeErrorwhen the input does not fit. - Effect programs over state. Watchers and change streams that live for
the caller's scope (
Program.watchScoped,Program.changes), and stages that live for one section and apply when it succeeds (Stage.scoped). - One import. It re-exports every name of
@frondruntime/sorusas it is, soBox.make,Collection.make, andModel.Classcheck nothing here either.
This package is opt-in: @frondruntime/sorus and
@frondruntime/sorus-react import nothing from Effect, and an application
without Effect never installs it. @frondruntime/sorus and effect 4.1 or
a later 4.x are peer dependencies. 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 @frondruntime/sorus-effect effect |
| npm | npm install @frondruntime/sorus @frondruntime/sorus-effect effect |
| pnpm | pnpm add @frondruntime/sorus @frondruntime/sorus-effect effect |
| Yarn | yarn add @frondruntime/sorus @frondruntime/sorus-effect effect |
import { Schema } from "effect";
import { Computed, Model, Realm } from "@frondruntime/sorus-effect";
const realm = Realm.make();
class Todo extends Model.Schema<Todo>("Todo")({
id: Schema.String,
title: Schema.String,
$done: Model.boxField(Schema.Boolean), // a box field; the wire key is "done"
}) {
readonly $label = Computed.make(this.realm, (read) =>
read(this.$done) ? `✓ ${this.title}` : this.title,
);
toggle(): void {
Realm.update(this.realm, (read, write) => {
write(this.$done, !read(this.$done));
});
}
}
// Throws a DecodeError, holding the SchemaError, when the input does not fit.
const todo = Model.decode(realm, Todo)({ id: "1", title: "Write the docs", done: false });
todo.toggle();
const label = Realm.untracked(realm, (read) => read(todo.$label)); // "✓ Write the docs"A single checked box is Box.schema(realm, Schema.Number, 0). The check has
a cost: over seven runs the schema path took 3.3 to 7.5× the plain path's
time to build 10,000 rows, 7.2 to 12.4× to decode, and 2.5 to 3.8× on
toggleAll. Sorus does not skip the check or the tracking for speed, so data
that needs no check goes in a plain Box.make or Model.Class.
Exports
Everything @frondruntime/sorus exports, and:
| Kind | Exports |
| --- | --- |
| Checked state | Box.schema, Collection.schema, Collection.reconcile, whose equals defaults to the schema's |
| Models | Model.Schema, Model.boxField, Model.collectionField, Model.context, Model.decode, Model.restore |
| Effect programs | RealmService, Realm.scoped, Program.watchScoped, Program.changes, Program.updates, Program.fromStream, Program.fromEffect, Program.whileObserved, Source.observeScoped, Stage.scoped, View.orderedScoped, View.groupedScoped, View.totalsScoped |
| Errors | DecodeError |
