vstirbu/fsm-as-promised

FSM object as event emitter

Closed this issue · 4 comments

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

This is in addition to the current implementation, not instead of, correct?

Yes! Sorry for the confusion :)

I haven't looked at the branch, but I'd imagine you'd want to pass oldState into the event handler as well

solved by a23f345