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

Commita1f8922

Browse files
committed
refactor selectors
1 parent6107f9d commita1f8922

File tree

4 files changed

+77
-71
lines changed

4 files changed

+77
-71
lines changed
Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,2 @@
1-
import{MachineContext}from'typings'
2-
import*asGfrom'typings/graphql'
3-
import{createSelector}from'reselect'
4-
5-
exportconstcurrentTutorial=({tutorial}:MachineContext):G.Tutorial=>{
6-
if(!tutorial){
7-
thrownewError('Tutorial not found')
8-
}
9-
returntutorial
10-
}
11-
12-
exportconstcurrentVersion=createSelector(
13-
currentTutorial,
14-
(tutorial:G.Tutorial)=>{
15-
if(!tutorial.version){
16-
thrownewError('Tutorial version not found')
17-
}
18-
returntutorial.version
19-
})
20-
21-
exportconstcurrentLevel=(context:MachineContext):G.Level=>createSelector(
22-
currentVersion,
23-
(version:G.TutorialVersion):G.Level=>{
24-
// merge in the updated position
25-
// sent with the test to ensure consistency
26-
constlevels:G.Level[]=version.levels
27-
28-
constlevel:G.Level|undefined=levels.find((l:G.Level)=>l.id===context.position.levelId)
29-
30-
if(!level){
31-
thrownewError('Level not found when selecting level')
32-
}
33-
returnlevel
34-
})(context)
35-
36-
exportconstcurrentStage=(context:MachineContext):G.Stage=>createSelector(
37-
currentLevel,
38-
(level:G.Level):G.Stage=>{
39-
conststages:G.Stage[]=level.stages
40-
conststage:G.Stage|undefined=stages.find((s:G.Stage)=>s.id===context.position.stageId)
41-
if(!stage){
42-
thrownewError('No Stage found')
43-
}
44-
returnstage
45-
}
46-
)(context)
47-
48-
exportconstcurrentStep=(context:MachineContext):G.Step=>createSelector(
49-
currentStage,
50-
(stage:G.Stage):G.Step=>{
51-
conststeps:G.Step[]=stage.steps
52-
conststep:G.Step|undefined=steps.find((s:G.Step)=>s.id===context.position.stepId)
53-
if(!step){
54-
thrownewError('No Step found')
55-
}
56-
returnstep
57-
}
58-
)(context)
1+
export*from'./tutorial'
2+
export*from'./position'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import{createSelector}from'reselect'
2+
import*asGfrom'typings/graphql'
3+
import*asCRfrom'typings'
4+
import*astutorialfrom'./tutorial'
5+
6+
exportconstinitialPosition=createSelector(
7+
tutorial.currentVersion,
8+
(version:G.TutorialVersion)=>{
9+
constposition:CR.Position={
10+
levelId:version.levels[0].id,
11+
stageId:version.levels[0].stages[0].id,
12+
stepId:version.levels[0].stages[0].steps[0].id,
13+
}
14+
returnposition
15+
}
16+
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import{MachineContext}from'typings'
2+
import*asGfrom'typings/graphql'
3+
import{createSelector}from'reselect'
4+
5+
exportconstcurrentTutorial=({tutorial}:MachineContext):G.Tutorial=>{
6+
if(!tutorial){
7+
thrownewError('Tutorial not found')
8+
}
9+
returntutorial
10+
}
11+
12+
exportconstcurrentVersion=createSelector(
13+
currentTutorial,
14+
(tutorial:G.Tutorial)=>{
15+
if(!tutorial.version){
16+
thrownewError('Tutorial version not found')
17+
}
18+
returntutorial.version
19+
})
20+
21+
exportconstcurrentLevel=(context:MachineContext):G.Level=>createSelector(
22+
currentVersion,
23+
(version:G.TutorialVersion):G.Level=>{
24+
// merge in the updated position
25+
// sent with the test to ensure consistency
26+
constlevels:G.Level[]=version.levels
27+
28+
constlevel:G.Level|undefined=levels.find((l:G.Level)=>l.id===context.position.levelId)
29+
30+
if(!level){
31+
thrownewError('Level not found when selecting level')
32+
}
33+
returnlevel
34+
})(context)
35+
36+
exportconstcurrentStage=(context:MachineContext):G.Stage=>createSelector(
37+
currentLevel,
38+
(level:G.Level):G.Stage=>{
39+
conststages:G.Stage[]=level.stages
40+
conststage:G.Stage|undefined=stages.find((s:G.Stage)=>s.id===context.position.stageId)
41+
if(!stage){
42+
thrownewError('No Stage found')
43+
}
44+
returnstage
45+
}
46+
)(context)
47+
48+
exportconstcurrentStep=(context:MachineContext):G.Step=>createSelector(
49+
currentStage,
50+
(stage:G.Stage):G.Step=>{
51+
conststeps:G.Step[]=stage.steps
52+
conststep:G.Step|undefined=steps.find((s:G.Step)=>s.id===context.position.stepId)
53+
if(!step){
54+
thrownewError('No Step found')
55+
}
56+
returnstep
57+
}
58+
)(context)

‎web-app/src/services/state/actions/context.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,8 @@ export default {
2020
//@ts-ignore
2121
initPosition:assign({
2222
position:(context:CR.MachineContext,event:CR.MachineEvent):CR.Position=>{
23-
if(!event.payload.tutorial){
24-
thrownewError('Invalid tutorial')
25-
}
26-
27-
constversion:G.TutorialVersion=event.payload.tutorial.version
28-
29-
constposition:CR.Position={
30-
levelId:version.levels[0].id,
31-
stageId:version.levels[0].stages[0].id,
32-
stepId:version.levels[0].stages[0].steps[0].id,
33-
}
34-
23+
constposition:CR.Position=selectors.initialPosition(event.payload)
3524
storage.position.set(position)
36-
3725
returnposition
3826
},
3927
}),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp