Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/streetstrider/machine
strongly typed finite-state machine
https://github.com/streetstrider/machine
state-machine typescript
Last synced: 27 days ago
JSON representation
strongly typed finite-state machine
- Host: GitHub
- URL: https://github.com/streetstrider/machine
- Owner: StreetStrider
- Created: 2022-06-23T18:30:49.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-09-09T01:38:32.000Z (over 2 years ago)
- Last Synced: 2024-11-08T05:21:10.177Z (3 months ago)
- Topics: state-machine, typescript
- Language: TypeScript
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# machine
My approach on a strongly typed finite-state machine. Simple design, strong guarantees, both type guards and asserts for a running machine.
```ts
import { Schema } from 'machine'
import { Machine } from 'machine'const schema = Schema()
.state('init', () => { /* enter init */ }, () => { /* leave init */ })
.state('running', (id: number) => ({ id }))
.state('stopped')
.path('init', 'running')
.path('running', 'running')
.path('running', 'stopped')const machine = Machine(schema, 'init')
machine.is('init') // → true
machine.is('running') // → falsemachine.go('running', 1001)
machine.is('running') // → true
machine.key // → 'running'
machine.state // → { id: 1001 }machine.go('running', 1002)
machine.go('running', 1003)machine.go('stopped')
if (machine.is('running')) {
machine.state // will be refined to proper type
}
function machinery (machine: Machine) {
machine.must('running') // will throw if in wrong state
machine.state // will be refined to proper type
}
```## license
ISC, © Strider, 2022.