Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yarinvak/easy-state-machine
https://github.com/yarinvak/easy-state-machine
Last synced: 20 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/yarinvak/easy-state-machine
- Owner: yarinvak
- Created: 2020-01-19T13:02:46.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T05:29:55.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T13:27:45.609Z (about 1 month ago)
- Language: TypeScript
- Size: 1.54 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
## easy-sm
An easy to use state machine.![npm](https://img.shields.io/npm/v/easy-sm)
### Installation
```
npm i easy-sm
```### How to
Each state contains a name and an array of events.
An event contains a handler, that will be performed when the event will occur on the state.For example, lets say we have a car object, with an idle state.
The idle state has an event called "ignite", to ignite the car.
That's how our state is going to look:
```javascript
const idle: State = {
name: "idle",
events: {
ignite: {
handler: igniteCar,
nextState: "ignited",
failedState: "failedIgniting"
}
}
};
```Now we shall create the ignited state:
```javascript
const ignited: State = {
name: "ignited",
events: {
move: {
handler: moveCar,
nextState: "carMoved",
failedState: "failedMoving"
}
}
};
```We also have to create our failing states. For example:
```javascript
const ignited: State = {
name: "failedIgniting",
events: {
retry: {
handler: igniteCar,
nextState: "ignited",
failedState: "failedIgniting"
}
}
};
```An handler looks like this:
```javascript
const igniteCar = async (carName: string): Promise => {
//todo pythonexecutor createMosaicPython, args.gdbpath, args.mapname, args.srid.tostring
console.log("Igniting...");
return {data: carName, newEvent: 'moveCar'};
};
```
The handler can return some data, and the next event that should be performed (after igniting, we would like to move the car).You can also create an onChanged function, that will be called every time a state is changed:
```javascript
const onChanged = (newStateName: string, result: EventHandlerResult) => {
console.log(`Transformed to ${newStateName}`);
};
```Starting the machine:
```javascript
const states: State[] = [idle, ignited, movedCar, completed];
const stateMachine = new StateMachine(initialState, states, onChanged);
await stateMachine.execute("start", "mazda"); // sending a start event to the machine, and some data
```