Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

simple asynchronous finite state machine

License

NotificationsYou must be signed in to change notification settings

sscholle/ts-async-state-machine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM

Description

the objective is to provide a simple interface to build a finite state machine

Features

  • thoroughly tested with good code coverage
  • small package file: 3.2kb (compressed)
  • written in TypeScript and therefore includes a definitions file with full documentation (*.d.ts)

Core Concepts

  • Define a set ofStates, each with aname, a list offromStates and some optional callbacks
  • Call.transition('newStateName') to trigger a state change to the specified state
  • UsePromises inside theonBeforeEnter callback to resolve/reject the state change (usually via server API)

Example Usage

import{Machine}from"ts-async-state-machine";// Create your MachineconstMicrowaveMachine=newMachine('Microwave');// Add States to the MachineMicrowaveMachine.addState('off',['running'],{onBeforeEnter:(prevState)=>newPromise((resolve,rejection)=>{// use a server APi request to 'confirm' the state changeresolve();// resolves the state transition - changes 'MicrowaveMachine's state to 'off'// a 'rejection' will fail the internal state transition})});MicrowaveMachine.addState('on',['off'],{// callbacks are optional and use them as needed: 'onEnter', 'onExit', 'onBeforeEnter' as optionsonExit:()=>{console.log('Exited ON state')}});MicrowaveMachine.addState('running',['on'],{onEnter:()=>{console.log('Entered RUNNING State')}});// Initialise into your required starting stateMicrowaveMachine.start('off');// Start Triggering State changes when neccessaryMicrowaveMachine.transition('on').then(newState=>{console.log('Entered State',newState.name)});// omitted 'catch' for brevityMicrowaveMachine.transition('running').then(newState=>{console.log('Entered State',newState.name)});

Examples

  • see/examples folder for a full examples inmicrowave.js,traffic-light.js andtraffic-light-server-stateful.js
  • run microwave example:npm run example
  • other examples:npm run build && node ./examples/traffic-light.js
  • other examples:npm run build && node ./examples/traffic-light-server-stateful.js

Development

  • build:npm run build
  • test:npm run test
  • code coverage:npm run test:coverage

Credits

About

simple asynchronous finite state machine

Topics

Resources

License

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp