From 335a9c72f625111f4898735e76a11ac5bc5219c1 Mon Sep 17 00:00:00 2001 From: shmck Date: Sat, 20 Nov 2021 18:38:17 -0800 Subject: [PATCH 1/2] filter events by source=coderoad Signed-off-by: shmck --- src/channel.ts | 5 +++++ src/services/webview/create.ts | 6 +++++- typings/index.d.ts | 1 + web-app/src/services/state/useStateMachine.tsx | 16 ++++++++++------ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/channel.ts b/src/channel.ts index ad41f040..994ec658 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -20,6 +20,11 @@ class Channel implements Channel { // receive from webview public receive = async (action: T.Action): Promise => { + if (action.source !== 'coderoad') { + // filter out events from other extensions + return + } + // action may be an object.type or plain string const actionType: string = typeof action === 'string' ? action : action.type diff --git a/src/services/webview/create.ts b/src/services/webview/create.ts index 1db37e6a..491e17eb 100644 --- a/src/services/webview/create.ts +++ b/src/services/webview/create.ts @@ -56,7 +56,11 @@ const createReactWebView = ({ extensionPath, channel }: ReactWebViewProps): Outp // Handle messages from the webview const receive = channel.receive - const send = (action: T.Action) => panel.webview.postMessage(action) + const send = (action: T.Action) => + panel.webview.postMessage({ + ...action, + source: 'coderoad', // filter events on client by source. origin is not reliable + }) panel.webview.onDidReceiveMessage(receive, null, disposables) diff --git a/typings/index.d.ts b/typings/index.d.ts index 837d6517..b1aa36f4 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -49,6 +49,7 @@ export interface Position { // current tutorial state export interface Action { + source?: 'coderoad' // filter received actions by this type: string payload?: any meta?: any diff --git a/web-app/src/services/state/useStateMachine.tsx b/web-app/src/services/state/useStateMachine.tsx index 6d57ada5..296ce76a 100644 --- a/web-app/src/services/state/useStateMachine.tsx +++ b/web-app/src/services/state/useStateMachine.tsx @@ -16,7 +16,10 @@ declare let acquireVsCodeApi: any const editor = acquireVsCodeApi() const editorSend = (action: T.Action) => { logger(`TO EXT: "${action.type}"`) - return editor.postMessage(action) + return editor.postMessage({ + ...action, + source: 'coderoad', // filter events by source on editor side + }) } // router finds first state match of @@ -31,14 +34,15 @@ const useStateMachine = (): Output => { // event bus listener React.useEffect(() => { const listener = 'message' - // propograte channel event to state machine + // propagate channel event to state machine const handler = (event: any) => { - // ensure events are coming from coderoad webview - if (!event.origin.match(/^vscode-webview/)) { - return - } // NOTE: must call event.data, cannot destructure. VSCode acts odd const action = event.data + + if (action.source !== 'coderoad') { + // filter out events from other extensions + return + } sendWithLog(action) } window.addEventListener(listener, handler) From 055024e090b78d501c7a1c903ced6a84fdcfd6c4 Mon Sep 17 00:00:00 2001 From: shmck Date: Sat, 20 Nov 2021 18:38:24 -0800 Subject: [PATCH 2/2] ts error updates Signed-off-by: shmck --- src/actions/onStartup.ts | 6 +++--- src/actions/onTutorialConfigContinue.ts | 2 +- src/services/hooks/utils/openFiles.ts | 2 +- src/services/hooks/utils/runCommands.ts | 2 +- src/services/reset/index.ts | 2 +- src/services/testRunner/index.ts | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/actions/onStartup.ts b/src/actions/onStartup.ts index c0a751ba..09cc97a3 100644 --- a/src/actions/onStartup.ts +++ b/src/actions/onStartup.ts @@ -42,9 +42,9 @@ const onStartup = async (context: Context): Promise => { const tutorial = await tutorialRes.json() send({ type: 'START_TUTORIAL_FROM_URL', payload: { tutorial } }) return - } catch (e) { + } catch (e: any) { // on failure to load a tutorial url fallback to NEW - console.log(`Failed to load tutorial from url ${TUTORIAL_URL} with error "${e.message}"`) + throw new Error(`Failed to load tutorial from url ${TUTORIAL_URL} with error "${e.message}"`) } } // NEW from start click @@ -56,7 +56,7 @@ const onStartup = async (context: Context): Promise => { const { position } = await context.onContinue(tutorial) // communicate to client the tutorial & stepProgress state send({ type: 'LOAD_STORED_TUTORIAL', payload: { env, tutorial, position } }) - } catch (e) { + } catch (e: any) { const error = { type: 'UnknownError', message: `Location: Editor startup\n\n${e.message}`, diff --git a/src/actions/onTutorialConfigContinue.ts b/src/actions/onTutorialConfigContinue.ts index f5e173b0..1a116c46 100644 --- a/src/actions/onTutorialConfigContinue.ts +++ b/src/actions/onTutorialConfigContinue.ts @@ -25,7 +25,7 @@ const onTutorialConfigContinue = async (action: T.Action, context: Context): Pro if (tutorialToContinue.config?.webhook) { setupWebhook(tutorialToContinue.config.webhook) } - } catch (e) { + } catch (e: any) { const error = { type: 'UnknownError', message: `Location: Editor tutorial continue config.\n\n ${e.message}`, diff --git a/src/services/hooks/utils/openFiles.ts b/src/services/hooks/utils/openFiles.ts index a57a7fc0..592f14ff 100644 --- a/src/services/hooks/utils/openFiles.ts +++ b/src/services/hooks/utils/openFiles.ts @@ -15,7 +15,7 @@ const openFiles = async (files: string[] = []): Promise => { const absoluteFilePath = join(wr, filePath) const doc = await vscode.workspace.openTextDocument(absoluteFilePath) await vscode.window.showTextDocument(doc, vscode.ViewColumn.One) - } catch (error) { + } catch (error: any) { console.log(`Failed to open file ${filePath}: ${error.message}`) } } diff --git a/src/services/hooks/utils/runCommands.ts b/src/services/hooks/utils/runCommands.ts index ed3b871d..a462d8b8 100644 --- a/src/services/hooks/utils/runCommands.ts +++ b/src/services/hooks/utils/runCommands.ts @@ -15,7 +15,7 @@ const runCommands = async (commands: string[] = []): Promise => { try { result = await exec({ command }) console.log(result) - } catch (error) { + } catch (error: any) { console.error(`Command failed: ${error.message}`) send({ type: 'COMMAND_FAIL', payload: { process: { ...process, status: 'FAIL' } } }) return diff --git a/src/services/reset/index.ts b/src/services/reset/index.ts index 887feed2..b05016da 100644 --- a/src/services/reset/index.ts +++ b/src/services/reset/index.ts @@ -63,7 +63,7 @@ const reset = async ({ branch, hash }: Input): Promise => { await exec({ command: `git reset --hard ${hash}`, }) - } catch (error) { + } catch (error: any) { console.error('Error resetting') console.error(error.message) } diff --git a/src/services/testRunner/index.ts b/src/services/testRunner/index.ts index a9081673..cb264665 100644 --- a/src/services/testRunner/index.ts +++ b/src/services/testRunner/index.ts @@ -75,7 +75,7 @@ const createTestRunner = (data: TT.Tutorial, callbacks: Callbacks): ((params: an } logger('COMMAND', command) result = await exec({ command, dir: testRunnerConfig.directory }) - } catch (err) { + } catch (err: any) { result = { stdout: err.stdout, stderr: err.stack } }