-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
183 lines (165 loc) · 4.07 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Dependencies
*/
const emitter = require('zeroin')
/**
* Create finite state machine.
*
* @param {String?} init state
* @param {Object?} map
* @return {Emitter}
* @public
*/
module.exports = (init, map) => {
let state = current(map || init)
const machine = emitter()
/**
* Add transition.
*
* @param {String} topic
* @param {String?} condition
* @param {Function} transition
* @param {String} resolved
* @param {String?} rejected
* @private
*/
const add = function (topic, transition, resolved, rejected){
return new Promise(resolve => {
machine.on(topic, (args, cb) => {
Promise.resolve(transition.call(machine, ...args))
.then(
(...attrs) => {
return Promise.resolve(callbackify(resolved)(...attrs))
.then(str => machine.state(str, ...attrs))
},
(...attrs) =>{
return Promise.resolve(callbackify(rejected)(...attrs))
.then(str => machine.state(str, ...attrs))
}
).then(cb)
})
})
}
/**
* Add state machine transition.
*
* A transition occurs when a given condition (optional) is triggered
* and can change the state machine current state.
*
* Examples:
*
* machine.add('before', 'condition', cb, 'after')
* machine.add('before', cb)
* machine.add('before', cb, 'after')
* machine.add('before', 'condition', 'after')
* machine.add('before', 'condition', () => {
* return Promise.resolve('something')
* }, 'resolved')
* machine.add('before', 'condition', () => {
* return Promise.reject('something')
* }, 'resolved', 'rejected')
*
* @param {String} topic
* @param {String | Function} condition
* @param {Function | String} transition
* @param {String?} resolved
* @param {String?} rejected
* @public
*/
machine.add = function (topic, condition, transition, resolved, rejected) {
if (typeof condition === 'function') {
add(topic, condition, transition, resolved)
} else {
if (typeof transition === 'string') {
resolved = transition
transition = (...args) => Promise.resolve(args)
}
add(state + ' ' + condition, transition, resolved, rejected)
}
}
/**
* Trigger transition by emitting condition
* on current state.
*
* Transitions are synchronous and this method returns
* a promise that resolve with new state.
*
* Examples:
*
* machine.trigger('condition')
* .then(state => console.log(state))
*
*
* @param {String} condition
* @return {Promise}
* @public
*/
machine.trigger = function (condition, ...args) {
return new Promise(resolve => {
machine.emit(state + ' ' + condition, args, resolve)
})
}
/**
* Set state or return current state.
*
* Examples:
*
* machine.state('opened')
* machine.state()
* // => opened
*
* @param {String?}
* @return {String}
* @public
*/
machine.state = function (str, ...args) {
if (str) {
state = str
machine.emit(state, args)
return state
} else return state
}
initialize(machine, map || init)
machine.state(state)
return machine
}
/**
* Transform string or null/undefined values
* into callbacks.
*
* @param {String?|Function?} cb
* @return {Function}
* @private
*/
function callbackify (cb = () => {}) {
return typeof cb === 'string' ? () => cb : cb
}
/**
* Add map states to the state machine.
*
* @param {Object} machine
* @param {Object} map
* @private
*/
function initialize (machine, map = {}) {
if (typeof map === 'object') {
Object.keys(map).map(key => {
const arr = map[key]
if (arr[0] instanceof Array) {
arr.map(item => machine.add(key, ...item))
} else machine.add(key, ...arr)
})
}
}
/**
* Get current state from
*
* @param {String|Function|Object} state
* @return {String} (null if not passed)
* @private
*/
function current (state) {
const type = typeof state
if (type === 'string') return state
else return Object.keys(state)[0]
}