@pyreon/machine 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# @pyreon/machine
|
|
2
|
+
|
|
3
|
+
Reactive state machines for Pyreon. A machine is a constrained signal — it can only hold specific values and transition between them via specific events. Type-safe states and events inferred from the definition.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @pyreon/machine
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { createMachine } from '@pyreon/machine'
|
|
15
|
+
|
|
16
|
+
const machine = createMachine({
|
|
17
|
+
initial: 'idle',
|
|
18
|
+
states: {
|
|
19
|
+
idle: { on: { FETCH: 'loading' } },
|
|
20
|
+
loading: { on: { SUCCESS: 'done', ERROR: 'error' } },
|
|
21
|
+
done: {},
|
|
22
|
+
error: { on: { RETRY: 'loading' } },
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
machine() // 'idle' — reads like a signal
|
|
27
|
+
machine.send('FETCH') // transition to 'loading'
|
|
28
|
+
machine.matches('loading') // true — reactive in effects/JSX
|
|
29
|
+
|
|
30
|
+
// Reactive in JSX
|
|
31
|
+
{() => machine.matches('loading') && <Spinner />}
|
|
32
|
+
{() => machine.matches('done') && <Results />}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Guards
|
|
36
|
+
|
|
37
|
+
Conditional transitions using guard functions:
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
const form = createMachine({
|
|
41
|
+
initial: 'editing',
|
|
42
|
+
states: {
|
|
43
|
+
editing: {
|
|
44
|
+
on: { SUBMIT: { target: 'submitting', guard: () => isValid() } },
|
|
45
|
+
},
|
|
46
|
+
submitting: { on: { SUCCESS: 'done', ERROR: 'editing' } },
|
|
47
|
+
done: {},
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## API
|
|
53
|
+
|
|
54
|
+
### `createMachine(config)`
|
|
55
|
+
|
|
56
|
+
Create a reactive state machine. Config: `initial` (starting state) and `states` (state/event map).
|
|
57
|
+
|
|
58
|
+
**Returns `Machine`:**
|
|
59
|
+
|
|
60
|
+
| Property | Description |
|
|
61
|
+
| --- | --- |
|
|
62
|
+
| `machine()` | Read current state (reactive) |
|
|
63
|
+
| `send(event, payload?)` | Trigger a transition |
|
|
64
|
+
| `matches(...states)` | Check if in one of the given states (reactive) |
|
|
65
|
+
| `can(event)` | Check if event would trigger a valid transition |
|
|
66
|
+
| `nextEvents()` | Available events from current state |
|
|
67
|
+
| `reset()` | Return to initial state |
|
|
68
|
+
| `onEnter(state, callback)` | Fire callback when entering a state |
|
|
69
|
+
| `onTransition(callback)` | Fire on any transition |
|
|
70
|
+
|
|
71
|
+
Use signals alongside machines for data. The machine manages transitions, signals manage data.
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|