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

Commitaa1a2a5

Browse files
committed
cleanup react webview channel
1 parent87d8246 commitaa1a2a5

File tree

4 files changed

+53
-47
lines changed

4 files changed

+53
-47
lines changed

‎src/editor/Channel.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import*asCRfrom'typings'
2+
import*asvscodefrom'vscode'
3+
4+
interfaceChannel{
5+
receive(action:CR.Action):void
6+
send(action:CR.Action):void
7+
}
8+
9+
classChannelimplementsChannel{
10+
privatepostMessage:(action:CR.Action)=>Thenable<boolean>
11+
constructor(webview:vscode.Webview){
12+
this.postMessage=webview.postMessage
13+
}
14+
15+
// receive from webview
16+
publicreceive(action:CR.Action){
17+
constactionType:string=typeofaction==='string' ?action :action.type
18+
console.log('RECEIVED',actionType)
19+
switch(actionType){
20+
case'TEST_RUN':
21+
return
22+
case'TUTORIAL_CONFIG':
23+
return
24+
case'STEP_ACTIONS':
25+
return
26+
// add other cases
27+
default:
28+
return
29+
}
30+
}
31+
// send to webview
32+
publicasyncsend(action:CR.Action){
33+
constsuccess=awaitthis.postMessage(action)
34+
if(!success){
35+
thrownewError(`Message post failure:${JSON.stringify(action)}`)
36+
}
37+
}
38+
39+
}
40+
41+
exportdefaultChannel
42+

‎src/editor/ReactWebView.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import*aspathfrom'path'
2-
import*asCRfrom'typings'
32
import*asvscodefrom'vscode'
4-
import{tutorialModel}from'../extension'
3+
importChannel,*aschannelfrom'./channel'
54

65
constgetNonce=():string=>{
76
lettext=''
@@ -17,9 +16,12 @@ const getNonce = (): string => {
1716
classReactWebView{
1817
//@ts-ignore
1918
publicloaded:boolean
19+
20+
publicsend:Channel['send']
2021
privatepanel:vscode.WebviewPanel
2122
privateextensionPath:string
2223
privatedisposables:vscode.Disposable[]=[]
24+
privatechannel:Channel
2325

2426
publicconstructor(extensionPath:string){
2527
this.extensionPath=extensionPath
@@ -34,24 +36,12 @@ class ReactWebView {
3436
// This happens when the user closes the panel or when the panel is closed programatically
3537
this.panel.onDidDispose(this.dispose,this,this.disposables)
3638

39+
this.channel=newChannel(this.panel.webview)
3740
// Handle messages from the webview
38-
constonReceive=(action:string|CR.Action)=>{
39-
constactionType:string=typeofaction==='string' ?action :action.type
40-
switch(actionType){
41-
case'TUTORIAL_START':
42-
if(typeofaction==='string'||!action.payload||!action.payload.id){
43-
thrownewError('No tutorial id on tutorial start action')
44-
}
45-
tutorialModel.launch(action.payload.id)
46-
break
47-
// add other cases
48-
default:
49-
// send to state machine
50-
console.log('onReceive',action)
51-
vscode.commands.executeCommand('coderoad.receive_machine_action',action)
52-
}
53-
}
54-
this.panel.webview.onDidReceiveMessage(onReceive,null,this.disposables)
41+
constreceive=this.channel.receive
42+
this.panel.webview.onDidReceiveMessage(receive,null,this.disposables)
43+
this.send=this.channel.send
44+
5545

5646
// update panel on changes
5747
constupdateWindows=()=>{
@@ -88,15 +78,6 @@ class ReactWebView {
8878
}
8979
}
9080

91-
publicasyncpostMessage(action:CR.Action):Promise<void>{
92-
// Send a message to the webview webview.
93-
// You can send any JSON serializable data.
94-
constsuccess=awaitthis.panel.webview.postMessage(action)
95-
if(!success){
96-
thrownewError(`Message post failure:${JSON.stringify(action)}`)
97-
}
98-
}
99-
10081
privateasyncdispose():Promise<void>{
10182
// Clean up our resources
10283
this.loaded=false

‎src/editor/commands/index.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ const COMMANDS = {
1010
START:'coderoad.start',
1111
TEST_RUNNER_SETUP:'coderoad.test_runner_setup',
1212
OPEN_WEBVIEW:'coderoad.open_webview',
13-
SEND_STATE:'coderoad.send_state',
14-
SEND_DATA:'coderoad.send_data',
15-
RECEIVE_MACHINE_ACTION:'coderoad.receive_machine_action',
1613
OPEN_FILE:'coderoad.open_file',
1714
RUN_TEST:'coderoad.run_test',
1815
TEST_PASS:'coderoad.test_pass',
@@ -101,18 +98,6 @@ export const createCommands = ({vscodeExt}: CreateCommandProps) => {
10198
console.log(`Failed to open file${relativeFilePath}`,error)
10299
}
103100
},
104-
// send messages to webview
105-
[COMMANDS.SEND_STATE]:(payload:{data:any;state:any})=>{
106-
webview.postMessage({type:'SET_STATE', payload})
107-
},
108-
[COMMANDS.SEND_DATA]:(payload:{data:any})=>{
109-
webview.postMessage({type:'SET_DATA', payload})
110-
},
111-
[COMMANDS.RECEIVE_MACHINE_ACTION]:(action:string|CR.Action)=>{
112-
// send received actions from web-app into state machine
113-
console.log('receive action',action)
114-
// machine.send(action)
115-
},
116101
[COMMANDS.RUN_TEST]:()=>{
117102
runTest({
118103
onSuccess:()=>{

‎web-app/src/services/state/machine.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
5858
initial:'Initialize',
5959
states:{
6060
Initialize:{
61+
onEntry:['initializeTutorial'],
6162
after:{
6263
0:'Summary',
6364
},
@@ -104,10 +105,7 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
104105
states:{
105106
Normal:{
106107
on:{
107-
TEST_RUN:{
108-
target:'TestRunning',
109-
actions:'testStart',
110-
},
108+
TEST_RUN:'TestRunning',
111109
STEP_SOLUTION_LOAD:{
112110
actions:['callSolution'],
113111
},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp