Skip to content

separate vscode from services #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/editor/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const COMMANDS = {
RECEIVE_ACTION: 'coderoad.receive_action',
OPEN_FILE: 'coderoad.open_file',
RUN_TEST: 'coderoad.run_test',
TEST_PASS: 'coderoad.test_pass',
TEST_FAIL: 'coderoad.test_fail',
SET_LAYOUT: 'coderoad.set_layout',
}

interface CreateCommandProps {
Expand All @@ -41,8 +44,12 @@ export const createCommands = ({ context, machine, storage, git, position }: Cre
machine.activate()
},
// open React webview
[COMMANDS.OPEN_WEBVIEW]: (column: number = vscode.ViewColumn.One) => {
[COMMANDS.OPEN_WEBVIEW]: (column: number = vscode.ViewColumn.Two) => {
// setup 1x1 horizontal layout
vscode.commands.executeCommand('vscode.setEditorLayout', { orientation: 0, groups: [{ groups: [{}], size: 0.6 }, { groups: [{}], size: 0.4 }] })
webview.createOrShow(column);
// NOTE: createOrShow and layout command cannot be async
// this creates an async issue where the webview cannot detect when it has been initialized
setTimeout(() => {
machine.send('WEBVIEW_INITIALIZED')
}, 2000)
Expand Down Expand Up @@ -112,5 +119,15 @@ export const createCommands = ({ context, machine, storage, git, position }: Cre
onSuccess: () => machine.send('TEST_PASS'),
onFail: () => machine.send('TEST_FAIL')
})
}
},
[COMMANDS.TEST_PASS]: () => {
vscode.window.showInformationMessage('PASS')
},
[COMMANDS.TEST_FAIL]: () => {
vscode.window.showWarningMessage('FAIL')
},
[COMMANDS.SET_LAYOUT]: () => {
console.log('setLayout')
vscode.commands.executeCommand('vscode.setEditorLayout', { orientation: 0, groups: [{ groups: [{}], size: 0.6 }, { groups: [{}], size: 0.4 }] })
},
})
4 changes: 2 additions & 2 deletions src/editor/commands/loadSolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as CR from 'typings'
import * as storage from '../../services/storage'
import { gitLoadCommits, gitClear } from '../../services/git'

