FSM object as event emitter
Closed this issue · 4 comments
vstirbu commented
With this feature the FSM object is able to emit events on state changes when the provided target object is an instance of EventEmitter, or by default when no target is provided:
var fsm = StateMachine({
initial: 'here',
events: [
{ name: 'jump', from: 'here', to: 'there' },
{ name: 'walk', from: 'there', to: 'here' }
]
});
fsm.on('state', function (newState) {
// do something when newState has some value
});
The rationale behind this feature is that it enables the fsm state to be observed without hooking into the promise chain.
A prototype implementation is available in the the emitter branch.
cc: @boneskull
boneskull commented
This is in addition to the current implementation, not instead of, correct?
vstirbu commented
Yes! Sorry for the confusion :)
boneskull commented
I haven't looked at the branch, but I'd imagine you'd want to pass oldState
into the event handler as well