-
Notifications
You must be signed in to change notification settings - Fork 28
/
trigger.js
71 lines (56 loc) · 1.96 KB
/
trigger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import errors from 'errors'
/**
* triggerCommands
* @desc 通过传入的参数改变当前状态
* @param {String} name
* @return {Function}
*/
export default function triggerCommands() {
return function changeState(name) {
const state = this.currentState
let action = this.states[state][name]
this.config.debug && console.log(`W-TGR:Trying to change state from '${state}.${name}' to '${'function' === typeof action ? action.state : action}' state.`)
// this.config.debug && console.log(`state: ${state}, \nname:${name}, \naction:${'function' === typeof action ? action.state : action}`)
if (action) {
if ('string' === typeof action && ~action.indexOf('error')) action = errors[action.split('.')[1]]
if ('function' !== typeof action) {
throw new Error(`W-TGR:Have you ever defined this action before? \ntype: '${typeof action}'\nname: '${name}'\nstate: '${state}'`)
} else {
return exec.apply(this, Array.prototype.slice.call(arguments, 1))
}
} else {
return console.warn(`W-TGR:The ${state} does not have internal method ${name}`)
}
function exec() {
try {
action.apply(this, arguments)
if (state === 'init' && name === 'failure') {
this.currentState = 'start'
} else {
this.currentState = action.state
}
this.config.debug && console.log(`W-TGR:Change state success, Previous state:${state}, Be called mehtod name: ${name === 'failure' ? 'errors.' + state : name}, Current state: ${this.currentState}`)
} catch (error) {
console.error(error, `W-TGR:at action:${action} - name:${name}. `)
this.currentState = state
}
}
}
}
/**
init = function () {
console.log('into init ')
}
search = function () {
console.log('into search ')
}
connect = function () {
console.log('into connect ')
}
transfer = function () {
console.log('into transfer ')
}
finish = function () {
console.log('into finish ')
}
*/