|
1 | 1 | import * as CR from 'typings' |
2 | | -import { Machine, MachineOptions } from 'xstate' |
3 | | -import actions from './actions' |
| 2 | +import { Machine } from 'xstate' |
| 3 | +import { selectTutorialMachine } from './selectTutorial' |
| 4 | +import { playTutorialMachine } from './playTutorial' |
4 | 5 |
|
5 | | -const options: MachineOptions<CR.MachineContext, CR.MachineEvent> = { |
6 | | - // @ts-ignore |
7 | | - actions, |
8 | | -} |
9 | | - |
10 | | -export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.MachineEvent>( |
11 | | - { |
12 | | - id: 'root', |
13 | | - initial: 'Start', |
14 | | - context: { |
15 | | - error: null, |
16 | | - env: { machineId: '', sessionId: '', token: '' }, |
17 | | - tutorial: null, |
18 | | - position: { levelId: '', stepId: '' }, |
19 | | - progress: { |
20 | | - levels: {}, |
21 | | - steps: {}, |
22 | | - complete: false, |
23 | | - }, |
24 | | - processes: [], |
25 | | - }, |
26 | | - states: { |
27 | | - Start: { |
28 | | - initial: 'Startup', |
29 | | - states: { |
30 | | - Startup: { |
31 | | - onEntry: ['loadEnv'], |
32 | | - on: { |
33 | | - ENV_LOAD: { |
34 | | - target: 'Authenticate', |
35 | | - actions: ['setEnv'], |
36 | | - }, |
37 | | - }, |
38 | | - }, |
39 | | - Authenticate: { |
40 | | - onEntry: ['authenticate'], |
41 | | - on: { |
42 | | - AUTHENTICATED: 'NewOrContinue', |
43 | | - ERROR: { |
44 | | - actions: ['setError'], |
45 | | - }, |
46 | | - }, |
47 | | - }, |
48 | | - NewOrContinue: { |
49 | | - onEntry: ['loadStoredTutorial'], |
50 | | - on: { |
51 | | - CONTINUE_TUTORIAL: { |
52 | | - target: 'ContinueTutorial', |
53 | | - actions: ['continueTutorial'], |
54 | | - }, |
55 | | - NEW_TUTORIAL: { |
56 | | - target: 'SelectTutorial', |
57 | | - }, |
58 | | - }, |
59 | | - }, |
60 | | - SelectTutorial: { |
61 | | - onEntry: ['clearStorage'], |
62 | | - id: 'start-new-tutorial', |
63 | | - on: { |
64 | | - TUTORIAL_START: { |
65 | | - target: '#tutorial', |
66 | | - actions: ['newTutorial'], |
67 | | - }, |
68 | | - }, |
69 | | - }, |
70 | | - ContinueTutorial: { |
71 | | - on: { |
72 | | - TUTORIAL_START: { |
73 | | - target: '#tutorial-level', |
74 | | - actions: ['continueConfig'], |
75 | | - }, |
76 | | - TUTORIAL_SELECT: 'SelectTutorial', |
77 | | - }, |
78 | | - }, |
| 6 | +export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.MachineEvent>({ |
| 7 | + id: 'root', |
| 8 | + initial: 'SelectTutorial', |
| 9 | + context: { |
| 10 | + error: null, |
| 11 | + env: { machineId: '', sessionId: '', token: '' }, |
| 12 | + tutorial: null, |
| 13 | + }, |
| 14 | + states: { |
| 15 | + // start/continue a tutorial |
| 16 | + // select tutorial |
| 17 | + // view tutorial summary |
| 18 | + SelectTutorial: { |
| 19 | + invoke: { |
| 20 | + src: selectTutorialMachine, |
| 21 | + onDone: 'PlayTutorial', |
| 22 | + data: { |
| 23 | + env: (context: CR.MachineContext) => context.env, |
| 24 | + tutorial: (context: CR.MachineContext) => context.tutorial, |
| 25 | + error: null, |
79 | 26 | }, |
80 | 27 | }, |
81 | | - Tutorial: { |
82 | | - id: 'tutorial', |
83 | | - initial: 'Initialize', |
84 | | - on: { |
85 | | - // track commands |
86 | | - COMMAND_START: { |
87 | | - actions: ['commandStart'], |
88 | | - }, |
89 | | - COMMAND_SUCCESS: { |
90 | | - actions: ['commandSuccess'], |
91 | | - }, |
92 | | - COMMAND_FAIL: { |
93 | | - actions: ['commandFail'], |
94 | | - }, |
95 | | - ERROR: { |
96 | | - actions: ['setError'], |
97 | | - }, |
98 | | - }, |
99 | | - states: { |
100 | | - // TODO move Initialize into New Tutorial setup |
101 | | - Initialize: { |
102 | | - onEntry: ['initializeTutorial'], |
103 | | - on: { |
104 | | - TUTORIAL_CONFIGURED: 'Summary', |
105 | | - // TUTORIAL_CONFIG_ERROR: 'Start' // TODO should handle error |
106 | | - }, |
107 | | - }, |
108 | | - Summary: { |
109 | | - on: { |
110 | | - LOAD_TUTORIAL: { |
111 | | - target: 'Level', |
112 | | - actions: ['initPosition', 'initTutorial'], |
113 | | - }, |
114 | | - }, |
115 | | - }, |
116 | | - LoadNext: { |
117 | | - id: 'tutorial-load-next', |
118 | | - onEntry: ['loadNext'], |
119 | | - on: { |
120 | | - NEXT_STEP: { |
121 | | - target: 'Level', |
122 | | - actions: ['updatePosition'], |
123 | | - }, |
124 | | - NEXT_LEVEL: { |
125 | | - target: 'Level', // TODO should return to levels summary page |
126 | | - actions: ['updatePosition'], |
127 | | - }, |
128 | | - COMPLETED: '#completed-tutorial', |
129 | | - }, |
130 | | - }, |
131 | | - Level: { |
132 | | - initial: 'Load', |
133 | | - states: { |
134 | | - Load: { |
135 | | - onEntry: ['loadLevel', 'loadStep'], |
136 | | - after: { |
137 | | - 0: 'Normal', |
138 | | - }, |
139 | | - }, |
140 | | - Normal: { |
141 | | - id: 'tutorial-level', |
142 | | - on: { |
143 | | - TEST_RUNNING: 'TestRunning', |
144 | | - STEP_SOLUTION_LOAD: { |
145 | | - actions: ['editorLoadSolution'], |
146 | | - }, |
147 | | - }, |
148 | | - }, |
149 | | - TestRunning: { |
150 | | - onEntry: ['testStart'], |
151 | | - on: { |
152 | | - TEST_PASS: { |
153 | | - target: 'TestPass', |
154 | | - actions: ['updateStepProgress'], |
155 | | - }, |
156 | | - TEST_FAIL: 'TestFail', |
157 | | - TEST_ERROR: 'TestError', |
158 | | - }, |
159 | | - }, |
160 | | - TestError: { |
161 | | - onEntry: ['testFail'], |
162 | | - after: { |
163 | | - 0: 'Normal', |
164 | | - }, |
165 | | - }, |
166 | | - TestPass: { |
167 | | - onExit: ['updateStepPosition'], |
168 | | - after: { |
169 | | - 1000: 'StepNext', |
170 | | - }, |
171 | | - }, |
172 | | - TestFail: { |
173 | | - onEntry: ['testFail'], |
174 | | - after: { |
175 | | - 0: 'Normal', |
176 | | - }, |
177 | | - }, |
178 | | - StepNext: { |
179 | | - onEntry: ['stepNext'], |
180 | | - on: { |
181 | | - LOAD_NEXT_STEP: { |
182 | | - target: 'Normal', |
183 | | - actions: ['loadStep'], |
184 | | - }, |
185 | | - LEVEL_COMPLETE: { |
186 | | - target: 'LevelComplete', |
187 | | - actions: ['updateLevelProgress'], |
188 | | - }, |
189 | | - }, |
190 | | - }, |
191 | | - LevelComplete: { |
192 | | - on: { |
193 | | - LEVEL_NEXT: '#tutorial-load-next', |
194 | | - }, |
195 | | - }, |
196 | | - }, |
197 | | - }, |
198 | | - Completed: { |
199 | | - id: 'completed-tutorial', |
200 | | - onEntry: ['userTutorialComplete'], |
201 | | - on: { |
202 | | - SELECT_TUTORIAL: { |
203 | | - target: '#start-new-tutorial', |
204 | | - actions: ['reset'], |
205 | | - }, |
206 | | - }, |
207 | | - }, |
| 28 | + }, |
| 29 | + // initialize a selected tutorial |
| 30 | + // progress through tutorial level/steps |
| 31 | + // complete tutorial |
| 32 | + PlayTutorial: { |
| 33 | + invoke: { |
| 34 | + src: playTutorialMachine, |
| 35 | + onDone: 'SelectTutorial', |
| 36 | + data: { |
| 37 | + context: (context: CR.MachineContext) => context.env, |
| 38 | + tutorial: (context: CR.MachineContext) => context.tutorial, |
| 39 | + error: null, |
208 | 40 | }, |
209 | 41 | }, |
210 | 42 | }, |
211 | 43 | }, |
212 | | - options, |
213 | | -) |
| 44 | +}) |
214 | 45 |
|
215 | 46 | export default machine |
0 commit comments