• Stars
    star
    271
  • Rank 151,717 (Top 3 %)
  • Language
    TypeScript
  • License
    BSD 2-Clause "Sim...
  • Created over 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A strongly typed finite state machine for TypeScript

Build Status npm version NuGet version Bower version

TypeState

TypeState is a strongly typed finite state machine for TypeScript or JavaScript. Finite state machines are useful for modeling complicated flows and keeping track of state.

Installation

Install-Package TypeState

npm install typestate

bower install typestate

Basic Example

// Let's model the states of an elevator

// Define an Enum with all possible valid states
enum Elevator {
   DoorsOpened,
   DoorsClosed,
   Moving
}

// Construct the FSM with the inital state, in this case the elevator starts with its doors opened
var fsm = new TypeState.FiniteStateMachine<Elevator>(Elevator.DoorsOpened);

// Declare the valid state transitions to model your system

// Doors can go from opened to closed, and vice versa
fsm.from(Elevator.DoorsOpened).to(Elevator.DoorsClosed);
fsm.from(Elevator.DoorsClosed).to(Elevator.DoorsOpened);

// Once the doors are closed the elevator may move
fsm.from(Elevator.DoorsClosed).to(Elevator.Moving);

// When the elevator reaches its destination, it may stop moving
fsm.from(Elevator.Moving).to(Elevator.DoorsClosed);

// Check that the current state is the initial state
if(fsm.is(Elevator.DoorsOpened)){
   console.log("The doors are open Dave");  
}


// Test validity of transitions from the current state, in this case 'Elevator.DoorsOpened'
fsm.canGo(Elevator.DoorsClosed); // returns true
fsm.canGo(Elevator.Moving); //returns false

// Go to a new state, closing the elevator doors. 
fsm.go(Elevator.DoorsClosed); // The fsm.currentState is now set to 'Elevator.DoorsClosed'

// The elevator can now move or open the doors again
fsm.canGo(Elevator.Moving); // returns true
fsm.canGo(Elevator.DoorsOpened); //returns true

Using JavaScript

JavaScript is easy with TypeState. The finite state machine relies on states that can be converted to strings with the .toString() method. So to use JavaScript simple replace the top few lines of the previous example with the following:

var Elevator = {
	DoorsOpened : "DoorsOpened",
	DoorsClosed : "DoorsClosed",
	Moving : "Moving"
}

var fsm = new FiniteStateMachine(Elevator.DoorsOpened)

Listening for state changes

TypeState allows you to listen to state changes. For example if the elevator starts moving, we would like to play some elevator music. Additionally we would like to stop the music when the elevator stops.

fsm.on(Elevator.Moving, (from: Elevator)=>{
	playGroovyElevatorMusic();
});

fsm.on(Elevator.DoorsClosed, (from: Elevator)=>{
	stopGroovyElevatorMusic();
});

Interrupting Transitions

Sometimes you need to interrupt transitions. You may interrupt transitions to a state with onEnter(STATE, CALLBACK) and interrupt transitions from a state with the onExit(STATE, CALLBACK). If the CALLBACK returns false the transition is canceled and the state will not change.

console.log("DoorsOpened", fsm.currentState === Elevator.DoorsOpened); // true
var handsInDoor = true;

// Listen for transitions to DoorsClosed, if the callback returns false the transition is canceled.
fsm.onEnter(Elevator.DoorsClosed, (from: Elevator)=>{
   if(handsInDoor){
      return false;
   }
   return true;
});

// Attempt to transition
fsm.go(Elevator.DoorsClosed);

// State does not change to DoorsClosed
console.log("DoorsOpened", fsm.currentState === Elevator.DoorsOpened); //true

Wildcard Transitions

If all transitions to or from a certain state are valid, there are a convience wildcard methods fromAny(STATE_ENUM) and toAny(STATE_ENUM) for such cases.

enum ValidStates {
	A,
	B,
	C,
	D
}

var newFsm = new TypeState.FiniteStateMachine<ValidStates>(ValidStates.A);
newFsm.from(ValidStates.A).toAny(ValidStates);
newFsm.fromAny(ValidStates).to(ValidStates.B);

More Repositories

1

nesgame

Assembly
32
star
2

NES-Sprite-Editor

Simple Sprite Editor for NES Games
JavaScript
32
star
3

planet-wars-competition

Planet wars AI competition
JavaScript
8
star
4

aspnet-core-demogame

ASPNET Core demo
JavaScript
7
star
5

GameOfLife

A JavaScript and HTML5 Canvas implementation of Conway's Game of Life
JavaScript
6
star
6

nespresentation

NES Game Presentation for Code Camp March 2017
JavaScript
6
star
7

webpack-bookmarklet

Build a bookmarklet with webpack for automating your websites (Like YouTube)
JavaScript
5
star
8

iterative-constraint-solver

A small TypeScript example of an iterative constraint solver based on Erin Catto's GDC 2014 talk https://www.youtube.com/watch?v=SHinxAhv1ZE
TypeScript
5
star
9

AntAICompetition

Simple Ant Hill AI Competition Simulation Server
C#
4
star
10

coroutine-animations

Small example of how to build animations with coroutines and JavaScript generators
TypeScript
4
star
11

HyperAsteroids

MMO Asteroids on NodeJS
JavaScript
3
star
12

BellTowerEscape

JavaScript
2
star
13

MyCoolProject

2
star
14

Excalibur-Shmup

DEPRECATED USE: https://github.com/excaliburjs/sample-shootemup
JavaScript
2
star
15

webgpu-example

TypeScript
2
star
16

capacitor-game-v2

Java
2
star
17

isometric-layers

This demo shows how to build isometric tile map layering with elevation!
TypeScript
2
star
18

java-game-engine

Provides a simply easy to use 2D rpg game engine that is portable to anything that can run java.
Java
2
star
19

VanillaJS

Code Camp VanillaJS Presetation
JavaScript
1
star
20

git-helper

Record of helpful git personalizations
1
star
21

AllYourGamesPresentation

Presentation on Building HTML5 Games : All Your Games Are Belong To Us
JavaScript
1
star
22

assemblyscript-webpack-wasm-loader

Example setup using AssemblyScript and WebPack with the wasm-loader
JavaScript
1
star
23

zelda-like

A zelda like game made live on stream
TypeScript
1
star
24

tiled-xml-parser

Lightweight parser for tiled xml, converting it to tiled json and validating with zod
TypeScript
1
star
25

ghostlight-excalibur-demo

A little demo game that utilizes the level editor GhostLight in combination with the game dev framework Excalibur.
TypeScript
1
star
26

SignalRGameDemo

Simple example of how to create a multiplayer game in SignalR
JavaScript
1
star
27

object-pool

TypeScript
1
star
28

TripsGame

Trips game
C#
1
star