export default async function loadSolution(): Promise<void> {
export default async function loadSolution(dispatch: CR.EditorDispatch): Promise<void> {
const [position, tutorial]: [CR.Position, CR.Tutorial | undefined] = await Promise.all([
storage.getPosition(),
storage.getTutorial(),
Expand All @@ -17,5 +17,5 @@ export default async function loadSolution(): Promise<void> {
const { solution } = tutorial.data.steps[position.stepId].actions

await gitClear()
await gitLoadCommits(solution)
await gitLoadCommits(solution, dispatch)
}
2 changes: 1 addition & 1 deletion src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Editor {
}

// execute vscode command
public dispatch = (type: string, payload: any) => {
public dispatch = (type: string, payload?: any) => {
vscode.commands.executeCommand(type, payload)
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import * as vscode from 'vscode'
import { setWorkspaceRoot } from './services/node'
import StateMachine from './state'
import Editor from './editor'


// state machine that governs application logic
export const machine = new StateMachine()
export const machine = new StateMachine({ dispatch: vscode.commands.executeCommand })

// vscode editor
export const editor = new Editor({
machine,
setWorkspaceRoot,
})

// activate run on vscode extension initialization
export const activate = editor.activate

// deactive run on vscode extension shut down
export const deactivate = editor.deactivate
5 changes: 2 additions & 3 deletions src/services/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as vscode from 'vscode'
import * as CR from 'typings'
import { exec, exists } from '../node'

Expand All @@ -9,7 +8,7 @@ const gitOrigin = 'coderoad'
MULTIPLE git cherry-pick %COMMIT_START%..%COMMIT_END%
if shell, run shell
*/
export async function gitLoadCommits(actions: CR.TutorialAction): Promise<void> {
export async function gitLoadCommits(actions: CR.TutorialAction, dispatch: CR.EditorDispatch): Promise<void> {
const { commits, commands, files } = actions

console.log('commits to load', commits)
Expand Down Expand Up @@ -41,7 +40,7 @@ export async function gitLoadCommits(actions: CR.TutorialAction): Promise<void>

if (files) {
for (const filePath of files) {
vscode.commands.executeCommand('coderoad.open_file', filePath)
dispatch('coderoad.open_file', filePath)
}
}
}
Expand Down
53 changes: 0 additions & 53 deletions src/services/testResult.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/services/tutorialSetup.ts

This file was deleted.

21 changes: 10 additions & 11 deletions src/state/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { assign } from 'xstate'
import { machine } from '../../extension'
import api from '../../services/api'
import * as CR from 'typings'
import * as vscode from 'vscode'
import * as storage from '../../services/storage'
import * as git from '../../services/git'

Expand All @@ -15,10 +14,10 @@ let currentProgress: CR.Progress = {
complete: false,
}

export default {
export default (dispatch: CR.EditorDispatch) => ({
createWebview() {
console.log('execute coderoad.open_webview')
vscode.commands.executeCommand('coderoad.open_webview')
dispatch('coderoad.open_webview')
},
async newOrContinue() {
// verify that the user has a tutorial & progress
Expand All @@ -45,11 +44,11 @@ export default {
currentTutorial = tutorial
console.log('api')
console.log(tutorial)
vscode.commands.executeCommand('coderoad.tutorial_launch', tutorial)
dispatch('coderoad.tutorial_launch', tutorial)
},
tutorialSetup() {
vscode.commands.executeCommand('coderoad.tutorial_setup', currentTutorial)
vscode.commands.executeCommand('coderoad.open_webview', vscode.ViewColumn.Two)
dispatch('coderoad.tutorial_setup', currentTutorial)
dispatch('coderoad.open_webview', 2)
},
initializeNewTutorial: assign({
position: (context: any): CR.Position => {
Expand Down Expand Up @@ -104,13 +103,13 @@ export default {
}
}),
testStart() {
vscode.commands.executeCommand('coderoad.run_test')
dispatch('coderoad.run_test')
},
testPass() {
vscode.window.showInformationMessage('PASS')
dispatch('coderoad.test_pass')
},
testFail() {
vscode.window.showWarningMessage('FAIL')
dispatch('coderoad.test_fail')
},
// @ts-ignore
progressUpdate: assign({
Expand Down Expand Up @@ -166,6 +165,6 @@ export default {
stepLoadCommits(context: CR.MachineContext): void {
const { data, position } = context
const { setup } = data.steps[position.stepId].actions
git.gitLoadCommits(setup)
git.gitLoadCommits(setup, dispatch)
}
}
})
14 changes: 9 additions & 5 deletions src/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { interpret, Interpreter } from 'xstate'
import * as CR from 'typings'
import machine from './machine'
import * as vscode from 'vscode'
import createMachine from './machine'

// machine interpreter
// https://xstate.js.org/docs/guides/interpretation.html

interface Props {
dispatch: CR.EditorDispatch
}

class StateMachine {
private machineOptions = {
logger: console.log,
Expand All @@ -14,17 +17,18 @@ class StateMachine {
execute: true
}
private service: Interpreter<CR.MachineContext, CR.MachineStateSchema, CR.MachineEvent>
constructor() {
constructor({ dispatch }: Props) {
const machine = createMachine(dispatch)
this.service = interpret(machine, this.machineOptions)
// logging
.onTransition(state => {
console.log('onTransition', state)
if (state.changed) {
console.log('next state')
console.log(state.value)
vscode.commands.executeCommand('coderoad.send_state', { state: state.value, data: state.context })
dispatch('coderoad.send_state', { state: state.value, data: state.context })
} else {
vscode.commands.executeCommand('coderoad.send_data', { data: state.context })
dispatch('coderoad.send_data', { data: state.context })
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/state/machine.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Machine } from 'xstate'
import * as CR from 'typings'

import actions from './actions'
import createActions from './actions'
import guards from './guards'
import initialContext from './context'

export const machine = Machine<
export const machine = (dispatch: CR.EditorDispatch) => Machine<
CR.MachineContext,
CR.MachineStateSchema,
CR.MachineEvent
Expand Down Expand Up @@ -164,7 +164,7 @@ export const machine = Machine<
}
},
{
actions,
actions: createActions(dispatch),
guards,
activities: {},
},
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,5 @@ export interface StateMachine {
deactivate(): void
send(action: string | Action): void
}

export type EditorDispatch = (type: string, payload?: any) => void