|
| 1 | +import*asCRfrom'typings' |
| 2 | +import*asGfrom'typings/graphql' |
| 3 | +import*asvscodefrom'vscode' |
| 4 | + |
| 5 | +importContextfrom'./context' |
| 6 | +importtutorialConfigfrom'../actions/tutorialConfig' |
| 7 | +importsetupActionsfrom'../actions/setupActions' |
| 8 | +importsolutionActionsfrom'../actions/solutionActions' |
| 9 | +importsaveCommitfrom'../actions/saveCommit' |
| 10 | + |
| 11 | + |
| 12 | +interfaceChannel{ |
| 13 | +receive(action:CR.Action):Promise<void> |
| 14 | +send(action:CR.Action):Promise<void> |
| 15 | +} |
| 16 | + |
| 17 | +interfaceChannelProps{ |
| 18 | +postMessage:(action:CR.Action)=>Thenable<boolean> |
| 19 | +workspaceState:vscode.Memento |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +classChannelimplementsChannel{ |
| 24 | +privatepostMessage:(action:CR.Action)=>Thenable<boolean> |
| 25 | +privateworkspaceState:vscode.Memento |
| 26 | +privatecontext:Context |
| 27 | +constructor({postMessage, workspaceState}:ChannelProps){ |
| 28 | +// workspaceState used for local storage |
| 29 | +this.workspaceState=workspaceState |
| 30 | +this.postMessage=postMessage |
| 31 | +this.context=newContext(workspaceState) |
| 32 | +} |
| 33 | + |
| 34 | +// receive from webview |
| 35 | +publicreceive=async(action:CR.Action)=>{ |
| 36 | +constactionType:string=typeofaction==='string' ?action :action.type |
| 37 | +console.log('RECEIVED:',actionType) |
| 38 | +switch(actionType){ |
| 39 | +// continue from tutorial from local storage |
| 40 | +case'EDITOR_TUTORIAL_LOAD': |
| 41 | +consttutorial:G.Tutorial|null=this.context.tutorial.get() |
| 42 | + |
| 43 | +// new tutorial |
| 44 | +if(!tutorial||!tutorial.id||!tutorial.version){ |
| 45 | +this.send({type:'NEW_TUTORIAL'}) |
| 46 | +return |
| 47 | +} |
| 48 | + |
| 49 | +// set tutorial |
| 50 | +constprogress:CR.Progress=awaitthis.context.progress.setTutorial(this.workspaceState,tutorial) |
| 51 | + |
| 52 | +console.log('looking at stored') |
| 53 | +console.log(JSON.stringify(tutorial)) |
| 54 | +console.log(JSON.stringify(progress)) |
| 55 | + |
| 56 | +if(progress.complete){ |
| 57 | +// tutorial is already complete |
| 58 | +this.send({type:'NEW_TUTORIAL'}) |
| 59 | +return |
| 60 | +} |
| 61 | + |
| 62 | +// communicate to client the tutorial & stepProgress state |
| 63 | +this.send({type:'CONTINUE_TUTORIAL',payload:{tutorial, progress, position}}) |
| 64 | + |
| 65 | +return |
| 66 | +// clear tutorial local storage |
| 67 | +case'TUTORIAL_CLEAR': |
| 68 | +// clear current progress/position/tutorial |
| 69 | +this.context=newContext(this.workspaceState) |
| 70 | +return |
| 71 | +// configure test runner, language, git |
| 72 | +case'EDITOR_TUTORIAL_CONFIG': |
| 73 | +consttutorialData=action.payload.tutorial |
| 74 | +console.log('tutorialConfig',JSON.stringify(tutorialData)) |
| 75 | +this.context.setTutorial(this.workspaceState,tutorialData) |
| 76 | +tutorialConfig(tutorialData) |
| 77 | +return |
| 78 | +// run unit tests on step |
| 79 | +case'TEST_RUN': |
| 80 | +vscode.commands.executeCommand('coderoad.run_test',action.payload) |
| 81 | +return |
| 82 | +// load step actions (git commits, commands, open files) |
| 83 | +case'SETUP_ACTIONS': |
| 84 | +vscode.commands.executeCommand('coderoad.set_current_step',action.payload) |
| 85 | +setupActions(action.payload) |
| 86 | +return |
| 87 | +// load solution step actions (git commits, commands, open files) |
| 88 | +case'SOLUTION_ACTIONS': |
| 89 | +solutionActions(action.payload) |
| 90 | +return |
| 91 | + |
| 92 | +default: |
| 93 | +console.log(`No match for action type:${actionType}`) |
| 94 | +return |
| 95 | +} |
| 96 | +} |
| 97 | +// send to webview |
| 98 | +publicsend=async(action:CR.Action)=>{ |
| 99 | + |
| 100 | +switch(action.type){ |
| 101 | +case'TEST_PASS': |
| 102 | +// update local storage stepProgress |
| 103 | +this.context.progress.setStepComplete(action.payload.stepId) |
| 104 | +saveCommit() |
| 105 | +} |
| 106 | + |
| 107 | +constsuccess=awaitthis.postMessage(action) |
| 108 | +if(!success){ |
| 109 | +thrownewError(`Message post failure:${JSON.stringify(action)}`) |
| 110 | +} |
| 111 | +} |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +exportdefaultChannel |
| 116 | + |