doc: update readme
This commit is contained in:
parent
b6d66fd906
commit
a91bc95bd5
1 changed files with 29 additions and 37 deletions
66
README.md
66
README.md
|
@ -3,50 +3,42 @@
|
||||||
[![ci](https://github.com/icetemple/it-fsm/actions/workflows/ci.yml/badge.svg)](https://github.com/icetemple/it-fsm/actions/workflows/ci.yml)
|
[![ci](https://github.com/icetemple/it-fsm/actions/workflows/ci.yml/badge.svg)](https://github.com/icetemple/it-fsm/actions/workflows/ci.yml)
|
||||||
[![Coverage Status](https://coveralls.io/repos/github/icetemple/it-fsm/badge.svg?branch=master)](https://coveralls.io/github/icetemple/it-fsm?branch=master)
|
[![Coverage Status](https://coveralls.io/repos/github/icetemple/it-fsm/badge.svg?branch=master)](https://coveralls.io/github/icetemple/it-fsm?branch=master)
|
||||||
|
|
||||||
Simple finite state machine
|
Simple full-featured finite state machine for your project
|
||||||
|
|
||||||
### Installation
|
### Why it-fsm?
|
||||||
|
|
||||||
`npm install --save it-fsm`
|
- 🚯 333 LOC - 0 dependencies
|
||||||
|
- 🍒 Sophisticated object-oriented design
|
||||||
|
|
||||||
### Usage
|
|
||||||
|
### Getting started
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { StateMachineBuilder } from "it-fsm";
|
import { StateMachineBuilder } from "it-fsm";
|
||||||
|
|
||||||
enum ProjectStatus {
|
const [locked, unlocked] = ['locked', 'unlocked'] as const;
|
||||||
Pending = "pending",
|
|
||||||
Active = "active",
|
|
||||||
Completed = "completed",
|
|
||||||
Archived = "archive",
|
|
||||||
}
|
|
||||||
|
|
||||||
const smbProject = new StateMachineBuilder()
|
const sm = new StateMachineBuilder()
|
||||||
.withStates(Object.values(ProjectStatus))
|
.withStates([locked, unlocked])
|
||||||
.withTransitions([
|
.withTransitions([
|
||||||
[ProjectStatus.Pending, [ProjectStatus.Active, ProjectStatus.Archived]],
|
[locked, { coin: unlocked }],
|
||||||
[ProjectStatus.Active, [ProjectStatus.Completed]],
|
[unlocked, { push: locked }],
|
||||||
]);
|
])
|
||||||
|
.build(locked);
|
||||||
async function main() {
|
|
||||||
const project1 = { id: 1, status: ProjectStatus.Pending };
|
|
||||||
const project2 = { id: 2, status: ProjectStatus.Completed };
|
|
||||||
|
|
||||||
// Build FSM with current project status
|
|
||||||
const smForProject1 = smbProject.build(project1.status);
|
|
||||||
const smForProject2 = smbProject.build(project2.status);
|
|
||||||
|
|
||||||
console.log(smForProject2.allowedTransitionStates()); // []
|
|
||||||
|
|
||||||
console.log(smForProject1.allowedTransitionStates()); // [active, archived]
|
|
||||||
await smForProject1.changeState(ProjectStatus.Active);
|
|
||||||
|
|
||||||
console.log(smForProject1.allowedTransitionStates()); // [completed]
|
|
||||||
await smForProject1.changeState(ProjectStatus.Completed);
|
|
||||||
|
|
||||||
console.log(smForProject1.allowedTransitionStates()); // []
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
or with deno
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { StateMachineBuilder } from "https://raw.githubusercontent.com/icetemple/it-fsm/master/fsm.ts";
|
||||||
|
|
||||||
|
// ...
|
||||||
|
```
|
||||||
|
|
||||||
|
You can find the full example in the examples folder.
|
||||||
|
|
||||||
|
### Install
|
||||||
|
|
||||||
|
If you want to use it in Node.js or the browser, you may need to install it as follows
|
||||||
|
|
||||||
|
`npm install --save it-fsm`
|
||||||
|
|
Loading…
Reference in a new issue