mfsm
v2.0.0
Published
my finite state machine. not yours. don't use it.
Downloads
10
Readme
My Finite State Machine
No really, this is probably not for you.
API
const fsm = require('mfsm')
const client = require('something')
const clientFsm = fsm({
api: {
connect: function () {
this.handle('connect')
return this.after('connected')
},
disconnect: function () {
this.handle('disconnect')
return this.after('disconnected')
}
},
init: {
url: process.env.HOST_URL,
client: client,
default: 'disconnected'
},
states: {
connected: {
disconnect: function () {
this.client.disconnect()
.then(
() => {
this.handle('disconnect')
}
)
}
},
connecting: {
disconnect: { deferUntil: 'connected' }
},
disconnecting: {
connect: { deferUntil: 'disconnected' }
},
disconnected: {
onEntry: { emit: 'ready', wait: 50 },
connect: function () {
this.client.connect(this.url)
.then(
() => {
this.next('connected')
}
)
this.next('connecting')
this.once('closed', () => {
this.next('disconnected')
})
}
}
}
}
})
clientFsm.connect()
.then(() => { console.log("connection established") })
Declarative Handle Properties
emit
- emits an event from the FSMnext
- transitions FSM to a new stateafter
- shorthand for deferUntildeferUntil
- delays handling the event until after a specific state has occurredforward
- forwards the event to a new state (after + next)wait
- amount of time (in ms) to wait before emitting events or transitioning