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

Commit97b878a

Browse files
committed
initialize state machine with tutorial model
1 parent0dd047f commit97b878a

File tree

4 files changed

+83
-60
lines changed

4 files changed

+83
-60
lines changed

‎src/extension.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import*asvscodefrom'vscode'
2-
import{setWorkspaceRoot}from'./services/node'
2+
import{setWorkspaceRoot}from'./services/node'
3+
importTutorialfrom'./services/tutorial'
34
importStateMachinefrom'./state'
45
importEditorfrom'./editor'
56

7+
exportconsttutorial=newTutorial()
68
// state machine that governs application logic
7-
exportconstmachine=newStateMachine({dispatch:vscode.commands.executeCommand})
9+
exportconstmachine=newStateMachine({dispatch:vscode.commands.executeCommand, tutorial})
810

911
// vscode editor
1012
exportconsteditor=newEditor({
11-
machine,
12-
setWorkspaceRoot,
13+
machine,
14+
setWorkspaceRoot,
1315
})
1416

1517
// activate run on vscode extension initialization

‎src/services/tutorial/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
import*asGfrom'typings/graphql'
22
import*asCRfrom'typings'
33

4+
interfaceTutorialConfig{
5+
codingLanguage:G.EnumCodingLanguage
6+
testRunner:G.EnumTestRunner
7+
}
8+
49
classTutorial{
510
publicrepo:G.TutorialRepo
6-
publicconfig:{codingLanguage:G.EnumCodingLanguage,testRunner:G.EnumTestRunner}
11+
publicconfig:TutorialConfig
712
privateversion:G.TutorialVersion
813
privateposition:CR.Position
914
privateprogress:CR.Progress
1015

11-
constructor(tutorial:G.Tutorial){
16+
constructor(){
17+
// initialize types, will be assigned when tutorial is selected
18+
this.repo={}asG.TutorialRepo
19+
this.config={}asTutorialConfig
20+
this.version={}asG.TutorialVersion
21+
this.position={}asCR.Position
22+
this.progress={}asCR.Progress
23+
}
24+
25+
publicinit=(tutorial:G.Tutorial)=>{
1226
this.repo=tutorial.repo
1327
this.config={
1428
codingLanguage:tutorial.codingLanguage,
1529
testRunner:tutorial.testRunner,
1630
}
17-
//TODO: allow specificversion, currently defaults to latest
31+
// version containing level data
1832
this.version=tutorial.version
1933
// set initial position
2034
this.position={
@@ -29,9 +43,11 @@ class Tutorial {
2943
steps:{},
3044
complete:false,
3145
}
46+
47+
// set position, progress, tutorial locally
3248
}
33-
publicload=()=>{
34-
//
49+
publicload=(tutorial:G.Tutorial)=>{
50+
//TODO: load from localStorage
3551
}
3652
publiclevel=(levelId:string):G.Level|null=>{
3753
constlevel:G.Level|undefined=this.version.levels.find((l:G.Level)=>l.id===levelId)

‎src/state/index.ts

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import{interpret,Interpreter}from'xstate'
1+
import{interpret,Interpreter}from'xstate'
2+
importTutorialfrom'../services/tutorial'
23
import*asCRfrom'typings'
34
importcreateMachinefrom'./machine'
45

@@ -7,61 +8,64 @@ import createMachine from './machine'
78

89
// convert state into a readable string
910
conststateToString=(state:string|object,str:string=''):string=>{
10-
if(typeofstate==='object'){
11-
constkeys=Object.keys(state)
12-
if(keys&&keys.length){
13-
constkey=keys[0]
14-
returnstateToString(state[key],str.length ?`${str}.${key}` :key)
15-
}
16-
returnstr
17-
}elseif(typeofstate==='string'){
18-
returnstate
19-
}
20-
return''
11+
if(typeofstate==='object'){
12+
constkeys=Object.keys(state)
13+
if(keys&&keys.length){
14+
constkey=keys[0]
15+
returnstateToString(state[key],str.length ?`${str}.${key}` :key)
16+
}
17+
returnstr
18+
}elseif(typeofstate==='string'){
19+
returnstate
20+
}
21+
return''
2122
}
2223

2324
interfaceProps{
24-
dispatch:CR.EditorDispatch
25+
dispatch:CR.EditorDispatch
26+
tutorial:Tutorial
2527
}
2628

2729
classStateMachine{
28-
privatedispatch:CR.EditorDispatch
29-
privatemachineOptions={
30-
devTools:true,
31-
deferEvents:true,
32-
execute:true,
33-
}
34-
privateservice:Interpreter<CR.MachineContext,CR.MachineStateSchema,CR.MachineEvent>
35-
constructor({ dispatch}:Props){
36-
this.dispatch=dispatch
37-
constmachine=createMachine(dispatch)
38-
this.service=interpret(machine,this.machineOptions)
39-
// logging
40-
.onTransition(state=>{
41-
if(state.changed){
42-
console.log(`STATE:${stateToString(state.value)}`)
43-
dispatch('coderoad.send_state',{state:state.value,data:state.context})
44-
}else{
45-
dispatch('coderoad.send_data',{data:state.context})
46-
}
47-
})
48-
}
49-
publicactivate(){
50-
// initialize
51-
this.service.start()
52-
}
53-
publicdeactivate(){
54-
this.service.stop()
55-
}
56-
publicrefresh(){
57-
console.log('service refresh')
58-
console.log(this.service.state)
59-
const{ value, context}=this.service.state
60-
this.dispatch('coderoad.send_state',{state:value,data:context})
61-
}
62-
publicsend(action:string|CR.Action){
63-
this.service.send(action)
64-
}
30+
privatedispatch:CR.EditorDispatch
31+
privatetutorial:Tutorial
32+
privatemachineOptions={
33+
devTools:true,
34+
deferEvents:true,
35+
execute:true,
36+
}
37+
privateservice:Interpreter<CR.MachineContext,CR.MachineStateSchema,CR.MachineEvent>
38+
constructor({dispatch, tutorial}:Props){
39+
this.dispatch=dispatch
40+
this.tutorial=tutorial
41+
constmachine=createMachine(dispatch)
42+
this.service=interpret(machine,this.machineOptions)
43+
// logging
44+
.onTransition(state=>{
45+
if(state.changed){
46+
console.log(`STATE:${stateToString(state.value)}`)
47+
dispatch('coderoad.send_state',{state:state.value,data:state.context})
48+
}else{
49+
dispatch('coderoad.send_data',{data:state.context})
50+
}
51+
})
52+
}
53+
publicactivate(){
54+
// initialize
55+
this.service.start()
56+
}
57+
publicdeactivate(){
58+
this.service.stop()
59+
}
60+
publicrefresh(){
61+
console.log('service refresh')
62+
console.log(this.service.state)
63+
const{value, context}=this.service.state
64+
this.dispatch('coderoad.send_state',{state:value,data:context})
65+
}
66+
publicsend(action:string|CR.Action){
67+
this.service.send(action)
68+
}
6569
}
6670

6771
exportdefaultStateMachine

‎tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"triple-equals":true,
1515
"forin":false,
1616
"no-console":false,
17-
"no-submodule-imports":false
17+
"no-submodule-imports":false,
18+
"no-object-literal-type-assertion":false
1819
},
1920
"defaultSeverity":"warning",
2021

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp