Skip to content
pyreon
Install
@pyreon/machine
fundamentals · state

The spinner and the error, on screen together? Not any more.

A machine is a signal that can only hold the states you declare and only moves along the events you allow. Read it like any signal, render from matches(), and let can() and nextEvents() enable the buttons. Against XState it sends events 34.7× faster.

bun add @pyreon/machine
Documentation
model
a signal with declared statesmachine() reads it; effects and JSX track it.
vs XState 5
34.7× faster sendbun, one run; faster on all four ops.
types
states and events inferredNo codegen, no typegen file.
native
iOS and Android, experimentalCompiles to a native PyreonMachine.
A state machine steps only along the transitions it declares Schematic of a createMachine fetcher with four states: idle, loading, success and error. Events FETCH, REJECT, RETRY, RESOLVE and RESET light the transition they take, one at a time. A side panel shows machine() and nextEvents() for the current state, and a FETCH sent while loading is ignored, so the spinner and the error are never on screen together. CREATEMACHINE · A SIGNAL WITH DECLARED STATES FETCHREJECTRESOLVERETRYRESET idleloadingsuccesserror send('FETCH')not handled · stays loadingmachine()nextEvents()'idle'FETCH'loading'RESOLVEREJECT'error'RETRY'success'RESET render from matches() · enable buttons with can() · no actor to start
  • 34.7× faster send than XState 5.BENCHMARKS.md §10 machine row, overnight run 2026-11-05, bun/JavaScriptCore, one run
  • 11.4× faster to create a machine.BENCHMARKS.md §10 machine row, one run
  • 6.0× faster can(); matches() 4.7×.BENCHMARKS.md §10 machine row, one run

synthetic · author-run · M3 Max · record of 2026-11-05

Every result, including where we lose →
why teams pick it

Booleans don't scale. States do.

  1. One value, not four flags

    loading, error and done can all be true when a slow response lands after a retry. A machine is in exactly one state, and an event the current state doesn't handle changes nothing.

  2. Buttons that know when they work

    can('PAY', payload) runs the guard and predicts exactly what send would do; nextEvents() lists what is possible now. Both are reactive, so disabled={() => !checkout.can('PAY')} stays right.

  3. Branch states, no glue

    always: [{ target: 'empty', guard: () => cart().length === 0 }, 'ready'] routes the moment a state is entered, reading ordinary signals. send returns the settled state after the cascade.

  4. No actor to start

    No interpreter, no createActor().start(), no context object. Data lives in signals beside the machine; side effects hang off onEnter, onExit and onTransition.

the difference

Three flags, or one machine.

boolean flags

Nothing stops two of these being true. This snippet runs and prints both.

import { signal } from '@pyreon/reactivity'

const loading = signal(false)
const error = signal<Error | null>(null)
const done = signal(false)

// a slow first request finishes after a retry started:
loading.set(true)
error.set(null)
done.set(true) // spinner and "Saved!" on screen together
console.log({ loading: loading(), done: done() })
with @pyreon/machine

Only the transitions you wrote can happen. FETCH while loading does nothing.

import { createMachine } from '@pyreon/machine'
import { effect } from '@pyreon/reactivity'

const fetcher = createMachine({
  initial: 'idle',
  states: {
    idle: { on: { FETCH: 'loading' } },
    loading: { on: { SUCCESS: 'done', ERROR: 'error' } },
    error: { on: { RETRY: 'loading' } },
    done: { final: true },
  },
})

effect(() => console.log(fetcher())) // it is a signal; no actor to start

fetcher.send('FETCH') // 'loading'
fetcher.send('FETCH') // not handled in 'loading': stays 'loading'
console.log(fetcher.matches('loading'), fetcher.can('SUCCESS'))
coming from

Coming from XState?

Same states block. No actor, no event objects.

XState
import { createActor, createMachine } from 'xstate'

const fetcher = createMachine({
  id: 'fetcher',
  initial: 'idle',
  states: {
    idle: { on: { FETCH: 'loading' } },
    loading: { on: { SUCCESS: 'done', ERROR: 'error' } },
    error: { on: { RETRY: 'loading' } },
    done: { type: 'final' },
  },
})

const actor = createActor(fetcher).start()
actor.subscribe((snapshot) => console.log(snapshot.value))

actor.send({ type: 'FETCH' }) // 'loading'
actor.send({ type: 'FETCH' }) // not handled in 'loading': stays 'loading'
console.log(actor.getSnapshot().matches('loading'), actor.getSnapshot().can({ type: 'SUCCESS' }))
Pyreon

The states config ports almost line for line (type: 'final' becomes final: true). send({ type }) becomes send(type, payload?), and the machine is read like a signal. Statecharts, context and actors have no equivalent here; if you use them, keep XState.

import { createMachine } from '@pyreon/machine'
import { effect } from '@pyreon/reactivity'

const fetcher = createMachine({
  initial: 'idle',
  states: {
    idle: { on: { FETCH: 'loading' } },
    loading: { on: { SUCCESS: 'done', ERROR: 'error' } },
    error: { on: { RETRY: 'loading' } },
    done: { final: true },
  },
})

effect(() => console.log(fetcher())) // it is a signal; no actor to start

fetcher.send('FETCH') // 'loading'
fetcher.send('FETCH') // not handled in 'loading': stays 'loading'
console.log(fetcher.matches('loading'), fetcher.can('SUCCESS'))
try it

Drive the checkout.

Only valid events are enabled. That list is nextEvents(), and it's reactive.

states · final: paid
  1. cart
  2. details
  3. paying
  4. declined
  5. paid
nextEvents() → [CHECKOUT]
onTransition
  1. Send CHECKOUT to start.
benchmarks

34.7× faster send than XState, and faster on all four ops.

A machine here is a constrained signal, not an interpreter with an actor model, so it does less work per event. It also does less: no statecharts, no actors. Compare features before you compare numbers.

vs XState 5.33 · bun / JavaScriptCore, one process per opPyreonrival · lower is better
  • send
    win34.7×
  • create
    win11.4×
  • can
    win6.0×
  • matches
    win4.7×
Apple M3 Max · record of 2026-11-05 · overnight run, one run · author-run, syntheticFull method, every loss →
what's included

Everything, and nothing extra.

  • createMachine({ initial, states }) with state and event names inferred from the config.
  • send(event, payload?) returns the settled state; unhandled events are no-ops.
  • Guards { target, guard }; a guard that throws denies instead of crashing send.
  • can(event, payload?) predicts send exactly, guard included.
  • matches(...states) and nextEvents() reactive reads for rendering.
  • Eventless always transitions for branch states; a runaway loop throws after 1,000 steps.
  • Final states final: true, isFinal(), onDone.
  • Lifecycle listeners onEnter, onExit (before the next onEnter), onTransition; each returns an unsubscribe.
  • Run to completion an event sent from inside a listener is queued and handled after the current one, in order.
  • StateOf / EventOf type helpers that work on an instance or a config.
recipes

Things to build with it.

A checkout that routes itself

A branch state reads the cart and the user, a guard checks the payment payload, and onTransition logs every hop.

import { createMachine } from '@pyreon/machine'
import { signal } from '@pyreon/reactivity'

const cart = signal<string[]>([])
const user = signal<string | null>(null)

const checkout = createMachine({
  initial: 'validating',
  states: {
    // routes itself the moment it is entered
    validating: {
      always: [
        { target: 'empty', guard: () => cart().length === 0 },
        { target: 'needsLogin', guard: () => !user() },
        'ready',
      ],
    },
    empty: { on: { ADD: 'validating' } },
    needsLogin: { on: { LOGIN: 'validating' } },
    ready: { on: { PAY: { target: 'paying', guard: (p) => (p as { total: number }).total > 0 } } },
    paying: { on: { PAID: 'done', DECLINED: 'ready' } },
    done: { final: true },
  },
})

checkout.onTransition((from, to, event) => console.log(`${from} → ${to} (${event.type})`))

cart.set(['book'])
checkout.send('ADD') // settles in 'needsLogin', after passing through 'validating'
user.set('ada')
checkout.send('LOGIN') // 'ready'
console.log(checkout.nextEvents(), checkout.can('PAY', { total: 42 })) // ['PAY'] true
checkout.send('PAY', { total: 0 }) // guard says no: still 'ready'
checkout.send('PAY', { total: 42 }) // 'paying'
api

One function, fully typed.

Every export has a reference page with its signature, an example and the mistakes people make with it.

create · 1
  • createMachine
read · 5
  • machine()
  • matches
  • can
  • nextEvents
  • isFinal
drive · 3
  • send
  • reset
  • dispose
listen · 4
  • onEnter
  • onExit
  • onTransition
  • onDone
types · 5
  • StateOf
  • EventOf
  • InferStates
  • InferEvents
  • MachineConfig
good to know

Good to know before you start.

Data lives in signals

There is no context. Keep fetched data and errors in signals beside the machine and update them in onEnter.

Flat by design

No hierarchical or parallel states, invoked actors or delayed after transitions. Compose several machines with effects when you need them, or use XState for statecharts.

dispose() clears listeners

It removes onEnter, onExit, onTransition and onDone callbacks; the machine itself still answers send.

questions

What people ask first.

Why is it so much faster than XState?

It does less: no actor system, no snapshot objects, no statechart semantics. A transition is a lookup and a signal write. The figures are one overnight run under bun that we wrote; run packages/fundamentals/machine/bench/ yourself.

Does it work in effects and JSX?

Yes. machine(), matches(), can(), nextEvents() and isFinal() are reactive reads, like any signal.

Can I send an event from a listener?

Yes. It is queued and handled after the current transition finishes, in order; more than 10,000 in one send throws a runaway error.

Model the states once. Delete the bugs that lived between them.

bun add @pyreon/machine
Read the state machine guide