1
1
import * as CR from 'typings'
2
2
import * as vscode from 'vscode'
3
3
4
+ import Storage from './services/storage'
4
5
import tutorialConfig from './actions/tutorialConfig'
5
6
import setupActions from './actions/setupActions'
6
7
import solutionActions from './actions/solutionActions'
@@ -12,18 +13,23 @@ interface Channel {
12
13
13
14
interface ChannelProps {
14
15
postMessage : ( action : CR . Action ) => Thenable < boolean >
15
- storage : {
16
- tutorial : any
17
- stepProgress : any
18
- }
16
+ workspaceState : vscode . Memento
19
17
}
20
18
21
19
class Channel implements Channel {
22
20
private postMessage : ( action : CR . Action ) => Thenable < boolean >
23
- private storage : any
24
- constructor ( { postMessage, storage} : ChannelProps ) {
21
+ private currentTutorial : Storage < { id : string | null , version : string | null } >
22
+ private stepProgress : Storage < CR . StepProgress > | undefined
23
+ private workspaceState : vscode . Memento
24
+ constructor ( { postMessage, workspaceState} : ChannelProps ) {
25
25
this . postMessage = postMessage
26
- this . storage = storage
26
+ this . workspaceState = workspaceState
27
+
28
+ this . currentTutorial = new Storage < { id : string | null , version : string | null } > ( {
29
+ key : 'coderoad:currentTutorial' ,
30
+ storage : workspaceState ,
31
+ defaultValue : { id : null , version : null }
32
+ } )
27
33
}
28
34
29
35
// receive from webview
@@ -33,14 +39,19 @@ class Channel implements Channel {
33
39
switch ( actionType ) {
34
40
// continue from tutorial from local storage
35
41
case 'TUTORIAL_LOAD_STORED' :
36
- const tutorial = await this . storage . tutorial . get ( )
37
- const stepProgress = await this . storage . stepProgress . get ( )
38
-
39
- console . log ( 'looking at stored' )
40
- console . log ( JSON . stringify ( tutorial ) )
41
- console . log ( JSON . stringify ( stepProgress ) )
42
+ const tutorial = await this . currentTutorial . get ( )
42
43
43
- if ( tutorial && tutorial . id ) {
44
+ if ( tutorial && tutorial . id && tutorial . version ) {
45
+ this . stepProgress = new Storage < CR . StepProgress > ( {
46
+ key : `coderoad:stepProgress:${ tutorial . id } :${ tutorial . version } ` ,
47
+ storage : this . workspaceState ,
48
+ defaultValue : { }
49
+ } )
50
+ const stepProgress = await this . 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
44
55
this . send ( { type : 'CONTINUE_TUTORIAL' , payload : { tutorial, stepProgress} } )
45
56
} else {
46
57
this . send ( { type : 'NEW_TUTORIAL' } )
@@ -49,13 +60,17 @@ class Channel implements Channel {
49
60
return
50
61
// clear tutorial local storage
51
62
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
+ }
54
69
return
55
70
// configure test runner, language, git
56
71
case 'TUTORIAL_CONFIG' :
57
72
tutorialConfig ( action . payload )
58
- this . storage . tutorial . set ( action . payload )
73
+ this . currentTutorial . set ( action . payload )
59
74
return
60
75
// run unit tests on step
61
76
case 'TEST_RUN' :
@@ -78,6 +93,16 @@ class Channel implements Channel {
78
93
}
79
94
// send to webview
80
95
public send = 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
+
81
106
const success = await this . postMessage ( action )
82
107
if ( ! success ) {
83
108
throw new Error ( `Message post failure: ${ JSON . stringify ( action ) } ` )
0 commit comments