https://github.com/henriquelimas/mchine
🎲 A Simple State Machine
https://github.com/henriquelimas/mchine
state-machine states
Last synced: 4 days ago
JSON representation
🎲 A Simple State Machine
- Host: GitHub
- URL: https://github.com/henriquelimas/mchine
- Owner: HenriqueLimas
- Created: 2018-07-02T21:09:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-22T11:26:13.000Z (over 7 years ago)
- Last Synced: 2025-02-01T12:45:31.359Z (9 months ago)
- Topics: state-machine, states
- Language: TypeScript
- Homepage:
- Size: 76.2 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 🎲 mchine
[](https://travis-ci.org/HenriqueLimas/mchine)
A Simple State Machine
## Why?
Because State machine are sexy, and easy to use. Using a state machine will change how you think and develop
a Front-end application. Think more on your view's state instead of his transactions, this will reduce
a lot of the if else and make the code more maintanable.
## Install
```
npm install mchine
```
## How to use
```js
import mchine from "mchine";
const stateMachineSchema = {
initial: "idle",
states: {
idle: {
events: {
login: {
target: "sending"
}
}
},
sending: {
events: {
success: {
target: "idle"
},
error: {
target: "error"
}
}
},
error: {}
}
};
const stateMachine = mchine(stateMachineSchema);
stateMachine.getCurrentState(); // idle
stateMachine.transition("login");
stateMachine.getCurrentState(); // sending
API.login("john@example.com", "Some.secure.password42")
.then(() => stateMachine.transition("success")) // idle
.catch(() => stateMachine.transition("error")); // error
```
## How to import
### Browser (Using modules)
```html
import mchine from './node_modules/mchine/dist/index.m.js';
// some magic code ✨...
```
### Browser (UMD)
```html
// some magic code ✨...
```
### Transpilers (Babel, Rollup, Typescript, ...)
```js
import mchine from "mchine";
// Some magic code ✨...
```
### Node
```js
const mchine = require("mchine");
// Some magic code ✨...
```
## References
- Implemention of MChine follows the main algorithm of the [SCXML spec from the W3C](https://www.w3.org/TR/scxml/#invoke)