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

Coming from MobX

Example

A cart in MobX: an observable class, a getter, an action, a reaction, and an observer component.

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

class CartStore {
  quantity = 1;
  price = 10;

  constructor() {
    makeAutoObservable(this);
  }

  get total() {
    return this.quantity * this.price;
  }

  add() {
    this.quantity += 1;
  }
}

const store = new CartStore();

reaction(
  () => store.total,
  (total) => console.log(`total: ${total}`),
);

const StoreTotal = observer(function StoreTotal() {
  return (
    <button type="button" onClick={() => store.add()}>
      {store.total}
    </button>
  );
});

The same cart in Pinna. The fields that change are boxes, named with $. The getter is a computed value, the action a method that calls update, the reaction a watch. The component reads with useValue and needs no wrapper. The cart is a Model made from a realm, so a test or a screen can make one of its own.

tsx
import { box, computed, createRealm, Model, update, watch } from "@frondruntime/pinna";
import { useValue } from "@frondruntime/pinna-react";

class Cart extends Model {
  readonly $quantity = box(this.realm, 1);
  readonly $price = box(this.realm, 10);
  readonly $total = computed(this.realm, (read) => read(this.$quantity) * read(this.$price));

  add(): void {
    update(this.realm, (read, write) => {
      write(this.$quantity, read(this.$quantity) + 1);
    });
  }
}

const realm = createRealm();
const cart = new Cart(realm);

watch(
  realm,
  (read) => read(cart.$total),
  (total) => {
    console.log(`total: ${total}`);
  },
);

function CartTotal() {
  const total = useValue(cart.$total);
  return (
    <button type="button" onClick={() => cart.add()}>
      {total}
    </button>
  );
}

Rules

  • Nothing is read by property access. store.total becomes read(cart.$total) in a place, or useValue(cart.$total) in a component.
  • Nothing is written by assignment. store.quantity += 1 becomes write(cart.$quantity, ...) in an update.
  • There are no deep proxies. A box holds plain data, replaced whole or through write.modify. A keyed list is a collection, tracked row by row: see Collections.
  • An update is synchronous and atomic. An update that throws is undone: every write before the throw in the same update is taken back.
  • There is no global state. Every observable belongs to the realm that made it, as with configure({ isolateGlobalState: true }) for every store.
  • watch runs for changes, not for the current value. Where MobX would pass fireImmediately, call the effect once by hand.
  • flow has no counterpart: await outside, then one update per step. For cancellation, write the work as an Effect program.
  • An autorun writes through update, whose reads do not subscribe it.
  • toJS is not needed: values are plain data already.

Answers

MobX Pinna
observable.box box(realm, initial)
makeAutoObservable, makeObservable, @observable class X extends Model with $ fields made by box(this.realm, ...) and computed(this.realm, ...); with Effect, BoxField(schema) in a SchemaModel
observable arrays and objects a box of plain data, written whole or through write.modify
ObservableMap collection(realm, { keyOf }), or a box of an array written through reconcile; with Effect, a CollectionField
computed, a getter computed(realm, (read) => ...)
computedFn (mobx-utils) computedFunction
action, runInAction update(realm, (read, write) => ...)
reaction(select, effect) watch(realm, (read) => ..., effect)
autorun autorun(realm, (read) => ...)
when, a reaction with delay a watch; with Effect, changes($x) and an Effect Stream
untracked untracked(realm, (read) => ...)
onBecomeObserved, onBecomeUnobserved onObserved($x, start): start runs when $x gains its first observer and returns the cleanup, which runs when the last leaves; onObserved returns the function that removes the hook
computed({ keepAlive: true }) observe($x), released when done
createAtom atom(realm)
fromStream, fromResource (mobx-utils) fromStream from pinna-effect
trace, getDependencyTree inspect($x)
onReactionError createRealm({ onError })
observer(Component) useValue($x) for each value the component shows
useLocalObservable useState(() => box(realm, ...)), with the realm from useRealm()

With Effect

An application on Effect Schema writes the store as a SchemaModel: its $ fields are BoxField(schema), every write is checked, and decode makes it from server data. This is the same cart, with the watch and the component as above.

ts
import { BoxField, computed, decode, SchemaModel, update, watch } from "@frondruntime/pinna-effect";
import { Schema } from "effect";

class Cart extends SchemaModel<Cart>("Cart")({
  $quantity: BoxField(Schema.Int),
  $price: BoxField(Schema.Number),
}) {
  readonly $total = computed(this.realm, (read) => read(this.$quantity) * read(this.$price));

  add(): void {
    update(this.realm, (read, write) => {
      write(this.$quantity, read(this.$quantity) + 1);
    });
  }
}

const cart = decode(realm, Cart)({ quantity: 1, price: 10 });

watch(
  realm,
  (read) => read(cart.$total),
  (total) => {
    console.log(`total: ${total}`);
  },
);

See also

  • Start: a realm, a box, and a computed value.
  • Updates: drafts and atomic updates.
  • Classes: SchemaModel, BoxField, and decode.
  • Live data: a WebSocket feed and a fetch that refetches, with and without Effect, where MobX would use flow.