typeonce.devypeonce
Menu

Effect Machine

Schema-first state machines and statecharts for Effect.

Effect-native, completely type-safe state machines and statecharts, inspired by XState. Schemas define the protocol, the root defines the topology, and the result runs as an Effect-managed machine.

pnpm add @typeonce/effect-machine

LockedUnlockedCoinPushEffect MachineSchema-first state machines for Effect

Why Effect Machine

  • Schemas first

    States and events are Effect schemas, so every transition is typed from the protocol down.

  • Runs as an Effect

    Machines start inside Effect and use its services, scopes, and errors. No second runtime.

  • React without re-renders

    Own machine atoms in React without subscribing their owners to machine state.

  • Devtools and lint rules

    A local machine visualizer, and lint rules that catch structural mistakes in models.

A first look

turnstile.ts
import { Machine } from "@typeonce/effect-machine"
import { Effect } from "effect"
const Root = Machine.state({
  states: { Locked: {}, Unlocked: {} }
})
const Events = Machine.events({ Coin: {}, Push: {} })
const Turnstile = Machine.make({
  root: Root,
  events: Events
}).handle({
  initial: {
    target: "Locked"
  },
  states: {
    Locked: { on: { Coin: { target: "Unlocked" } } },
    Unlocked: { on: { Push: { target: "Locked" } } }
  }
})
const program = Effect.gen(function*() {
  const ref = yield* Machine.start(Turnstile)
  yield* ref.send(Events.Coin())
})

Packages