Skip to content

Refactor editor #396

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 8 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor onEditorStartup
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jul 20, 2020
commit 7f12a0f362307eda078f803c5a55dab1b1d3de7a
1 change: 1 addition & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as onStartup } from './onStartup'
export { default as onErrorPage } from './onErrorPage'
export { default as onTestPass } from './onTestPass'
79 changes: 79 additions & 0 deletions src/actions/onStartup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as vscode from 'vscode'
import * as T from 'typings'
import * as TT from 'typings/tutorial'
import * as E from 'typings/error'
import Context from '../services/context/context'
import { WORKSPACE_ROOT, TUTORIAL_URL } from '../environment'
import fetch from 'node-fetch'
import logger from '../services/logger'

const onStartup = async (
context: Context,
workspaceState: vscode.Memento,
send: (action: T.Action) => Promise<void>,
) => {
try {
// check if a workspace is open, otherwise nothing works
const noActiveWorkspace = !WORKSPACE_ROOT.length
if (noActiveWorkspace) {
const error: E.ErrorMessage = {
type: 'NoWorkspaceFound',
message: '',
actions: [
{
label: 'Open Workspace',
transition: 'REQUEST_WORKSPACE',
},
],
}
send({ type: 'NO_WORKSPACE', payload: { error } })
return
}

const env = {
machineId: vscode.env.machineId,
sessionId: vscode.env.sessionId,
}

// load tutorial from url
if (TUTORIAL_URL) {
try {
const tutorialRes = await fetch(TUTORIAL_URL)
const tutorial = await tutorialRes.json()
send({ type: 'START_TUTORIAL_FROM_URL', payload: { tutorial } })
return
} catch (e) {
console.log(`Failed to load tutorial from url ${TUTORIAL_URL} with error "${e.message}"`)
}
}

// continue from tutorial from local storage
const tutorial: TT.Tutorial | null = context.tutorial.get()

// no stored tutorial, must start new tutorial
if (!tutorial || !tutorial.id) {
send({ type: 'START_NEW_TUTORIAL', payload: { env } })
return
}

// load continued tutorial position & progress
const { position, progress } = await context.setTutorial(workspaceState, tutorial)
logger('CONTINUE STATE', position, progress)

if (progress.complete) {
// tutorial is already complete
send({ type: 'TUTORIAL_ALREADY_COMPLETE', payload: { env } })
return
}
// communicate to client the tutorial & stepProgress state
send({ type: 'LOAD_STORED_TUTORIAL', payload: { env, tutorial, progress, position } })
} catch (e) {
const error = {
type: 'UnknownError',
message: `Location: Editor startup\n\n${e.message}`,
}
send({ type: 'EDITOR_STARTUP_FAILED', payload: { error } })
}
}

export default onStartup
65 changes: 1 addition & 64 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as T from 'typings'
import * as TT from 'typings/tutorial'
import * as E from 'typings/error'
import * as vscode from 'vscode'
import fetch from 'node-fetch'
import { satisfies } from 'semver'
import { setupActions, solutionActions } from './actions/setupActions'
import tutorialConfig from './actions/tutorialConfig'
Expand All @@ -13,7 +12,6 @@ import { version, compareVersions } from './services/dependencies'
import { openWorkspace, checkWorkspaceEmpty } from './services/workspace'
import { showOutput } from './services/testRunner/output'
import { exec } from './services/node'
import { WORKSPACE_ROOT, TUTORIAL_URL } from './environment'
import reset from './services/reset'
import getLastCommitHash from './services/reset/lastHash'
import { onEvent } from './services/telemetry'
Expand Down Expand Up @@ -50,68 +48,7 @@ class Channel implements Channel {

switch (actionType) {
case 'EDITOR_STARTUP':
try {
// check if a workspace is open, otherwise nothing works
const noActiveWorkspace = !WORKSPACE_ROOT.length
if (noActiveWorkspace) {
const error: E.ErrorMessage = {
type: 'NoWorkspaceFound',
message: '',
actions: [
{
label: 'Open Workspace',
transition: 'REQUEST_WORKSPACE',
},
],
}
this.send({ type: 'NO_WORKSPACE', payload: { error } })
return
}

const env = {
machineId: vscode.env.machineId,
sessionId: vscode.env.sessionId,
}

// load tutorial from url
if (TUTORIAL_URL) {
try {
const tutorialRes = await fetch(TUTORIAL_URL)
const tutorial = await tutorialRes.json()
this.send({ type: 'START_TUTORIAL_FROM_URL', payload: { tutorial } })
return
} catch (e) {
console.log(`Failed to load tutorial from url ${TUTORIAL_URL} with error "${e.message}"`)
}
}

// continue from tutorial from local storage
const tutorial: TT.Tutorial | null = this.context.tutorial.get()

// no stored tutorial, must start new tutorial
if (!tutorial || !tutorial.id) {
this.send({ type: 'START_NEW_TUTORIAL', payload: { env } })
return
}

// load continued tutorial position & progress
const { position, progress } = await this.context.setTutorial(this.workspaceState, tutorial)
logger('CONTINUE STATE', position, progress)

if (progress.complete) {
// tutorial is already complete
this.send({ type: 'TUTORIAL_ALREADY_COMPLETE', payload: { env } })
return
}
// communicate to client the tutorial & stepProgress state
this.send({ type: 'LOAD_STORED_TUTORIAL', payload: { env, tutorial, progress, position } })
} catch (e) {
const error = {
type: 'UnknownError',
message: `Location: Editor startup\n\n${e.message}`,
}
this.send({ type: 'EDITOR_STARTUP_FAILED', payload: { error } })
}
actions.onStartup(this.context, this.workspaceState, this.send)
return

// clear tutorial local storage
Expand Down