11import * as CR from 'typings'
22import * as vscode from 'vscode'
33
4+ import Storage from './services/storage'
45import tutorialConfig from './actions/tutorialConfig'
56import setupActions from './actions/setupActions'
67import solutionActions from './actions/solutionActions'
@@ -12,18 +13,23 @@ interface Channel {
1213
1314interface ChannelProps {
1415postMessage :( action :CR . Action ) => Thenable < boolean >
15- storage :{
16- tutorial :any
17- stepProgress :any
18- }
16+ workspaceState :vscode . Memento
1917}
2018
2119class Channel implements Channel {
2220private 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 ) {
2525this . 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+ } )
2733}
2834
2935// receive from webview
@@ -33,14 +39,19 @@ class Channel implements Channel {
3339switch ( actionType ) {
3440// continue from tutorial from local storage
3541case '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 ( )
4243
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
4455this . send ( { type :'CONTINUE_TUTORIAL' , payload :{ tutorial, stepProgress} } )
4556} else {
4657this . send ( { type :'NEW_TUTORIAL' } )
@@ -49,13 +60,17 @@ class Channel implements Channel {
4960return
5061// clear tutorial local storage
5162case '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+ }
5469return
5570// configure test runner, language, git
5671case 'TUTORIAL_CONFIG' :
5772tutorialConfig ( action . payload )
58- this . storage . tutorial . set ( action . payload )
73+ this . currentTutorial . set ( action . payload )
5974return
6075// run unit tests on step
6176case 'TEST_RUN' :
@@ -78,6 +93,16 @@ class Channel implements Channel {
7893}
7994// send to webview
8095public 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+
81106const success = await this . postMessage ( action )
82107if ( ! success ) {
83108throw new Error ( `Message post failure:${ JSON . stringify ( action ) } ` )