Simple full-featured finite state machine for your project
Find a file
2021-08-21 08:56:18 +03:00
.github/workflows chore: improve makefile commands 2021-08-21 08:53:36 +03:00
.vscode chore: add tab size for vscode 2021-08-21 08:54:07 +03:00
examples example: add full ood example with new version 2021-08-21 08:56:18 +03:00
.gitignore chore: remove dest from repo 2021-08-20 11:13:30 +03:00
fsm.test.ts refac: add more generic to improve design 2021-08-21 08:55:52 +03:00
fsm.ts refac: add more generic to improve design 2021-08-21 08:55:52 +03:00
LICENSE chore: change licence 2021-08-20 11:21:12 +03:00
makefile chore: improve makefile commands 2021-08-21 08:53:36 +03:00
package.json fix: remove files from package.json 2021-08-20 11:20:55 +03:00
README.md chore: update badges 2021-08-20 01:49:47 +02:00
tsconfig.json chore: remove dom from ts lib 2021-08-20 11:21:53 +03:00

IT FSM

ci Coverage Status

Simple finite state machine

Installation

npm install --save it-fsm

Usage

import { StateMachineBuilder } from "it-fsm";

enum ProjectStatus {
  Pending = "pending",
  Active = "active",
  Completed = "completed",
  Archived = "archive",
}

const smbProject = new StateMachineBuilder()
  .withStates(Object.values(ProjectStatus))
  .withTransitions([
    [ProjectStatus.Pending, [ProjectStatus.Active, ProjectStatus.Archived]],
    [ProjectStatus.Active, [ProjectStatus.Completed]],
  ]);

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();