|
1 | | -# typescript-library-starter |
| 1 | +# @escapace/fsm |
| 2 | + |
| 3 | +Type-safe finite state machine library for TypeScript. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Type-safe state machine definition and execution |
| 8 | +- Conditional transitions with predicates |
| 9 | +- Context management with reducers |
| 10 | +- State change subscriptions |
| 11 | +- High-frequency transition performance (8x faster than @xstate/fsm) |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install @escapace/fsm |
| 17 | +``` |
| 18 | + |
| 19 | +## Example |
| 20 | + |
| 21 | +```typescript |
| 22 | +import { stateMachine, interpret } from '@escapace/fsm' |
| 23 | + |
| 24 | +// Define coin values and state types |
| 25 | +type Coin = 5 | 10 | 25 | 50 |
| 26 | +enum State { |
| 27 | + Locked = 'LOCKED', |
| 28 | + Unlocked = 'UNLOCKED', |
| 29 | +} |
| 30 | +enum Action { |
| 31 | + Coin = 'COIN', |
| 32 | + Push = 'PUSH', |
| 33 | +} |
| 34 | + |
| 35 | +// Create a turnstile that requires 50 cents to unlock |
| 36 | +const machine = stateMachine() |
| 37 | + .state(State.Locked) // Define possible states |
| 38 | + .state(State.Unlocked) |
| 39 | + .initial(State.Locked) // Set starting state |
| 40 | + .action<Action.Coin, { coin: Coin }>(Action.Coin) // Define action with payload type |
| 41 | + .action(Action.Push) // Define action without payload |
| 42 | + .context<{ total: number }>({ total: 0 }) // Set context type and initial value |
| 43 | + .transition( |
| 44 | + State.Locked, // From locked state |
| 45 | + [ |
| 46 | + Action.Coin, |
| 47 | + ( |
| 48 | + context, |
| 49 | + action, // On coin insert, with predicate |
| 50 | + ) => context.total + action.payload.coin >= 50, |
| 51 | + ], |
| 52 | + State.Unlocked, // Go to unlocked state |
| 53 | + (context, action) => { |
| 54 | + // Run this reducer |
| 55 | + context.total += action.payload.coin |
| 56 | + return context |
| 57 | + }, |
| 58 | + ) |
| 59 | + .transition( |
| 60 | + State.Locked, // Fallback transition when not enough coins |
| 61 | + Action.Coin, |
| 62 | + State.Locked, |
| 63 | + (context, action) => { |
| 64 | + // Add coin to total |
| 65 | + context.total += action.payload.coin |
| 66 | + return context |
| 67 | + }, |
| 68 | + ) |
| 69 | + .transition(State.Unlocked, Action.Coin, State.Unlocked) // Stay unlocked on coin insert |
| 70 | + .transition( |
| 71 | + [State.Locked, State.Unlocked], // Push always locks |
| 72 | + Action.Push, |
| 73 | + State.Locked, |
| 74 | + (context) => { |
| 75 | + // Reset total on push |
| 76 | + context.total = 0 |
| 77 | + return context |
| 78 | + }, |
| 79 | + ) |
| 80 | + |
| 81 | +// Create and use the state machine |
| 82 | +const turnstile = interpret(machine) |
| 83 | + |
| 84 | +console.log(turnstile.state) // 'LOCKED' |
| 85 | + |
| 86 | +turnstile.do(Action.Coin, { coin: 25 }) // Insert 25 cents |
| 87 | +console.log(turnstile.state) // 'LOCKED' |
| 88 | + |
| 89 | +turnstile.do(Action.Coin, { coin: 25 }) // Insert another 25 cents (total 50) |
| 90 | +console.log(turnstile.state) // 'UNLOCKED' |
| 91 | + |
| 92 | +turnstile.do(Action.Push) // Push through turnstile |
| 93 | +console.log(turnstile.state) // 'LOCKED' |
| 94 | +``` |
| 95 | + |
| 96 | +## API |
| 97 | + |
| 98 | +### `stateMachine()` |
| 99 | + |
| 100 | +Creates a new state machine builder. |
| 101 | + |
| 102 | +#### Methods |
| 103 | + |
| 104 | +- `.state(name)` - Define a state |
| 105 | +- `.initial(state)` - Set initial state |
| 106 | +- `.action<Type, Payload>(name)` - Define an action type |
| 107 | +- `.context<Type>(initialValue)` - Set context type and initial value |
| 108 | +- `.transition(source, action, target, reducer?)` - Define state transition |
| 109 | + |
| 110 | +### `interpret(machine)` |
| 111 | + |
| 112 | +Creates an executable state machine instance. |
| 113 | + |
| 114 | +#### Properties |
| 115 | + |
| 116 | +- `.state` - Current state (readonly) |
| 117 | +- `.context` - Current context (readonly) |
| 118 | + |
| 119 | +#### Methods |
| 120 | + |
| 121 | +- `.do(action, payload?)` - Dispatch an action |
| 122 | +- `.subscribe(callback)` - Subscribe to state changes |
0 commit comments