Skip to content
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

Merged
merged 6 commits into from
Dec 11, 2024

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented Sep 11, 2024

This is the sister PR for apollographql/apollo-client-devtools#1513

CI fails because no devtools package has been published yet, but please review it anyways :)

Comment on lines +57 to +73
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,
},
});
}
}
},
Copy link
Member Author

Choose a reason for hiding this comment

The 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 initializePanel message with some additional context.

| { port: false | number; disposable: Disposable }
| undefined = undefined;

export function startServer(port: number) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of checks moved over from extension.ts here and the whole "disposing" and "restarting" should be a lot more solid now.

Comment on lines +30 to +31
| { port: false | number; disposable: Disposable }
| undefined = undefined;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false if the server is in the middle of starting, number if the server is running. The whole serverState is undefined after the server has stopped or if it has never been started before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why use false instead of null or undefined to represent the initial state?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It felt more deliberate than undefined. null would have been fine too, I guess.

Copy link
Contributor

github-actions bot commented Sep 20, 2024

You can download the latest build of the extension for this PR here:
vscode-apollo-0.0.0-build-1733911509.pr-197.commit-51db1cf.zip.

To install the extension, download the file, unzip it and install it in VS Code by selecting "Install from VSIX..." in the Extensions view.

Alternatively, run

code --install-extension vscode-apollo-0.0.0-build-1733911509.pr-197.commit-51db1cf.vsix --force

from the command line.

For older builds, please see the edit history of this comment.

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a few suggestions to tighten up the code a bit, but otherwise looks good

Comment on lines +58 to +59
if (data.source === "vscode-panel") {
if (data.type === "mounted") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (data.source === "vscode-panel") {
if (data.type === "mounted") {
if (data.source === "vscode-panel" && data.type === "mounted") {

You should be able to combine these conditionals.

Copy link
Member Author

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 other data.type in the future, which is why I separated those from each other.

Comment on lines +30 to +31
| { port: false | number; disposable: Disposable }
| undefined = undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why use false instead of null or undefined to represent the initial state?

| undefined = undefined;

export function startServer(port: number) {
const state = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, whats the advantage of using a separate local state variable here instead of just assigning this object to serverState and modifying that in wss.on('listening') for example? Is it to avoid ? or ! since serverState is of type | undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See a bit further down: if (serverState === state) { - it makes it possible to detect if serverState has been reassigned.

Comment on lines 47 to 55
if (serverState) {
if (serverState.port === port) {
// nothing to do
return;
} else {
// changing port, stop the old server
serverState.disposable.dispose();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (serverState) {
if (serverState.port === port) {
// nothing to do
return;
} else {
// changing port, stop the old server
serverState.disposable.dispose();
}
}
if (serverState && serverState.port !== port) {
// changing port, stop the old server
serverState.disposable.dispose();
}

Should be able to simplify this by combining the conditionals and getting rid of the branch that doesn't do anything.

Copy link
Member

@jerelmiller jerelmiller Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed the return keyword 🤦. Ignore the original suggestion.

That said, I think just adding an if above this makes sense:

if (serverState?.port === port) {
  return;
}

Which would mean that any code after this would mean that serverState.port is different or uninitialized. I think then you can just remove the if. The whole thing would look like:

if (serverState?.port === port) {
  return;
}

// changing port, stop the old server
serverState?.disposable.dispose();
// ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c902746

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and d4a65ee ... that took a moment to click for me

@jerelmiller jerelmiller removed the request for review from alessbell December 9, 2024 21:10
@phryneas phryneas merged commit 096869a into pr/devtools-webview Dec 11, 2024
11 checks passed
@phryneas phryneas deleted the pr/port-info branch December 11, 2024 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants