@traxsharp/state-machine
v1.68.0
Published
Portable, resumable snapshot state-machine engine — the TypeScript twin of Trax.StateMachine. Represents a multi-step flow's state as language-neutral JSON so a client and a C# backend agree on it, with illegal (state, context) pairs unrepresentable and e
Readme
@traxsharp/state-machine
Part of Trax .NET: business logic you can call, schedule, or serve as an API, with every run recorded in your Postgres. Docs · Getting started · Repository
The TypeScript twin of Trax.StateMachine. A machine is declared once in C#; the twin generated from it runs
the same rules in the browser, so a page can show what an event will do before the server, which applies every
transition, answers. It also holds the client a page uses to drive a machine a Trax host serves, and its React hooks.
Install
npm install @traxsharp/state-machine graphql-wsgraphql-ws and React are optional peer dependencies: the socket a machine's pushes arrive on, and the hooks in
@traxsharp/state-machine/react. The package is released with Trax, at the same version as the Trax.* NuGet
packages.
Example
trax client generates a typed client for every machine a host exposes. A page configures it once and uses the
machine's hook:
import { configureTrax, useRememberedCheckout } from "./trax/index.g";
configureTrax({
url: "https://shop.example/trax/graphql",
socketUrl: "wss://shop.example/trax/graphql",
headers: () => ({ "X-Api-Key": key }),
connectionParams: () => ({ apiKey: key }),
user: userId,
});
function PayButton() {
const checkout = useRememberedCheckout();
if (checkout.error) return <p>{checkout.error.code}</p>;
return (
<button disabled={!checkout.can("Pay")} onClick={() => checkout.send("Pay")}>
Pay ({checkout.state})
</button>
);
}useRememberedCheckout opens the instance this browser holds for the user, or starts one. send shows the twin's
prediction at once and replaces it with what the server stored. The same machine can be driven without React
(ServerMachineController), in memory with no server (MachineController), or from a form that owns its own data
(createFormView).
Where this fits
- State machines: declaring a machine in C#, generating its twin, and serving it.
- Drive a machine instance from React and Call a machine from TypeScript.
- TypeScript package reference: every export, by entry point.
trax client: the client generated from a host's manifest.
Contributing
Read AGENTS.md before changing code. Report vulnerabilities privately as described in SECURITY.md.
License
MIT. There is no commercial edition, and there will not be one.
Trax .NET is an independent open-source project and is not affiliated with the Utah Transit Authority, Trax Retail, or any other organization using the Trax name.
