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

Commit1253177

Browse files
committed
Refactor editor dispatch uses
1 parentf462b39 commit1253177

File tree

11 files changed

+50
-89
lines changed

11 files changed

+50
-89
lines changed

‎src/extension.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,18 @@ import Tutorial, {TutorialModel} from './services/tutorial'
44
importStateMachinefrom'./state'
55
importEditorfrom'./editor'
66

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

1111
// vscode editor
1212
exportconsteditor=newEditor({
1313
machine,
1414
setWorkspaceRoot,
1515
})
1616

17-
// TODO: refactor tutorial & editor relationships
18-
// code here is a bit smelly
19-
tutorial.setClientDispatch(editor.dispatch)
20-
2117
// activate run on vscode extension initialization
2218
exportconstactivate=editor.activate
2319

24-
//deactive run on vscode extension shut down
20+
//deactivate run on vscode extension shut down
2521
exportconstdeactivate=editor.deactivate

‎src/services/git/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function gitLoadCommits(actions: G.StepActions, editorDispatch: CR.
5858
const{stdout, stderr}=awaitexec(command)
5959
if(stderr){
6060
console.error(stderr)
61-
//langauge specific error messages from running commands
61+
//language specific error messages from running commands
6262
for(constmessageofObject.keys(errorMessages.js)){
6363
if(stderr.match(message)){
6464
// ignored error

‎src/services/tutorial/index.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ interface TutorialConfig {
99
testRunner:G.EnumTestRunner
1010
}
1111

12-
interfaceMessageData{
13-
tutorial?:{id:string}
14-
position:CR.Position
15-
progress:CR.Progress
16-
}
17-
1812
exportinterfaceTutorialModel{
1913
repo:G.TutorialRepo
2014
config:TutorialConfig
@@ -28,7 +22,7 @@ export interface TutorialModel {
2822
updateProgress():void
2923
nextPosition():CR.Position
3024
hasExisting():Promise<boolean>
31-
setClientDispatch(editorDispatch:CR.EditorDispatch):void
25+
syncClient():void
3226
}
3327

3428
classTutorialimplementsTutorialModel{
@@ -37,19 +31,21 @@ class Tutorial implements TutorialModel {
3731
publicversion:G.TutorialVersion
3832
publicposition:CR.Position
3933
publicprogress:CR.Progress
40-
privateclientDispatch:(props:MessageData)=>void
34+
publicsyncClient:()=>void
4135

42-
constructor(){
36+
constructor(editorDispatch:CR.EditorDispatch){
4337
// initialize types, will be assigned when tutorial is selected
44-
this.clientDispatch=(props:MessageData)=>{
45-
thrownewError('Tutorial client dispatch yet initialized')
46-
}
38+
4739
this.repo={}asG.TutorialRepo
4840
this.config={}asTutorialConfig
4941
this.version={}asG.TutorialVersion
5042
this.position={}asCR.Position
5143
this.progress={}asCR.Progress
52-
44+
this.syncClient=()=>editorDispatch('coderoad.send_data',{
45+
tutorial:{id:this.version.tutorialId},
46+
progress:this.progress,
47+
position:this.position,
48+
})
5349
// Promise.all([
5450
// storage.getTutorial(),
5551
// storage.getProgress(),
@@ -61,10 +57,6 @@ class Tutorial implements TutorialModel {
6157

6258
}
6359

64-
publicsetClientDispatch(editorDispatch:CR.EditorDispatch){
65-
this.clientDispatch=({progress, position}:MessageData)=>editorDispatch('coderoad.send_data',{progress, position})
66-
}
67-
6860
publicasynclaunch(tutorialId:string){
6961
const{tutorial}:{tutorial:G.Tutorial|null}=awaitapi.request(tutorialQuery,{
7062
tutorialId,// TODO: add selection of tutorial id
@@ -97,11 +89,7 @@ class Tutorial implements TutorialModel {
9789

9890
console.log('this.position',JSON.stringify(this.position))
9991

100-
this.clientDispatch({
101-
tutorial:{id:tutorial.id},
102-
position:this.position,
103-
progress:this.progress
104-
})
92+
this.syncClient()
10593

10694
// set tutorial, position, progress locally
10795
// TODO: base position off of progress
@@ -149,10 +137,7 @@ class Tutorial implements TutorialModel {
149137
this.progress.stages[stageId]=true
150138
this.progress.steps[stepId]=true
151139

152-
this.clientDispatch({
153-
progress:this.progress,
154-
position:this.position,// TODO: calculate next position
155-
})
140+
this.syncClient()
156141
}
157142
publicnextPosition=():CR.Position=>{
158143
const{levelId, stageId, stepId}=this.position

‎src/state/actions/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// NOTE: codesmell - importing machine
21
import{machine}from'../../extension'
32
import{TutorialModel}from'../../services/tutorial'
43
import*asCRfrom'typings'
54
import*asGfrom'typings/graphql'
65
import*asgitfrom'../../services/git'
76

87

9-
exportdefault(editorDispatch:CR.EditorDispatch,tutorialModel:TutorialModel)=>({
8+
exportdefault(tutorialModel:TutorialModel,editorDispatch:CR.EditorDispatch)=>({
109
createWebview(){
1110
editorDispatch('coderoad.open_webview')
1211
},

‎src/state/index.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,31 @@ const stateToString = (state: string | object, str: string = ''): string => {
2121
return''
2222
}
2323

24-
interfaceProps{
25-
dispatch:CR.EditorDispatch
26-
tutorial:TutorialModel
27-
}
2824

2925
classStateMachine{
30-
privatedispatch:CR.EditorDispatch
3126
privatemachineOptions={
3227
devTools:true,
3328
deferEvents:true,
3429
execute:true,
3530
}
3631
privateservice:Interpreter<{},CR.MachineStateSchema,CR.MachineEvent>
37-
constructor({dispatch, tutorial}:Props){
38-
this.dispatch=dispatch
39-
constmachine=createMachine(dispatch,tutorial)
32+
33+
constructor(tutorialModel:TutorialModel,editorDispatch:CR.EditorDispatch){
34+
constmachine=createMachine(tutorialModel,editorDispatch)
35+
36+
// format state as a string and send it to the client
37+
this.syncState=(state:any):void=>{
38+
conststateValue:CR.MessageState=stateToString(state.value)
39+
console.log(`STATE:${stateValue}`)
40+
editorDispatch('send_data',stateValue)
41+
}
42+
43+
// callback on all state changes
4044
this.service=interpret(machine,this.machineOptions)
4145
// logging
4246
.onTransition(state=>{
4347
if(state.changed){
44-
console.log(`STATE:${stateToString(state.value)}`)
45-
dispatch('coderoad.send_state',{state:state.value})
48+
this.syncState(state)
4649
}
4750
})
4851
}
@@ -56,12 +59,14 @@ class StateMachine {
5659
publicrefresh(){
5760
console.log('service refresh')
5861
console.log(this.service.state)
59-
const{value}=this.service.state
60-
this.dispatch('coderoad.send_state',{state:value})
62+
this.syncState(this.service.state)
6163
}
6264
publicsend(action:string|CR.Action){
6365
this.service.send(action)
6466
}
67+
//@ts-ignore
68+
privatesyncState(state:any):void
69+
6570
}
6671

6772
exportdefaultStateMachine

‎src/state/machine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {TutorialModel} from '../services/tutorial'
55
importcreateActionsfrom'./actions'
66
importcreateGuardsfrom'./guards'
77

8-
exportconstmachine=(editorDispatch:CR.EditorDispatch,tutorialModel:TutorialModel)=>
8+
exportconstmachine=(tutorialModel:TutorialModel,editorDispatch:CR.EditorDispatch)=>
99
Machine<{},CR.MachineStateSchema,CR.MachineEvent>(
1010
{
1111
id:'root',
@@ -164,7 +164,7 @@ export const machine = (editorDispatch: CR.EditorDispatch, tutorialModel: Tutori
164164
},
165165
},
166166
{
167-
actions:createActions(editorDispatch,tutorialModel),
167+
actions:createActions(tutorialModel,editorDispatch),
168168
guards:createGuards(tutorialModel),
169169
activities:{},
170170
},

‎typings/index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,13 @@ export interface StateMachine {
164164
send(action:string|Action):void
165165
}
166166

167-
exporttypeEditorDispatch=(type:string,payload?:any)=>void
167+
interfaceMessageData{
168+
tutorial?:{id:string}
169+
position:Position
170+
progress:Progress
171+
}
172+
173+
typeMessageState=string
174+
175+
176+
exporttypeEditorDispatch=(type:string,payload?:MessageData|MessageState)=>void

‎web-app/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const App = () => {
2323
})
2424

2525
// update level/stage/step status based on user progress & position
26-
// TODO: model server moreeffeciently
26+
// TODO: model server moreefficiently
2727
const[setStatus]=useMutation(SET_STATUS)
2828

2929
// event bus listener

‎web-app/src/components/Cond/index.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎web-app/src/components/Cond/utils/state.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎web-app/src/components/Router/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import*asReactfrom'react'
22
importRoutefrom'./Route'
3-
import{stateMatch}from'../Cond/utils/state'
43

54
interfaceProps{
65
state:string
76
children:any
87
}
98

9+
conststateMatch=(state:string,path:string)=>{
10+
return!!(newRegExp(`^${state}`).exec(path))
11+
}
12+
1013
// router finds first state match of <Route path='' />
1114
constRouter=({ state, children}:Props)=>{
1215
constchildArray=React.Children.toArray(children)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp