UNPKG

937 BJavaScriptView Raw
1const Promise = require('bluebird');
2//const _ = require('lodash');
3
4module.exports = function (fsm, stateRepository) {
5 return {
6 entry: function (source) {
7 return stateRepository.init(source, fsm.init);
8 },
9 dealWith: function (event) {
10 var me = this;
11 return stateRepository.current(event.source)
12 .then(function (currentState) {
13 return me.doTrans(event, currentState);
14 });
15 },
16 doTrans: function (event, currentState) {
17 var transition = _.find(fsm.transitions, function (item) {
18 return item.name == event.name && item.from == currentState;
19 });
20 if (transition) {
21 stateRepository.update(event.source, transition.to);
22 return fsm.methods['on' + _.capitalize(event.name)](event.source, event.data);
23 }
24 }
25 }
26}
\No newline at end of file