@j0u1/finity
v1.0.5
Published
Lightweight finite state machine library for TypeScript
Maintainers
Readme
@j0u1/finity
Lightweight finite state machine (FSM) library for TypeScript.
Install
bun add @j0u1/finity
# or
npm install @j0u1/finityUsage
Single transition per state:
import { createMachine } from "@j0u1/finity"
const traffic = createMachine({
initial: "red",
transitions: {
red: "yellow",
yellow: "green",
green: "yellow",
},
})
traffic.current // "red"
traffic.moveTo("yellow") // "yellow"
traffic.canChangeTo("green") // true
traffic.canChangeTo("red") // falseMultiple transitions per state:
const order = createMachine({
initial: "pending",
transitions: {
pending: ["success", "fail"],
success: [],
fail: [],
},
})
order.canChangeTo("success") // true
order.canChangeTo("fail") // true
order.moveTo("success") // "success"API
createMachine(config)
Creates a new state machine.
| Parameter | Type | Description |
|---|---|---|
| config.initial | string | Initial state |
| config.transitions | Record<string, string \| string[]> | Map of state transitions |
Returns an object with:
current— current statemoveTo(state)— transitions to the given state. Throws if transition is not allowed.canChangeTo(state)— returnstrueif transition to given state is possible from current state
License
MIT
