-
Notifications
You must be signed in to change notification settings - Fork 23
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
Adjustments to VSCode <-> DevTools communication #197
Changes from 1 commit
3700b50
277a5ad
c902746
d4a65ee
7325a4d
91967fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
adjustments for apollographql/apollo-client-devtools#1513
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,34 @@ | ||
import * as vscode from "vscode"; | ||
import { devtoolsEvents } from "./server"; | ||
import { devtoolsEvents, sendToDevTools, serverState } from "./server"; | ||
|
||
type ActorMessage = { type: "actor"; message: unknown }; | ||
|
||
export function isActorMessage(message: any): message is ActorMessage { | ||
return message && message.type === "actor"; | ||
} | ||
|
||
type DevToolsOpenCommandMessage = { | ||
type: "vscode:executeCommand"; | ||
command: string; | ||
arguments?: unknown[]; | ||
}; | ||
|
||
export function isDevToolsExecuteCommandMessage( | ||
message: any, | ||
): message is DevToolsOpenCommandMessage { | ||
return message && message.type === "vscode:executeCommand"; | ||
} | ||
|
||
type DevToolsOpenExternalMessage = { | ||
type: "vscode:openExternal"; | ||
uri: string; | ||
}; | ||
|
||
export function isDevToolsOpenExternalMessage( | ||
message: any, | ||
): message is DevToolsOpenExternalMessage { | ||
return message && message.type === "vscode:openExternal"; | ||
} | ||
|
||
export class DevToolsViewProvider implements vscode.WebviewViewProvider { | ||
public static readonly viewType = "vscode-apollo-client-devtools"; | ||
|
@@ -20,16 +49,40 @@ export class DevToolsViewProvider implements vscode.WebviewViewProvider { | |
panel.webview, | ||
this._extensionUri, | ||
); | ||
|
||
panel.webview.onDidReceiveMessage((data) => { | ||
devtoolsEvents.emit("fromDevTools", data); | ||
}); | ||
const panelDisposables: vscode.Disposable[] = []; | ||
panel.webview.onDidReceiveMessage( | ||
(data) => { | ||
if (data.source === "apollo-client-devtools") { | ||
devtoolsEvents.emit("fromDevTools", data); | ||
} | ||
if (data.source === "vscode-panel") { | ||
if (data.type === "mounted") { | ||
sendToDevTools({ | ||
type: "initializePanel", | ||
initialContext: { | ||
port: | ||
serverState?.port || | ||
vscode.workspace | ||
.getConfiguration("apollographql") | ||
.get("devTools.serverPort", 0), | ||
listening: !!serverState?.port, | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
Comment on lines
+58
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where previously the panel kickstarted itself, it now notifies the extension, and the extension dispatches the |
||
this, | ||
panelDisposables, | ||
); | ||
function forwardToDevTools(data: unknown) { | ||
panel.webview.postMessage(data); | ||
} | ||
devtoolsEvents.addListener("toDevTools", forwardToDevTools); | ||
panel.onDidDispose(() => { | ||
devtoolsEvents.removeListener("toDevTools", forwardToDevTools); | ||
for (const disposable of panelDisposables) { | ||
disposable.dispose(); | ||
} | ||
}); | ||
} | ||
|
||
|
@@ -107,11 +160,9 @@ export class DevToolsViewProvider implements vscode.WebviewViewProvider { | |
</script> | ||
<script nonce="${nonce}" src="${scriptUri}"></script> | ||
<script nonce="${nonce}"> | ||
window.originalPostMessage({ | ||
id: 123, | ||
source: "apollo-client-devtools", | ||
type: "actor", | ||
message: { type: "initializePanel" }, | ||
window.postMessage({ | ||
source: "vscode-panel", | ||
type: "mounted", | ||
}); | ||
</script> | ||
</body> | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,8 +15,53 @@ devtoolsEvents.addListener("fromDevTools", (msg) => { | |||||||||||||||||||||||||||
Debug.info("DevTools > WS: " + JSON.stringify(msg)); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
export function startServer(port: number): Disposable { | ||||||||||||||||||||||||||||
let id = 1; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
export function sendToDevTools(message: unknown) { | ||||||||||||||||||||||||||||
devtoolsEvents.emit("toDevTools", { | ||||||||||||||||||||||||||||
id: `vscode-${id++}`, | ||||||||||||||||||||||||||||
source: "apollo-client-devtools", | ||||||||||||||||||||||||||||
type: "actor", | ||||||||||||||||||||||||||||
message, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
export let serverState: | ||||||||||||||||||||||||||||
| { port: false | number; disposable: Disposable } | ||||||||||||||||||||||||||||
| undefined = undefined; | ||||||||||||||||||||||||||||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It felt more deliberate than |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
export function startServer(port: number) { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bunch of checks moved over from |
||||||||||||||||||||||||||||
const state = { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious, whats the advantage of using a separate local There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See a bit further down: |
||||||||||||||||||||||||||||
port: false as false | number, | ||||||||||||||||||||||||||||
disposable: new Disposable(() => { | ||||||||||||||||||||||||||||
if (wss) { | ||||||||||||||||||||||||||||
wss.close(); | ||||||||||||||||||||||||||||
wss = null; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
if (serverState === state) { | ||||||||||||||||||||||||||||
serverState = undefined; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
sendToDevTools({ type: "port.changed", port, listening: false }); | ||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
if (serverState) { | ||||||||||||||||||||||||||||
if (serverState.port === port) { | ||||||||||||||||||||||||||||
// nothing to do | ||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
// changing port, stop the old server | ||||||||||||||||||||||||||||
serverState.disposable.dispose(); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Should be able to simplify this by combining the conditionals and getting rid of the branch that doesn't do anything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just noticed the That said, I think just adding an if (serverState?.port === port) {
return;
} Which would mean that any code after this would mean that if (serverState?.port === port) {
return;
}
// changing port, stop the old server
serverState?.disposable.dispose();
// ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. c902746 ✅ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and d4a65ee ... that took a moment to click for me |
||||||||||||||||||||||||||||
serverState = state; | ||||||||||||||||||||||||||||
let wss: WebSocketServer | null = new WebSocketServer({ port }); | ||||||||||||||||||||||||||||
wss.on("listening", () => { | ||||||||||||||||||||||||||||
state.port = port; | ||||||||||||||||||||||||||||
sendToDevTools({ type: "port.changed", port, listening: true }); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
wss.on("close", () => { | ||||||||||||||||||||||||||||
state.disposable.dispose(); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
runServer(wss, { | ||||||||||||||||||||||||||||
addListener: (listener) => { | ||||||||||||||||||||||||||||
devtoolsEvents.addListener("fromDevTools", listener); | ||||||||||||||||||||||||||||
|
@@ -28,9 +73,4 @@ export function startServer(port: number): Disposable { | |||||||||||||||||||||||||||
devtoolsEvents.emit("toDevTools", message); | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return new Disposable(() => { | ||||||||||||||||||||||||||||
wss?.close(); | ||||||||||||||||||||||||||||
wss = null; | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to combine these conditionals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume that inside of
data.source
we'll see more reactions to otherdata.type
in the future, which is why I separated those from each other.