Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9509a15

Browse files
committed
refactor state machine
1 parent4bb0575 commit9509a15

File tree

5 files changed

+39
-60
lines changed

5 files changed

+39
-60
lines changed

‎package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858
"prettier":"^1.18.2",
5959
"tslint":"^5.17.0",
6060
"tslint-config-prettier":"^1.18.0",
61-
"typescript":"^3.5.1",
62-
"vscode":"^1.1.28"
61+
"typescript":"^3.5.1"
6362
},
6463
"dependencies": {
65-
"xstate":"^4.6.1"
64+
"xstate":"^4.6.1",
65+
"vscode":"^1.1.28"
6666
},
6767
"license":"SEE LICENSE IN LICENSE.md"
6868
}

‎src/state/actions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default {
4747
},
4848
tutorialSetup(){
4949
vscode.commands.executeCommand('coderoad.tutorial_setup',currentTutorial)
50+
vscode.commands.executeCommand('coderoad.open_webview',vscode.ViewColumn.Two)
5051
},
5152
tutorialContinue:assign({
5253
// load initial data, progress & position

‎src/state/guards/index.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
import*asCRfrom'typings'
22

33
exportdefault{
4-
// skip to the stage if the level has already been started
5-
hasNoNextLevelProgress:(context:CR.MachineContext):boolean=>{
6-
console.log('GUARD: hasNoNextLevelProgress')
7-
returnfalse
8-
},
9-
tasksComplete:(context:CR.MachineContext):boolean=>{
10-
console.log('GUARD: tasksComplete')
11-
returnfalse
12-
},
134
hasNextStep:(context:CR.MachineContext):boolean=>{
14-
console.log('GUARD: hasNextStep')
15-
const{ data, position, progress}=context
5+
const{ data, position}=context
166
conststeps=data.stages[position.stageId].stepList
17-
constisStageComplete=progress.stages[position.stageId]||steps[steps.length-1]===position.stepId
18-
return!isStageComplete
7+
consthasNext=steps[steps.length-1]!==position.stepId
8+
console.log('GUARD: hasNextStep',hasNext)
9+
returnhasNext
1910
},
2011
hasNextStage:(context:CR.MachineContext):boolean=>{
21-
console.log('GUARD: hasNextStage')
22-
const{ data, position, progress}=context
12+
const{ data, position}=context
2313
conststages=data.levels[position.levelId].stageList
24-
constisLevelComplete=progress.levels[position.levelId]||stages[stages.length-1]===position.stageId
25-
return!isLevelComplete
14+
consthasNext=stages[stages.length-1]!==position.stageId
15+
console.log('GUARD: hasNextStage',hasNext)
16+
returnhasNext
2617
},
2718
hasNextLevel:(context:CR.MachineContext):boolean=>{
28-
console.log('GUARD: hasNextLevel')
29-
const{ data, position, progress}=context
19+
const{ data, position}=context
3020
constlevels=data.summary.levelList
31-
constisTutorialComplete=progress.complete||levels[levels.length-1]===position.levelId
32-
return!isTutorialComplete
21+
consthasNext=levels[levels.length-1]!==position.levelId
22+
console.log('GUARD: hasNextLevel',hasNext)
23+
returnhasNext
3324
},
3425
}

‎src/state/machine.ts

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const machine = Machine<
5050
ContinueTutorial:{
5151
onEntry:['tutorialContinue'],
5252
on:{
53-
TUTORIAL_START:'#tutorial-load-next-stage'
53+
TUTORIAL_START:'#tutorial-load-next'
5454
}
5555
},
5656
}
@@ -60,18 +60,22 @@ export const machine = Machine<
6060
initial:'Summary',
6161
onEntry:['tutorialSetup'],
6262
states:{
63-
LoadNextStage:{
64-
id:'tutorial-load-next-stage',
63+
LoadNext:{
64+
id:'tutorial-load-next',
6565
onEntry:['tutorialLoadNext'],
6666
on:{
6767
LOAD_NEXT:[
6868
{
69-
target:'Level',
70-
cond:'hasNoNextLevelProgress',
69+
target:'Stage',
70+
cond:'hasNextStage',
7171
},
7272
{
73-
target:'Stage',
73+
target:'Level',
74+
cond:'hasNextLevel'
7475
},
76+
{
77+
target:'#end-tutorial'
78+
}
7579
],
7680
},
7781
},
@@ -90,9 +94,9 @@ export const machine = Machine<
9094
},
9195
Stage:{
9296
onEntry:['loadStage'],
93-
initial:'StageNormal',
97+
initial:'Normal',
9498
states:{
95-
StageNormal:{
99+
Normal:{
96100
on:{
97101
TEST_RUN:'TestRunning',
98102
STEP_SOLUTION_LOAD:{
@@ -103,22 +107,14 @@ export const machine = Machine<
103107
TestRunning:{
104108
onEntry:['testStart'],
105109
on:{
106-
TEST_PASS:[
107-
{
108-
target:'StageComplete',
109-
cond:'tasksComplete',
110-
},
111-
{
112-
target:'TestPass',
113-
},
114-
],
110+
TEST_PASS:'TestPass',
115111
TEST_FAIL:'TestFail',
116112
},
117113
},
118114
TestPass:{
119115
onEntry:['testPass','stepComplete'],
120116
after:{
121-
0:{
117+
1000:{
122118
target:'StepNext',
123119
cond:'hasNextStep',
124120
}
@@ -130,36 +126,27 @@ export const machine = Machine<
130126
TestFail:{
131127
onEntry:['testFail'],
132128
after:{
133-
0:'StageNormal'
129+
0:'Normal'
134130
},
135131
},
136132
StepNext:{
137133
onEntry:['stepLoadNext'],
138134
after:{
139-
0:'StageNormal'
135+
0:'Normal'
140136
}
141137
},
142138
StageComplete:{
143139
onEntry:'stageComplete',
144140
on:{
145-
NEXT:[
146-
{
147-
target:'Stage',
148-
cond:'hasNextStage',
149-
},
150-
{
151-
target:'Level',
152-
cond:'hasNextLevel',
153-
},
154-
{
155-
target:'#root.Tutorial.EndTutorial',
156-
},
157-
],
141+
NEXT:'#tutorial-load-next',
158142
},
159143
},
160144
},
161145
},
162-
EndTutorial:{},
146+
EndTutorial:{
147+
id:'end-tutorial',
148+
type:'final'
149+
},
163150
}
164151
}
165152
}

‎typings/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ export interface MachineStateSchema {
146146
}
147147
Tutorial:{
148148
states:{
149-
LoadNextStage:{}
150149
Summary:{}
150+
LoadNext:{}
151151
Level:{}
152152
Stage:{
153153
states:{
154-
StageNormal:{}
154+
Normal:{}
155155
TestRunning:{}
156156
TestPass:{}
157157
TestFail:{}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp