|
| 1 | +# @typeonce/effect-machine |
| 2 | + |
| 3 | +External home for the schema-first Machine API proposed in |
| 4 | +[Effect PR #6429](https://github.com/Effect-TS/effect/pull/6429). During |
| 5 | +incubation this repository is the canonical implementation; the synchronization |
| 6 | +tool keeps the Effect PR mechanically aligned without a second logic |
| 7 | +implementation. |
| 8 | + |
| 9 | +> Early-release software: APIs may change, and releases are coupled to an exact |
| 10 | +> Effect beta. |
| 11 | +
|
| 12 | +## Installation |
| 13 | + |
| 14 | +```sh |
| 15 | +pnpm add @typeonce/effect-machine effect@4.0.0-beta.102 |
| 16 | +``` |
| 17 | + |
| 18 | +`effect` is an exact peer dependency, not a bundled runtime dependency. Consumers |
| 19 | +must install `effect@4.0.0-beta.102`. Upgrading this package may require upgrading |
| 20 | +Effect in lockstep; do not override the peer to another beta. |
| 21 | + |
| 22 | +## Entrypoints |
| 23 | + |
| 24 | +```ts |
| 25 | +import { Machine } from "@typeonce/effect-machine" |
| 26 | +import { AtomMachine } from "@typeonce/effect-machine/reactivity" |
| 27 | +import { ClusterMachine } from "@typeonce/effect-machine/cluster" |
| 28 | +``` |
| 29 | + |
| 30 | +Each ESM entrypoint is independent and tree-shakeable. Importing the root does |
| 31 | +not load the reactivity or cluster adapters. |
| 32 | + |
| 33 | +## Basic machine |
| 34 | + |
| 35 | +```ts |
| 36 | +import { Effect, Schema } from "effect" |
| 37 | +import { Machine } from "@typeonce/effect-machine" |
| 38 | + |
| 39 | +class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {} |
| 40 | +class Running extends Schema.TaggedClass<Running>("Running")("Running", {}) {} |
| 41 | +class Start extends Schema.TaggedClass<Start>("Start")("Start", {}) {} |
| 42 | + |
| 43 | +const states = Machine.defineStates({ Idle, Running }) |
| 44 | + |
| 45 | +const Counter = Machine.make({ |
| 46 | + id: "Counter", |
| 47 | + states: states.states, |
| 48 | + events: [Start], |
| 49 | + initial: states.initial.Idle(new Idle()) |
| 50 | +}).handle({ |
| 51 | + Idle: { |
| 52 | + on: { |
| 53 | + Start: ({ target }) => Effect.succeed(target.full.Running(new Running())) |
| 54 | + } |
| 55 | + } |
| 56 | +}) |
| 57 | +``` |
| 58 | + |
| 59 | +The exported namespaces preserve the API, type identifiers, service keys, |
| 60 | +semantics, and documentation of the Effect proposal. |
| 61 | + |
| 62 | +## Development and validation |
| 63 | + |
| 64 | +Use pnpm 10 and Node.js 20 or newer: |
| 65 | + |
| 66 | +```sh |
| 67 | +pnpm install --frozen-lockfile |
| 68 | +pnpm check |
| 69 | +``` |
| 70 | + |
| 71 | +Individual commands are available for `build`, `test`, `test:types`, |
| 72 | +`typecheck`, `format:check`, `test:consumer`, `test:sync`, `sync:check`, and |
| 73 | +`pack:check`. Runtime tests use `@effect/vitest`; type tests use TSTyche and |
| 74 | +TypeScript 6.0.3. |
| 75 | + |
| 76 | +## Synchronizing Effect PR #6429 |
| 77 | + |
| 78 | +Make logic changes here first and run `pnpm check`. Then generate the mapped |
| 79 | +production files, runtime tests, and type tests into a writable Effect checkout: |
| 80 | + |
| 81 | +```sh |
| 82 | +pnpm sync:effect -- /path/to/effect |
| 83 | +``` |
| 84 | + |
| 85 | +The explicit mapping covers only the eight production files and six test files. |
| 86 | +It rewrites package imports to Effect repository-relative `.ts` imports and |
| 87 | +restores Effect's private `PipeInspectableProto` boundary. It never copies |
| 88 | +package metadata, documentation, release files, or changesets. |
| 89 | + |
| 90 | +Check an existing checkout without writing: |
| 91 | + |
| 92 | +```sh |
| 93 | +pnpm sync:effect -- --check /path/to/effect |
| 94 | +``` |
| 95 | + |
| 96 | +Check mode exits non-zero on any drift. `pnpm test:sync` exercises generation, |
| 97 | +a clean check, and drift detection in a disposable temporary directory. Never |
| 98 | +generate into a checkout with work you have not reviewed. After generation, |
| 99 | +inspect the Effect diff and run its targeted machine, reactivity, cluster, and |
| 100 | +type-test validations followed by `pnpm check`. |
| 101 | + |
| 102 | +The current source reference is branch `sandro/state-charts` in the Effect |
| 103 | +repository. Exact dependency pins and the sync check are the proof boundary; |
| 104 | +vendoring the rest of Effect is intentionally unnecessary. |
| 105 | + |
| 106 | +## Releases |
| 107 | + |
| 108 | +Add a changeset with `pnpm changeset`. CI validates frozen installation and the |
| 109 | +complete check suite. The release workflow opens version PRs and publishes with |
| 110 | +npm provenance through GitHub Actions. |
| 111 | + |
| 112 | +Before the first release, create the `typeonce-dev/effect-machine` GitHub |
| 113 | +repository and configure npm trusted publishing for the repository and |
| 114 | +`.github/workflows/release.yml` environment. No npm token is intended. |
| 115 | + |
| 116 | +When equivalent Machine modules ship in Effect, this package is intended to |
| 117 | +become a thin compatibility re-export package before eventual retirement. |
0 commit comments