v0 · Developer PreviewSorus is under active development. APIs may change between releases.

Coming from MobX

Example

The chat’s composer in MobX: an observable class, a getter, two actions, a reaction, and an observer component.

tsx
import { makeAutoObservable, reaction } from "mobx";
import { observer } from "mobx-react-lite";

class ChatStore {
  roomId = "general";
  composer = { text: "", replyTo: null as string | null };

  constructor() {
    makeAutoObservable(this);
  }

  get hasText() {
    return this.composer.text.trim() !== "";
  }

  type(text: string) {
    this.composer.text = text;
  }

  openRoom(roomId: string) {
    this.roomId = roomId;
    this.composer = { text: "", replyTo: null };
  }
}

const store = new ChatStore();

reaction(
  () => store.roomId,
  (roomId) => console.log(`opened ${roomId}`),
);

const StoreSendButton = observer(function StoreSendButton() {
  return (
    <button type="submit" disabled={!store.hasText}>
      Send
    </button>
  );
});

In Sorus the same composer is the Chat model: the fields that change are boxes named with $, the getter is a computed value, and each action calls Realm.update.

ts
import { Box, Computed, Model, Realm } from "@frondruntime/sorus";
import type { Composer } from "./state.ts";

class Chat extends Model.Class {
  readonly $roomId = Box.make(this.realm, "general", { name: "roomId" });
  readonly $composer = Box.make<Composer>(
    this.realm,
    { text: "", replyTo: null },
    {
      name: "composer",
    },
  );
  readonly $hasText = Computed.make(this.realm, (read) => read(this.$composer).text.trim() !== "");

  type(text: string): void {
    Realm.update(this.realm, (_read, write) => {
      write.modify(this.$composer, (draft) => {
        draft.text = text;
      });
    });
  }

  openRoom(roomId: string): void {
    Realm.update(this.realm, (_read, write) => {
      write(this.$roomId, roomId);
      write(this.$composer, { text: "", replyTo: null });
    });
  }
}

Rules

  • Read through read and write through write, never by property access or assignment: store.hasText becomes read(chat.$hasText) or useValue(chat.$hasText), an assignment write.modify(chat.$composer, ...) in a Realm.update. A read is tracked only through a read it is handed.
  • A box holds plain data and belongs to one realm: there are no deep proxies (a keyed list is a collection) and no global state, so two realms share no batch, tracking, or watcher.
  • Realm.watch runs for changes only. Where MobX passes fireImmediately: true, pass { immediate: true }.
  • flow has no counterpart: await outside, then one Realm.update per step, checking an AbortController before each write (Live data).

Answers

MobX Sorus
observable.box Box.make(realm, initial)
makeAutoObservable, makeObservable, @observable class X extends Model.Class with $ fields; with Effect, Model.boxField(schema) in a Model.Schema
observable arrays and objects a Box.make of plain data, written whole or through write.modify
ObservableMap Collection.make(realm, { keyOf }), or a box of an array written through Collection.reconcile; with Effect, a Model.collectionField
computed, a getter Computed.make(realm, (read) => ...)
computedFn (mobx-utils) Computed.fn
action, runInAction Realm.update(realm, (read, write) => ...)
flow an async function that awaits outside and opens one Realm.update per step
reaction(select, effect) Realm.watch(realm, (read) => ..., effect)
autorun Realm.autorun(realm, (read) => ...)
when, a reaction with delay a Realm.watch; with Effect, Program.changes($x)
untracked Realm.untracked(realm, (read) => ...)
onBecomeObserved, onBecomeUnobserved Source.onObserved($x, start) (Lifecycles)
computed({ keepAlive: true }) Source.observe($x), released when done
createAtom Atom.make(realm)
fromPromise (mobx-utils) Computed.fromPromise(realm, (signal) => fetch(url, { signal }), initial) (Async state)
fromStream, fromResource (mobx-utils) Program.fromStream from sorus-effect
trace, getDependencyTree Source.inspect($x)
onReactionError Realm.make({ onError })
observer(Component) nothing: useValue($x) for each value the component shows
useLocalObservable useState(() => Box.make(realm, ...)), with the realm from useRealm()
toJS nothing: a box holds plain data

With Effect

An application on Effect Schema writes the store as a Model.Schema with Model.boxField fields, so every write is checked: see Classes.

See also

  • Start: a realm, boxes, and a computed value.
  • Boxes and computed: the Chat model and its fields.
  • Updates: atomic updates, where action maps.
  • Classes: Model.Schema, Model.boxField, and Model.decode.
  • Live data: a WebSocket feed and a fetch that refetches, with and without Effect, where MobX would use flow.