-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathparallel-state.js
38 lines (28 loc) · 1.11 KB
/
parallel-state.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
var Nanobus = require('nanobus')
var assert = require('assert')
module.exports = Parallelstate
function Parallelstate (transitions) {
assert.equal(typeof transitions, 'object', 'nanostate: transitions should be type object')
this.scopes = Object.keys(transitions)
this.transitions = transitions
Object.defineProperty(this, 'state', {
get: function () {
return this.scopes.reduce(function (state, scope) {
state[scope] = transitions[scope].state
return state
}, {})
}
})
Nanobus.call(this)
}
Parallelstate.prototype = Object.create(Nanobus.prototype)
Parallelstate.prototype.emit = function (eventName) {
var hasColon = eventName.indexOf(':') >= 0
assert.ok(hasColon, `nanostate.emit: invalid transition ${this.state} -> ${eventName}. For parallel nanostate eventName must have a colon ":"`)
var eventNameSplitted = eventName.split(':')
var scope = eventNameSplitted[0]
var event = eventNameSplitted[1]
assert.ok(scope, `nanostate.emit: invalid scope ${scope} for parallel emitting`)
this.transitions[scope].emit(event)
Nanobus.prototype.emit.call(this, eventName)
}