tiny-state-machine v1.0.2
Tiny State Machine
Introduction
a tiny finite state machine,
no state transform limit, so you can controll and transform state by yourself.
Useage
npm i tiny-state-machine
1.State Light Red
const red = new State({
name: 'red',
onEnter(lastState) {
console.log('light red on')
},
onLeave(nextState) {
console.log('light red off')
},
update() {
//do something when FSM.updateCall called like game update
}
})
//or
const red = new State()
red.name ='red'
red.onEnter = function(lastState) {
//code
}
//so on ...
2.State Light Green
const green = new State({
name: 'green',
onEnter(lastState) {
console.log('light green on')
},
onLeave(nextState) {
console.log('light green off')
}
})
3.TSM
const TSM = new TinyStateMachine([red, green])
TSM.set(red) //light red on
wait(10) //fake code, wait for 10 sec
TSM.go(green) // light red leave, light green on
//add a updater
const update = function() {
TSM.updateCall()
requestAnimationFrame(update)
}
update()
Setting TSM.beforeTranform, TSM.afterTransform and TSM.update to unify all state transform logics
Class and Method
State
1.name the name of state
2.onEnter(lastState) called when the state enter
3.onLeave(nextState) called when the state leave
4.update called if you have a frame or step execrutor
TinyStateMachine
1.constructor(states: State[]) need a State Array
2.current() get current activeState
3.is(state || stateName) activeState is state or the state named stateName
4.set(state || stateName) set activeState
5.go(state || stateName) transform activeState to state
6.add(state) add a new state
7.remove(state) remove a state
8.updateCall() need called in frame or step execrutor
9.beforeTransform rewrite it to listening transform
10.afterTransform like up
11.update rewrite it to listenting update