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

Commit89c5289

Browse files
committed
heavy refactor of channel/send
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parentff6eb22 commit89c5289

File tree

10 files changed

+33
-71
lines changed

10 files changed

+33
-71
lines changed

‎src/actions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export { default as onTutorialConfigContinue } from './onTutorialConfigContinue'
44
export{defaultasonValidateSetup}from'./onValidateSetup'
55
export{defaultasonRunReset}from'./onRunReset'
66
export{defaultasonErrorPage}from'./onErrorPage'
7-
export{runTest,onTestPass}from'./onTest'
7+
export{runTest}from'./onTest'
88
export{onOpenLogs}from'./onOpenLogs'

‎src/actions/onStartup.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import*asvscodefrom'vscode'
2-
import*asTfrom'typings'
32
import*asTTfrom'typings/tutorial'
43
import*asEfrom'typings/error'
54
importContextfrom'../services/context/context'
5+
import{send}from'../commands'
66
import{WORKSPACE_ROOT,TUTORIAL_URL}from'../environment'
77
importfetchfrom'node-fetch'
8-
importloggerfrom'../services/logger'
98

10-
constonStartup=async(
11-
context:Context,
12-
workspaceState:vscode.Memento,
13-
send:(action:T.Action)=>Promise<void>,
14-
):Promise<void>=>{
9+
constonStartup=async(context:Context):Promise<void>=>{
1510
try{
1611
// check if a workspace is open, otherwise nothing works
1712
constnoActiveWorkspace=!WORKSPACE_ROOT.length

‎src/actions/onTest.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import*asgitfrom'../services/git'
21
import*asTfrom'typings'
32
import*asvscodefrom'vscode'
43
import{COMMANDS}from'../commands'
5-
importContextfrom'../services/context/context'
6-
7-
exportconstonTestPass=(action:T.Action,context:Context):void=>{
8-
context.position.set({ ...action.payload.position,complete:true})
9-
git.saveCommit('Save progress')
10-
}
114

125
exportconstrunTest=(action?:T.Action):void=>{
136
vscode.commands.executeCommand(COMMANDS.RUN_TEST,action?.payload)

‎src/actions/onTutorialConfigContinue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as T from 'typings'
33
import*asTTfrom'typings/tutorial'
44
importContextfrom'../services/context/context'
55
importtutorialConfigfrom'./utils/tutorialConfig'
6-
import{COMMANDS}from'../commands'
6+
import{COMMANDS,send}from'../commands'
77

8-
constonTutorialConfigContinue=async(action:T.Action,context:Context,send:T.Send):Promise<void>=>{
8+
constonTutorialConfigContinue=async(action:T.Action,context:Context):Promise<void>=>{
99
try{
1010
consttutorialContinue:TT.Tutorial|null=context.tutorial.get()
1111
if(!tutorialContinue){

‎src/actions/onTutorialConfigNew.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { onEvent } from '../services/telemetry'
77
import{version,compareVersions}from'../services/dependencies'
88
importContextfrom'../services/context/context'
99
importtutorialConfigfrom'./utils/tutorialConfig'
10+
import{send}from'../commands'
1011

11-
constonTutorialConfigNew=async(action:T.Action,context:Context,send:T.Send):Promise<void>=>{
12+
constonTutorialConfigNew=async(action:T.Action,context:Context):Promise<void>=>{
1213
try{
1314
constdata:TT.Tutorial=action.payload.tutorial
1415

‎src/actions/onValidateSetup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as T from 'typings'
22
import*asEfrom'typings/error'
33
import{version}from'../services/dependencies'
44
import{checkWorkspaceEmpty}from'../services/workspace'
5+
import{send}from'../commands'
56

6-
constonValidateSetup=async(send:T.Send):Promise<void>=>{
7+
constonValidateSetup=async():Promise<void>=>{
78
try{
89
// check workspace is selected
910
constisEmptyWorkspace=awaitcheckWorkspaceEmpty()

‎src/channel.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,36 @@ import * as hooks from './services/hooks'
99

1010
interfaceChannel{
1111
receive(action:T.Action):Promise<void>
12-
send(action:T.Action):Promise<void>
13-
}
14-
15-
interfaceChannelProps{
16-
postMessage:(action:T.Action)=>Thenable<boolean>
17-
workspaceState:vscode.Memento
1812
}
1913

2014
classChannelimplementsChannel{
21-
privatepostMessage:(action:T.Action)=>Thenable<boolean>
22-
privateworkspaceState:vscode.Memento
23-
privatecontext:Context
24-
constructor({ postMessage, workspaceState}:ChannelProps){
25-
// workspaceState used for local storage
26-
this.workspaceState=workspaceState
27-
this.postMessage=postMessage
15+
publiccontext:Context
16+
constructor(workspaceState:vscode.Memento){
17+
// workspaceState used for local storages
2818
this.context=newContext(workspaceState)
2919
}
3020

3121
// receive from webview
3222
publicreceive=async(action:T.Action):Promise<void>=>{
3323
// action may be an object.type or plain string
3424
constactionType:string=typeofaction==='string' ?action :action.type
35-
// const onError = (error: T.ErrorMessage) => this.send({ type: 'ERROR', payload: { error }})
3625

3726
logger(`EXT RECEIVED: "${actionType}"`)
3827

3928
switch(actionType){
4029
case'EDITOR_STARTUP':
41-
actions.onStartup(this.context,this.workspaceState,this.send)
30+
actions.onStartup(this.context)
4231
return
4332
// clear tutorial local storage
4433
// configure test runner, language, git
4534
case'EDITOR_TUTORIAL_CONFIG':
46-
actions.onTutorialConfigNew(action,this.context,this.send)
35+
actions.onTutorialConfigNew(action,this.context)
4736
return
4837
case'EDITOR_TUTORIAL_CONTINUE_CONFIG':
49-
actions.onTutorialConfigContinue(action,this.context,this.send)
38+
actions.onTutorialConfigContinue(action,this.context)
5039
return
5140
case'EDITOR_VALIDATE_SETUP':
52-
actions.onValidateSetup(this.send)
41+
actions.onValidateSetup()
5342
return
5443
case'EDITOR_REQUEST_WORKSPACE':
5544
openWorkspace()
@@ -95,26 +84,6 @@ class Channel implements Channel {
9584
return
9685
}
9786
}
98-
// send to webview
99-
publicsend=async(action:T.Action):Promise<void>=>{
100-
// load error page if error action is triggered
101-
actions.onErrorPage(action)
102-
// action may be an object.type or plain string
103-
constactionType:string=typeofaction==='string' ?action :action.type
104-
105-
logger(`EXT TO CLIENT: "${actionType}"`)
106-
107-
switch(actionType){
108-
case'TEST_PASS':
109-
actions.onTestPass(action,this.context)
110-
}
111-
112-
// send message
113-
constsentToClient=awaitthis.postMessage(action)
114-
if(!sentToClient){
115-
thrownewError(`Message post failure:${JSON.stringify(action)}`)
116-
}
117-
}
11887
}
11988

12089
exportdefaultChannel

‎src/commands.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import createTestRunner from './services/testRunner'
55
importcreateWebViewfrom'./services/webview'
66
import*ashooksfrom'./services/hooks'
77
importloggerfrom'./services/logger'
8+
import*asactionsfrom'./actions'
9+
importChannelfrom'./channel'
810

911
exportconstCOMMANDS={
1012
START:'coderoad.start',
@@ -26,14 +28,20 @@ let sendToClient = (action: T.Action): void => {
2628
// This makes it easier to pass the send
2729
// function throughout the codebase
2830
exportconstsend=(action:T.Action):void=>{
29-
sendToClient(action)
31+
// load error page if error action is triggered
32+
actions.onErrorPage(action)
33+
34+
logger(`EXT TO CLIENT: "${typeofaction==='string' ?action :action.type}"`)
35+
36+
if(action)sendToClient(action)
3037
}
3138

3239
exportconstcreateCommands=({ extensionPath, workspaceState}:CreateCommandProps):{[key:string]:any}=>{
3340
// React panel webview
3441
letwebview:any
3542
letcurrentPosition:T.Position
3643
lettestRunner:any
44+
constchannel=newChannel(workspaceState)
3745

3846
return{
3947
// initialize
@@ -42,9 +50,9 @@ export const createCommands = ({ extensionPath, workspaceState }: CreateCommandP
4250
webview.createOrShow()
4351
}else{
4452
// activate machine
45-
webview=createWebView({
53+
webview=awaitcreateWebView({
4654
extensionPath,
47-
workspaceState,
55+
channel,
4856
})
4957
// make send to client function exportable
5058
// as "send".
@@ -60,6 +68,7 @@ export const createCommands = ({ extensionPath, workspaceState }: CreateCommandP
6068
onSuccess:(position:T.Position)=>{
6169
logger('test pass position',position)
6270
// send test pass message back to client
71+
channel.context.position.set({ ...position,complete:true})
6372
send({type:'TEST_PASS',payload:{position:{ ...position,complete:true}}})
6473
},
6574
onFail:(position:T.Position,failSummary:T.TestFail):void=>{
@@ -83,6 +92,7 @@ export const createCommands = ({ extensionPath, workspaceState }: CreateCommandP
8392
[COMMANDS.SET_CURRENT_POSITION]:(position:T.Position)=>{
8493
// set from last setup stepAction
8594
currentPosition=position
95+
channel.context.position.set(position)
8696
},
8797
[COMMANDS.RUN_TEST]:({
8898
subtasks,

‎src/services/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const onError = async (error: Error): Promise<void> => {
4848
}
4949

5050
exportconstonStepComplete=async({ levelId, stepId}:{levelId:string;stepId:string}):Promise<void>=>{
51+
git.saveCommit('Save progress')
5152
logger(`ON STEP COMPLETE:${JSON.stringify({ levelId, stepId})}`)
5253
}
5354

‎src/services/webview/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import*asTfrom'typings'
22
import*aspathfrom'path'
3-
import{Action}from'typings'
43
import*asvscodefrom'vscode'
5-
importChannelfrom'../../channel'
64
importrenderfrom'./render'
75

86
interfaceReactWebViewProps{
97
extensionPath:string
10-
workspaceState:vscode.Memento
8+
channel:any
119
}
1210

1311
interfaceOutput{
@@ -19,7 +17,7 @@ interface Output {
1917

2018
conststate={loaded:false}
2119

22-
constcreateReactWebView=({ extensionPath,workspaceState}:ReactWebViewProps):Output=>{
20+
constcreateReactWebView=({ extensionPath,channel}:ReactWebViewProps):Output=>{
2321
// TODO add disposables
2422
constdisposables:vscode.Disposable[]=[]
2523

@@ -53,15 +51,9 @@ const createReactWebView = ({ extensionPath, workspaceState }: ReactWebViewProps
5351
disposables,
5452
)
5553

56-
constchannel=newChannel({
57-
workspaceState,
58-
postMessage:(action:Action):Thenable<boolean>=>{
59-
returnpanel.webview.postMessage(action)
60-
},
61-
})
6254
// Handle messages from the webview
6355
constreceive=channel.receive
64-
constsend=channel.send
56+
constsend=(action:T.Action)=>panel.webview.postMessage(action)
6557

6658
panel.webview.onDidReceiveMessage(receive,null,disposables)
6759

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp