Metakit / Write a rule once

3. Declare a guarded transition and disable actions early

A Transition is one state change: a boolean guard (when) with the field writes (apply) that happen together when the guard holds. A Machine is a named set of transitions over one Schema. You can evaluate a transition against a full { state, input } pair to run the update, or against a partial { state } to know whether the action is allowed before the user types an amount.

ts
import * as Machine from '@doeixd/metakit/machine'

const Withdraw = Transition.make({
  state: Account,
  input: Schema.Struct({ amount: Schema.Int }),
  when: ({ state, input }) =>
    Expr.all(
      Eq.is(state.status, 'open'),
      Num.gt(input.amount, 0),
      Num.lte(input.amount, Num.sub(state.balance, state.reserved)),
    ),
  apply: ({ state, input }) => [
    Transition.set(state.balance, Num.sub(state.balance, input.amount)),
  ],
})

const Freeze = Transition.make({
  state: Account,
  input: Schema.Struct({}),
  when: ({ state }) => Eq.is(state.status, 'open'),
  apply: ({ state }) => [Transition.set(state.status, 'frozen')],
})

export const AccountMachine = Machine.make({
  state: Account,
  transitions: { Withdraw, Freeze },
})

const frozen = { id: 'a1', balance: 50_000, reserved: 10_000, status: 'frozen' as const }
const affordances = Machine.affordances(AccountMachine, frozen)
// affordances -> { available: [], disabled: ['Withdraw', 'Freeze'], pending: [] }

const withdraw = Machine.intent(AccountMachine.transitions.Withdraw)
const admission = withdraw.can({ state: frozen }) // False
const message = withdraw.message({ amount: 500 }) // { _tag: 'Withdraw', input: { amount: 500 } }

Machine.affordances sorts every transition into available (its guard is true now), disabled (false now) or pending (it still needs input). On an open account, Freeze is available (its guard needs no input) and Withdraw is pending (it needs input.amount). On a frozen account, both are disabled without a separate UI check. To turn that into a button, put disabled on the button whose Message tag is in the disabled list.

Use paths from state as write targets. Use Transition.pathsOf(stateSchema, inputSchema, suppliedSchemas) to get the same typed accessors outside a transition callback. input and supplied paths also work in read expressions.

Use Machine.intent when the same control checks a transition and builds its message. It takes the transition as the machine holds it, AccountMachine.transitions.Withdraw: the machine copies each transition it is given and names the copy by its key, so the message is tagged 'Withdraw' and a misspelt key is a type error on its line. The guard's Verdict is Unknown while it needs input. For a transition declared with supplied: { now: Schema.Int }, the host attaches the clock value by passing { ...message, supplied: { now } } to dispatch. Dispatch checks the guard against the current state.

Machine.stepObserved(Withdraw, account, { amount: 500 }) returns { state, emitted, writes } on success. A same-value assignment adds no entry to writes. Other entries name a state path, a list with changed membership, or a field of a keyed row. Lifted paths start at the parent state, and opaque writes report the whole subtree they write.