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

Commit38b0bad

Browse files
authored
Merge pull requestcoderoad#9 from ShMcK/fix/separate-concerns
separate vscode from services
2 parents3630acf +06a4d29 commit38b0bad

File tree

11 files changed

+53
-108
lines changed

11 files changed

+53
-108
lines changed

‎src/editor/commands/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const COMMANDS = {
1616
RECEIVE_ACTION:'coderoad.receive_action',
1717
OPEN_FILE:'coderoad.open_file',
1818
RUN_TEST:'coderoad.run_test',
19+
TEST_PASS:'coderoad.test_pass',
20+
TEST_FAIL:'coderoad.test_fail',
21+
SET_LAYOUT:'coderoad.set_layout',
1922
}
2023

2124
interfaceCreateCommandProps{
@@ -41,8 +44,12 @@ export const createCommands = ({ context, machine, storage, git, position }: Cre
4144
machine.activate()
4245
},
4346
// open React webview
44-
[COMMANDS.OPEN_WEBVIEW]:(column:number=vscode.ViewColumn.One)=>{
47+
[COMMANDS.OPEN_WEBVIEW]:(column:number=vscode.ViewColumn.Two)=>{
48+
// setup 1x1 horizontal layout
49+
vscode.commands.executeCommand('vscode.setEditorLayout',{orientation:0,groups:[{groups:[{}],size:0.6},{groups:[{}],size:0.4}]})
4550
webview.createOrShow(column);
51+
// NOTE: createOrShow and layout command cannot be async
52+
// this creates an async issue where the webview cannot detect when it has been initialized
4653
setTimeout(()=>{
4754
machine.send('WEBVIEW_INITIALIZED')
4855
},2000)
@@ -112,5 +119,15 @@ export const createCommands = ({ context, machine, storage, git, position }: Cre
112119
onSuccess:()=>machine.send('TEST_PASS'),
113120
onFail:()=>machine.send('TEST_FAIL')
114121
})
115-
}
122+
},
123+
[COMMANDS.TEST_PASS]:()=>{
124+
vscode.window.showInformationMessage('PASS')
125+
},
126+
[COMMANDS.TEST_FAIL]:()=>{
127+
vscode.window.showWarningMessage('FAIL')
128+
},
129+
[COMMANDS.SET_LAYOUT]:()=>{
130+
console.log('setLayout')
131+
vscode.commands.executeCommand('vscode.setEditorLayout',{orientation:0,groups:[{groups:[{}],size:0.6},{groups:[{}],size:0.4}]})
132+
},
116133
})

‎src/editor/commands/loadSolution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as CR from 'typings'
22
import*asstoragefrom'../../services/storage'
33
import{gitLoadCommits,gitClear}from'../../services/git'
44

5-
exportdefaultasyncfunctionloadSolution():Promise<void>{
5+
exportdefaultasyncfunctionloadSolution(dispatch:CR.EditorDispatch):Promise<void>{
66
const[position,tutorial]:[CR.Position,CR.Tutorial|undefined]=awaitPromise.all([
77
storage.getPosition(),
88
storage.getTutorial(),
@@ -17,5 +17,5 @@ export default async function loadSolution(): Promise<void> {
1717
const{ solution}=tutorial.data.steps[position.stepId].actions
1818

1919
awaitgitClear()
20-
awaitgitLoadCommits(solution)
20+
awaitgitLoadCommits(solution,dispatch)
2121
}

‎src/editor/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Editor {
6161
}
6262

6363
// execute vscode command
64-
publicdispatch=(type:string,payload:any)=>{
64+
publicdispatch=(type:string,payload?:any)=>{
6565
vscode.commands.executeCommand(type,payload)
6666
}
6767
}

‎src/extension.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
import*asvscodefrom'vscode'
12
import{setWorkspaceRoot}from'./services/node'
23
importStateMachinefrom'./state'
34
importEditorfrom'./editor'
45

56

67
// state machine that governs application logic
7-
exportconstmachine=newStateMachine()
8+
exportconstmachine=newStateMachine({dispatch:vscode.commands.executeCommand})
89

910
// vscode editor
1011
exportconsteditor=newEditor({
1112
machine,
1213
setWorkspaceRoot,
1314
})
1415

16+
// activate run on vscode extension initialization
1517
exportconstactivate=editor.activate
18+
19+
// deactive run on vscode extension shut down
1620
exportconstdeactivate=editor.deactivate

‎src/services/git/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import*asvscodefrom'vscode'
21
import*asCRfrom'typings'
32
import{exec,exists}from'../node'
43

@@ -9,7 +8,7 @@ const gitOrigin = 'coderoad'
98
MULTIPLE git cherry-pick %COMMIT_START%..%COMMIT_END%
109
if shell, run shell
1110
*/
12-
exportasyncfunctiongitLoadCommits(actions:CR.TutorialAction):Promise<void>{
11+
exportasyncfunctiongitLoadCommits(actions:CR.TutorialAction,dispatch:CR.EditorDispatch):Promise<void>{
1312
const{ commits, commands, files}=actions
1413

1514
console.log('commits to load',commits)
@@ -41,7 +40,7 @@ export async function gitLoadCommits(actions: CR.TutorialAction): Promise<void>
4140

4241
if(files){
4342
for(constfilePathoffiles){
44-
vscode.commands.executeCommand('coderoad.open_file',filePath)
43+
dispatch('coderoad.open_file',filePath)
4544
}
4645
}
4746
}

‎src/services/testResult.ts

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

‎src/services/tutorialSetup.ts

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

‎src/state/actions/index.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { assign } from 'xstate'
33
import{machine}from'../../extension'
44
importapifrom'../../services/api'
55
import*asCRfrom'typings'
6-
import*asvscodefrom'vscode'
76
import*asstoragefrom'../../services/storage'
87
import*asgitfrom'../../services/git'
98

@@ -15,10 +14,10 @@ let currentProgress: CR.Progress = {
1514
complete:false,
1615
}
1716

18-
exportdefault{
17+
exportdefault(dispatch:CR.EditorDispatch)=>({
1918
createWebview(){
2019
console.log('execute coderoad.open_webview')
21-
vscode.commands.executeCommand('coderoad.open_webview')
20+
dispatch('coderoad.open_webview')
2221
},
2322
asyncnewOrContinue(){
2423
// verify that the user has a tutorial & progress
@@ -45,11 +44,11 @@ export default {
4544
currentTutorial=tutorial
4645
console.log('api')
4746
console.log(tutorial)
48-
vscode.commands.executeCommand('coderoad.tutorial_launch',tutorial)
47+
dispatch('coderoad.tutorial_launch',tutorial)
4948
},
5049
tutorialSetup(){
51-
vscode.commands.executeCommand('coderoad.tutorial_setup',currentTutorial)
52-
vscode.commands.executeCommand('coderoad.open_webview',vscode.ViewColumn.Two)
50+
dispatch('coderoad.tutorial_setup',currentTutorial)
51+
dispatch('coderoad.open_webview',2)
5352
},
5453
initializeNewTutorial:assign({
5554
position:(context:any):CR.Position=>{
@@ -104,13 +103,13 @@ export default {
104103
}
105104
}),
106105
testStart(){
107-
vscode.commands.executeCommand('coderoad.run_test')
106+
dispatch('coderoad.run_test')
108107
},
109108
testPass(){
110-
vscode.window.showInformationMessage('PASS')
109+
dispatch('coderoad.test_pass')
111110
},
112111
testFail(){
113-
vscode.window.showWarningMessage('FAIL')
112+
dispatch('coderoad.test_fail')
114113
},
115114
//@ts-ignore
116115
progressUpdate:assign({
@@ -166,6 +165,6 @@ export default {
166165
stepLoadCommits(context:CR.MachineContext):void{
167166
const{ data, position}=context
168167
const{ setup}=data.steps[position.stepId].actions
169-
git.gitLoadCommits(setup)
168+
git.gitLoadCommits(setup,dispatch)
170169
}
171-
}
170+
})

‎src/state/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import{interpret,Interpreter}from'xstate'
22
import*asCRfrom'typings'
3-
importmachinefrom'./machine'
4-
import*asvscodefrom'vscode'
3+
importcreateMachinefrom'./machine'
54

65
// machine interpreter
76
// https://xstate.js.org/docs/guides/interpretation.html
87

8+
interfaceProps{
9+
dispatch:CR.EditorDispatch
10+
}
11+
912
classStateMachine{
1013
privatemachineOptions={
1114
logger:console.log,
@@ -14,17 +17,18 @@ class StateMachine {
1417
execute:true
1518
}
1619
privateservice:Interpreter<CR.MachineContext,CR.MachineStateSchema,CR.MachineEvent>
17-
constructor(){
20+
constructor({ dispatch}:Props){
21+
constmachine=createMachine(dispatch)
1822
this.service=interpret(machine,this.machineOptions)
1923
// logging
2024
.onTransition(state=>{
2125
console.log('onTransition',state)
2226
if(state.changed){
2327
console.log('next state')
2428
console.log(state.value)
25-
vscode.commands.executeCommand('coderoad.send_state',{state:state.value,data:state.context})
29+
dispatch('coderoad.send_state',{state:state.value,data:state.context})
2630
}else{
27-
vscode.commands.executeCommand('coderoad.send_data',{data:state.context})
31+
dispatch('coderoad.send_data',{data:state.context})
2832
}
2933
})
3034
}

‎src/state/machine.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import{Machine}from'xstate'
22
import*asCRfrom'typings'
33

4-
importactionsfrom'./actions'
4+
importcreateActionsfrom'./actions'
55
importguardsfrom'./guards'
66
importinitialContextfrom'./context'
77

8-
exportconstmachine=Machine<
8+
exportconstmachine=(dispatch:CR.EditorDispatch)=>Machine<
99
CR.MachineContext,
1010
CR.MachineStateSchema,
1111
CR.MachineEvent
@@ -164,7 +164,7 @@ export const machine = Machine<
164164
}
165165
},
166166
{
167-
actions,
167+
actions:createActions(dispatch),
168168
guards,
169169
activities:{},
170170
},

‎typings/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,5 @@ export interface StateMachine {
172172
deactivate():void
173173
send(action:string|Action):void
174174
}
175+
176+
exporttypeEditorDispatch=(type:string,payload?:any)=>void

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp