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

Commite49165c

Browse files
committed
manage storage in editor Channel
1 parent90e5e97 commite49165c

File tree

6 files changed

+88
-49
lines changed

6 files changed

+88
-49
lines changed

‎src/Channel.ts

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import*asCRfrom'typings'
22
import*asvscodefrom'vscode'
33

4+
importStoragefrom'./services/storage'
45
importtutorialConfigfrom'./actions/tutorialConfig'
56
importsetupActionsfrom'./actions/setupActions'
67
importsolutionActionsfrom'./actions/solutionActions'
@@ -12,18 +13,23 @@ interface Channel {
1213

1314
interfaceChannelProps{
1415
postMessage:(action:CR.Action)=>Thenable<boolean>
15-
storage:{
16-
tutorial:any
17-
stepProgress:any
18-
}
16+
workspaceState:vscode.Memento
1917
}
2018

2119
classChannelimplementsChannel{
2220
privatepostMessage:(action:CR.Action)=>Thenable<boolean>
23-
privatestorage:any
24-
constructor({postMessage, storage}:ChannelProps){
21+
privatecurrentTutorial:Storage<{id:string|null,version:string|null}>
22+
privatestepProgress:Storage<CR.StepProgress>|undefined
23+
privateworkspaceState:vscode.Memento
24+
constructor({postMessage, workspaceState}:ChannelProps){
2525
this.postMessage=postMessage
26-
this.storage=storage
26+
this.workspaceState=workspaceState
27+
28+
this.currentTutorial=newStorage<{id:string|null,version:string|null}>({
29+
key:'coderoad:currentTutorial',
30+
storage:workspaceState,
31+
defaultValue:{id:null,version:null}
32+
})
2733
}
2834

2935
// receive from webview
@@ -33,14 +39,19 @@ class Channel implements Channel {
3339
switch(actionType){
3440
// continue from tutorial from local storage
3541
case'TUTORIAL_LOAD_STORED':
36-
consttutorial=awaitthis.storage.tutorial.get()
37-
conststepProgress=awaitthis.storage.stepProgress.get()
38-
39-
console.log('looking at stored')
40-
console.log(JSON.stringify(tutorial))
41-
console.log(JSON.stringify(stepProgress))
42+
consttutorial=awaitthis.currentTutorial.get()
4243

43-
if(tutorial&&tutorial.id){
44+
if(tutorial&&tutorial.id&&tutorial.version){
45+
this.stepProgress=newStorage<CR.StepProgress>({
46+
key:`coderoad:stepProgress:${tutorial.id}:${tutorial.version}`,
47+
storage:this.workspaceState,
48+
defaultValue:{}
49+
})
50+
conststepProgress=awaitthis.stepProgress.get()
51+
console.log('looking at stored')
52+
console.log(JSON.stringify(tutorial))
53+
console.log(JSON.stringify(stepProgress))
54+
// communicate to client the tutorial & stepProgress state
4455
this.send({type:'CONTINUE_TUTORIAL',payload:{tutorial, stepProgress}})
4556
}else{
4657
this.send({type:'NEW_TUTORIAL'})
@@ -49,13 +60,17 @@ class Channel implements Channel {
4960
return
5061
// clear tutorial local storage
5162
case'TUTORIAL_CLEAR':
52-
this.storage.tutorial.set(null)
53-
this.storage.stepProgress.set({})
63+
this.currentTutorial.set({id:null,version:null})
64+
65+
// reset tutorial progress on clear
66+
if(this.stepProgress){
67+
this.stepProgress.set({})
68+
}
5469
return
5570
// configure test runner, language, git
5671
case'TUTORIAL_CONFIG':
5772
tutorialConfig(action.payload)
58-
this.storage.tutorial.set(action.payload)
73+
this.currentTutorial.set(action.payload)
5974
return
6075
// run unit tests on step
6176
case'TEST_RUN':
@@ -78,6 +93,16 @@ class Channel implements Channel {
7893
}
7994
// send to webview
8095
publicsend=async(action:CR.Action)=>{
96+
97+
switch(action.type){
98+
case'TEST_PASS':
99+
// update local storage stepProgress
100+
// stepProgress.update({
101+
// [action.payload.stepId]: true
102+
// })
103+
return
104+
}
105+
81106
constsuccess=awaitthis.postMessage(action)
82107
if(!success){
83108
thrownewError(`Message post failure:${JSON.stringify(action)}`)

‎src/editor/ReactWebView.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ const getNonce = (): string => {
1414

1515
interfaceReactWebViewProps{
1616
extensionPath:string
17-
storage:{
18-
tutorial:any
19-
stepProgress:any
20-
}
17+
workspaceState:vscode.Memento
2118
}
2219

2320

@@ -32,7 +29,7 @@ class ReactWebView {
3229
privatedisposables:vscode.Disposable[]=[]
3330
privatechannel:Channel
3431

35-
publicconstructor({extensionPath,storage}:ReactWebViewProps){
32+
publicconstructor({extensionPath,workspaceState}:ReactWebViewProps){
3633
this.extensionPath=extensionPath
3734

3835
// Create and show a new webview panel
@@ -45,8 +42,9 @@ class ReactWebView {
4542
// This happens when the user closes the panel or when the panel is closed programmatically
4643
this.panel.onDidDispose(this.dispose,this,this.disposables)
4744

45+
// channel connects webview to the editor
4846
this.channel=newChannel({
49-
storage,
47+
workspaceState,
5048
postMessage:(action:Action):Thenable<boolean>=>{
5149
returnthis.panel.webview.postMessage(action)
5250
}

‎src/editor/commands.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import*asGfrom'typings/graphql'
21
import*asvscodefrom'vscode'
3-
importStoragefrom'../services/storage'
2+
import{EditorStorage}from'typings'
43
importReactWebViewfrom'./ReactWebView'
54
importrunTestfrom'../actions/runTest'
65
import{isEmptyWorkspace}from'./workspace'
@@ -13,25 +12,15 @@ const COMMANDS = {
1312
}
1413

1514
interfaceCreateCommandProps{
16-
vscodeExt:vscode.ExtensionContext
15+
extensionPath:string
16+
workspaceState:vscode.Memento
1717
}
1818

19-
exportconstcreateCommands=({vscodeExt}:CreateCommandProps)=>{
19+
exportconstcreateCommands=({extensionPath, workspaceState}:CreateCommandProps)=>{
2020
// React panel webview
2121
letwebview:any
2222
letcurrentStepId=''
2323

24-
consttutorial=newStorage<G.Tutorial|null>({
25-
key:'coderoad:tutorial',
26-
storage:vscodeExt.workspaceState
27-
})
28-
29-
conststepProgress=newStorage<{[stepId:string]:boolean}>({
30-
key:'coderoad:progress',
31-
storage:vscodeExt.workspaceState
32-
})
33-
34-
3524
return{
3625
// initialize
3726
[COMMANDS.START]:async()=>{
@@ -53,11 +42,8 @@ export const createCommands = ({vscodeExt}: CreateCommandProps) => {
5342

5443
// activate machine
5544
webview=newReactWebView({
56-
extensionPath:vscodeExt.extensionPath,
57-
storage:{
58-
tutorial,
59-
stepProgress
60-
}
45+
extensionPath,
46+
workspaceState,
6147
})
6248
},
6349
// open React webview
@@ -86,10 +72,6 @@ export const createCommands = ({vscodeExt}: CreateCommandProps) => {
8672
console.log('COMMAND TEST_PASS')
8773
// send test pass message back to client
8874
webview.send({type:'TEST_PASS', payload})
89-
// update local storage stepProgress
90-
stepProgress.update({
91-
[payload.stepId]:true
92-
})
9375
vscode.window.showInformationMessage('PASS')
9476
},
9577
onFail:()=>{

‎src/editor/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,24 @@ class Editor {
3131
}
3232

3333
privateactivateCommands=():void=>{
34+
// NOTE: local storage must be bound to the vscodeExt.workspaceState
35+
36+
// store current tutorial id & version
37+
38+
39+
// store step progress for current tutorial
40+
// const stepProgress = new Storage<{[stepId: string]: boolean}>({
41+
// key: 'coderoad:progress',
42+
// storage: this.vscodeExt.workspaceState,
43+
// defaultValue: {},
44+
// })
45+
3446
constcommands=createCommands({
35-
vscodeExt:this.vscodeExt,
47+
extensionPath:this.vscodeExt.extensionPath,
48+
workspaceState:this.vscodeExt.workspaceState,
3649
})
50+
51+
// register commands
3752
for(constcmdincommands){
3853
constcommand:vscode.Disposable=vscode.commands.registerCommand(cmd,commands[cmd])
3954
this.vscodeExt.subscriptions.push(command)

‎src/services/storage/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ import * as vscode from 'vscode'
99
classStorage<T>{
1010
privatekey:string
1111
privatestorage:vscode.Memento
12-
constructor({key, storage}:{key:string,storage:vscode.Memento}){
12+
constructor({key, storage, defaultValue}:{key:string,storage:vscode.Memento,defaultValue?:T}){
1313
this.storage=storage
1414
this.key=key
15+
// set default if none exists
16+
if(!defaultValue){
17+
return
18+
}
19+
this.get().then((value:T|null)=>{
20+
if(!value){
21+
this.set(defaultValue)
22+
}
23+
})
1524
}
1625
publicget=async():Promise<T|null>=>{
1726
constvalue:string|undefined=awaitthis.storage.get(this.key)

‎typings/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import{send}from'xstate'
2+
importStoragefrom'../src/services/storage'
23
import*asGfrom'./graphql'
34

45
exportinterfaceTutorialLevel{
@@ -98,6 +99,10 @@ export interface Progress {
9899
complete:boolean
99100
}
100101

102+
exportinterfaceStepProgress{
103+
[stepId:string]:boolean
104+
}
105+
101106
// current tutorial position
102107
exportinterfacePosition{
103108
levelId:string
@@ -176,3 +181,8 @@ interface MessageState {
176181

177182
// todo: type each string param and payload
178183
exporttypeEditorDispatch=(type:string,payload?:MessageData|MessageState|any)=>void
184+
185+
exportinterfaceEditorStorage{
186+
currentTutorial:Storage<{id:string|null,version:string|null}>
187+
stepProgress:Storage<StepProgress>
188+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp