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 - model
- a signal with declared states
machine()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.
- 34.7× faster
sendthan 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 →Booleans don't scale. States do.
One value, not four flags
loading,erroranddonecan 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.Buttons that know when they work
can('PAY', payload)runs the guard and predicts exactly whatsendwould do;nextEvents()lists what is possible now. Both are reactive, sodisabled={() => !checkout.can('PAY')}stays right.Branch states, no glue
always: [{ target: 'empty', guard: () => cart().length === 0 }, 'ready']routes the moment a state is entered, reading ordinary signals.sendreturns the settled state after the cascade.No actor to start
No interpreter, no
createActor().start(), no context object. Data lives in signals beside the machine; side effects hang offonEnter,onExitandonTransition.
Three flags, or one machine.
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() })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 XState?
Same states block. No actor, no event objects.
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' }))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'))Drive the checkout.
Only valid events are enabled. That list is nextEvents(), and it's reactive.
- cart
- details
- paying
- declined
- paid
- Send CHECKOUT to start.
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.
- sendwin34.7×
- createwin11.4×
- canwin6.0×
- matcheswin4.7×
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 crashingsend. can(event, payload?)predictssendexactly, guard included.matches(...states)andnextEvents()reactive reads for rendering.- Eventless
alwaystransitions for branch states; a runaway loop throws after 1,000 steps. - Final states
final: true,isFinal(),onDone. - Lifecycle listeners
onEnter,onExit(before the nextonEnter),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/EventOftype helpers that work on an instance or a config.
Things to build with it.
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'One function, fully typed.
Every export has a reference page with its signature, an example and the mistakes people make with it.
- createMachine
- machine()
- matches
- can
- nextEvents
- isFinal
- send
- reset
- dispose
- onEnter
- onExit
- onTransition
- onDone
- StateOf
- EventOf
- InferStates
- InferEvents
- MachineConfig
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.
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