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

Pinna forEffect.

@frondruntime/pinna-effect puts synchronous domain state on Effect Schema: a model is a Schema.Class whose fields are boxes that check every write, and programs that react to it stop with their scope. It exports every name of @frondruntime/pinna, so an application imports from one package.

install; effect 4.1 or a later 4.x is a peer dependency
bun add @frondruntime/pinna @frondruntime/pinna-effect effect
models

Models on Schema.

One class decodes server data, checks each write to its boxes, derives values from them, and encodes back.

Classes
  • A model is a Schema.Class.

    SchemaModel is an Effect Schema.Class whose $ fields hold state: a BoxField is a box, a CollectionField holds rows by key. Plain fields stay plain.

    class.test.ts
  • Every write is checked.

    A write the field's schema refuses throws to the caller, with the Schema.SchemaError as its cause, and the whole update is undone.

    box.test.ts
  • The wire is the schema.

    decode takes JSON to a model in a realm and throws Schema.SchemaError on input that does not fit; Schema.encodeSync takes the model back to JSON with the boxes' current values.

    mix.test.ts
an order, its lines, a checked write, and the wire
import {
  BoxField,
  CollectionField,
  computed,
  createRealm,
  decode,
  SchemaModel,
  update,
} from "@frondruntime/pinna-effect";
import { Schema } from "effect";

// A line holds a plain field, and a box field whose schema checks every write.
class Line extends SchemaModel<Line>("Line")({
  sku: Schema.String,
  $quantity: BoxField(Schema.Int.check(Schema.isGreaterThan(0))),
}) {}

// An order holds its lines by key, and derives the units from them.
class Order extends SchemaModel<Order>("Order")({
  id: Schema.String,
  $lines: CollectionField(Line, { keyOf: (line) => line.sku }),
}) {
  readonly $units = computed(this.realm, (read) =>
    read(this.$lines.rows).reduce((units, line) => units + read(line.$quantity), 0),
  );

  // A quantity the schema refuses, such as 0, throws to the caller with the
  // Schema.SchemaError as its cause, and the update is undone.
  setQuantity(sku: string, quantity: number): void {
    update(this.realm, (read, write) => {
      const line = read.row(this.$lines, sku);
      if (line !== undefined) write(line.$quantity, quantity);
    });
  }
}

const realm = createRealm();

// JSON in, a checked Order out; input that does not fit throws a Schema.SchemaError.
const order = decode(realm, Order)({ id: "7", lines: [{ sku: "pen", quantity: 2 }] });
Schema.encodeSync(Order)(order); // { id: "7", lines: [{ sku: "pen", quantity: 2 }] }
programs

Programs that stop with their scope.

Effect programs react to the state, feed it from streams, and stop when their scope closes, so nothing keeps a stop function.

Programs
  • The realm is a service.

    RealmService carries the realm. Layer.succeed(RealmService, realm) provides it to the programs, and a test provides its own realm.

    effect.test.ts
  • Programs stop with their scope.

    watchScoped runs an effect on each change and interrupts the one still running. changes streams a value, starting with the current one. observeScoped holds a value observed until the scope closes.

    effect.test.ts
  • A stream becomes a value.

    fromStream runs its stream while the value is observed and resets the value to initial when the last observer leaves. whileObserved runs a program on the same lifetime, to fill a collection only while a list shows it.

    effect.test.ts
a save on each change, and a rate from a stream
import { fromStream, observeScoped, RealmService, watchScoped } from "@frondruntime/pinna-effect";
import { Effect, Layer, Stream } from "effect";

// The realm is a service: one layer provides it, and a test provides its own.
const RealmLive = Layer.succeed(RealmService, realm);

// Saves on each change of the units; a newer change interrupts the save still running.
const save = watchScoped(
  realm,
  (read) => read(order.$units),
  (units) => saveUnits(order.id, units),
);

// A value fed by a stream while something reads it. It has no error channel,
// so the failure becomes the last element, and the screen shows it.
type Rate =
  | { readonly _tag: "Loading" }
  | { readonly _tag: "Rate"; readonly rate: number }
  | { readonly _tag: "Unavailable"; readonly reason: string };

const rate = fromStream<Rate>(
  realm,
  rates.pipe(
    Stream.map((rate): Rate => ({ _tag: "Rate", rate })),
    Stream.catch((error) => Stream.succeed<Rate>({ _tag: "Unavailable", reason: error.reason })),
  ),
  { _tag: "Loading" },
);

// The watch, the stream, and the hold stop when the scope closes.
const main = Effect.gen(function* () {
  yield* save;
  const $rate = yield* rate;
  yield* observeScoped($rate); // the stream runs, rendered or not
});
const fiber = Effect.runFork(
  main.pipe(Effect.andThen(Effect.never), Effect.scoped, Effect.provide(RealmLive)),
);
reference

Reference.

Each page states the rules for one part of @frondruntime/pinna-effect, with the reason for each.