forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobservers.js
151 lines (127 loc) · 8.38 KB
/
observers.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
import test from 'ava'
import StateMachine from '../src/app'
import LifecycleLogger from './helpers/lifecycle_logger'
//-------------------------------------------------------------------------------------------------
test('lifecycle events can be observed by external observer methods', t => {
var logger = new LifecycleLogger(),
fsm = new StateMachine({
transitions: [
{ name: 'step', from: 'none', to: 'complete' }
]
});
fsm.observe("onBeforeTransition", logger)
fsm.observe("onBeforeStep", logger)
fsm.observe("onLeaveState", logger)
fsm.observe("onLeaveNone", logger)
fsm.observe("onLeaveComplete", logger)
fsm.observe("onTransition", logger)
fsm.observe("onEnterState", logger)
fsm.observe("onEnterNone", logger)
fsm.observe("onEnterComplete", logger)
fsm.observe("onAfterTransition", logger)
fsm.observe("onAfterStep", logger)
fsm.step('additional', 'arguments')
t.deepEqual(logger.log, [
{ event: 'onBeforeTransition', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onBeforeStep', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onLeaveState', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onLeaveNone', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onTransition', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onEnterState', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onEnterComplete', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onAfterTransition', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onAfterStep', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] }
])
})
//-------------------------------------------------------------------------------------------------
test('lifecycle events can be observed by external observer classes', t => {
var logger = new LifecycleLogger(),
fsm = new StateMachine({
transitions: [
{ name: 'step', from: 'none', to: 'complete' }
]
});
fsm.observe({
"onBeforeTransition": logger,
"onBeforeStep": logger,
"onLeaveState": logger,
"onLeaveNone": logger,
"onLeaveComplete": logger,
"onTransition": logger,
"onEnterState": logger,
"onEnterNone": logger,
"onEnterComplete": logger,
"onAfterTransition": logger,
"onAfterStep": logger,
})
fsm.step('additional', 'arguments')
t.deepEqual(logger.log, [
{ event: 'onBeforeTransition', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onBeforeStep', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onLeaveState', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onLeaveNone', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onTransition', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onEnterState', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onEnterComplete', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onAfterTransition', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onAfterStep', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] }
])
})
//-------------------------------------------------------------------------------------------------
test('lifecycle events can be observed by multiple observers', t => {
var logger1 = new LifecycleLogger(),
logger2 = new LifecycleLogger(),
fsm = new StateMachine({
transitions: [
{ name: 'step', from: 'none', to: 'complete' }
]
});
fsm.observe("onBeforeTransition", logger1)
fsm.observe("onBeforeStep", logger1)
fsm.observe("onLeaveState", logger1)
fsm.observe("onLeaveNone", logger1)
fsm.observe("onLeaveComplete", logger1)
fsm.observe("onTransition", logger1)
fsm.observe("onEnterState", logger1)
fsm.observe("onEnterNone", logger1)
fsm.observe("onEnterComplete", logger1)
fsm.observe("onAfterTransition", logger1)
fsm.observe("onAfterStep", logger1)
fsm.observe({
"onBeforeTransition": logger2,
"onBeforeStep": logger2,
"onLeaveState": logger2,
"onLeaveNone": logger2,
"onLeaveComplete": logger2,
"onTransition": logger2,
"onEnterState": logger2,
"onEnterNone": logger2,
"onEnterComplete": logger2,
"onAfterTransition": logger2,
"onAfterStep": logger2,
})
fsm.step('additional', 'arguments')
t.deepEqual(logger1.log, [
{ event: 'onBeforeTransition', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onBeforeStep', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onLeaveState', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onLeaveNone', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onTransition', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onEnterState', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onEnterComplete', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onAfterTransition', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onAfterStep', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] }
])
t.deepEqual(logger2.log, [
{ event: 'onBeforeTransition', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onBeforeStep', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onLeaveState', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onLeaveNone', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onTransition', transition: 'step', from: 'none', to: 'complete', current: 'none', args: [ 'additional', 'arguments' ] },
{ event: 'onEnterState', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onEnterComplete', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onAfterTransition', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] },
{ event: 'onAfterStep', transition: 'step', from: 'none', to: 'complete', current: 'complete', args: [ 'additional', 'arguments' ] }
])
})
//-------------------------------------------------------------------------------------------------