8000 filter events by source=coderoad · dcmade01/coderoad-vscode@335a9c7 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 335a9c7

Browse files
committed
filter events by source=coderoad
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 060ad10 commit 335a9c7

File tree

4 files changed

+21
-7
lines changed
  • typings
  • web-app/src/services/state
  • 4 files changed

    +21
    -7
    lines changed

    src/channel.ts

    Lines changed: 5 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -20,6 +20,11 @@ class Channel implements Channel {
    2020

    2121
    // receive from webview
    2222
    public receive = async (action: T.Action): Promise<void> => {
    23+
    if (action.source !== 'coderoad') {
    24+
    // filter out events from other extensions
    25+
    return
    26+
    }
    27+
    2328
    // action may be an object.type or plain string
    2429
    const actionType: string = typeof action === 'string' ? action : action.type
    2530

    src/services/webview/create.ts

    Lines changed: 5 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -56,7 +56,11 @@ const createReactWebView = ({ extensionPath, channel }: ReactWebViewProps): Outp
    5656

    5757
    // Handle messages from the webview
    5858
    const receive = channel.receive
    59-
    const send = (action: T.Action) => panel.webview.postMessage(action)
    59+
    const send = (action: T.Action) =>
    60+
    panel.webview.postMessage({
    61+
    ...action,
    62+
    source: 'coderoad', // filter events on client by source. origin is not reliable
    63+
    })
    6064

    6165
    panel.webview.onDidReceiveMessage(receive, null, disposables)
    6266

    typings/index.d.ts

    Lines changed: 1 addition & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -49,6 +49,7 @@ export interface Position {
    4949
    // current tutorial state
    5050

    5151
    export interface Action {
    52+
    source?: 'coderoad' // filter received actions by this
    5253
    type: string
    5354
    payload?: any
    5455
    meta?: any

    web-app/src/services/state/useStateMachine.tsx

    Lines changed: 10 additions & 6 deletions
    Original file line numberDiff line numberDiff line change
    @@ -16,7 +16,10 @@ declare let acquireVsCodeApi: any
    1616
    const editor = acquireVsCodeApi()
    1717
    const editorSend = (action: T.Action) => {
    1818
    logger(`TO EXT: "${action.type}"`)
    19-
    return editor.postMessage(action)
    19+
    return editor.postMessage({
    20+
    ...action,
    21+
    source: 'coderoad', // filter events by source on editor side
    22+
    })
    2023
    }
    2124

    2225
    // router finds first state match of <Route path='' />
    @@ -31,14 +34,15 @@ const useStateMachine = (): Output => {
    3134
    // event bus listener
    3235
    React.useEffect(() => {
    3336
    const listener = 'message'
    34-
    // propograte channel event to state machine
    37+
    // propagate channel event to state machine
    3538
    const handler = (event: any) => {
    36-
    // ensure events are coming from coderoad webview
    37-
    if (!event.origin.match(/^vscode-webview/)) {
    38-
    return
    39-
    }
    4039
    // NOTE: must call event.data, cannot destructure. VSCode acts odd
    4140
    const action = event.data
    41+
    42+
    if (action.source !== 'coderoad') {
    43+
    // filter out events from other extensions
    44+
    return
    45+
    }
    4246
    sendWithLog(action)
    4347
    }
    4448
    window.addEventListener(listener, handler)

    0 commit comments

    Comments
     (0)
    0