make-stateful
make-stateful is a library that turns your JavaScript objects into self-contained state machines. The goal is to provide a flexible state machine with minimal configuration.
API
makeStateful(Class)
Extends a JS class with the functions that makeStateful requires.
Class.addState(stateName, stateDefinition)
Adds a named state definition to the extended Class.
instance.gotoState(stateName, ...args)
Transitions the instance to the named state. If the instance currently has a method called exitState
, it will be called with the arguments passed. If the new state has a method called enterState
, it will also be called with the arguments passed to gotoState
.
Example
var { thishealth = health;}Enemyprototype { return 'My health is ' + thishealth;}; Enemy; var peter = 10 peter // My health is 10peterpeter // I am UNBREAKABLE!!peter // I can not die now!peterpeter // My health is 10