Skip to content

Commit

Permalink
Lazy Connect for Debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdriscoll committed May 8, 2024
1 parent 494a13f commit 3ea747e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "PowerShell Universal",
"description": "Visual Studio Code tools for PowerShell Universal",
"publisher": "ironmansoftware",
"version": "4.2.2",
"version": "4.2.3",
"engines": {
"vscode": "^1.72.0"
},
Expand Down
20 changes: 13 additions & 7 deletions src/commands/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const registerDebuggerCommands = (context: vscode.ExtensionContext) => {


export const attachRunspace = async (runspace: RunspaceTreeItem, context: vscode.ExtensionContext) => {
adapter.startSession();

await vscode.debug.startDebugging(undefined, {
name: "PowerShell Universal",
type: "powershelluniversal",
Expand All @@ -33,16 +35,19 @@ export const attachRunspace = async (runspace: RunspaceTreeItem, context: vscode
export class UniversalDebugAdapter implements vscode.DebugAdapter {

constructor(context: vscode.ExtensionContext) {
const connectionName = context.globalState.get("universal.connection");
this.connectionName = context.globalState.get("universal.connection");
}

public startSession(): void {
const settings = load();

var appToken = settings.appToken;
var url = settings.url;
var rejectUnauthorized = true;
var windowsAuth = false;

if (connectionName && connectionName !== 'Default') {
const connection = settings.connections.find(m => m.name === connectionName);
if (this.connectionName && this.connectionName !== 'Default') {
const connection = settings.connections.find(m => m.name === this.connectionName);
if (connection) {
appToken = connection.appToken;
url = connection.url;
Expand All @@ -65,13 +70,14 @@ export class UniversalDebugAdapter implements vscode.DebugAdapter {
});
}

private hubConnection: HubConnection;
private connectionName: string | undefined;
private hubConnection: HubConnection | undefined;
private sendMessage = new vscode.EventEmitter<DebugProtocol.ProtocolMessage>();

readonly onDidSendMessage: vscode.Event<DebugProtocol.ProtocolMessage> = this.sendMessage.event;

handleMessage(message: DebugProtocol.ProtocolMessage): void {
if (this.hubConnection.state === 'Disconnected') {
if (this.hubConnection?.state === 'Disconnected') {
this.hubConnection.start().then(() => {
this.handleMessage(message);
});
Expand All @@ -80,7 +86,7 @@ export class UniversalDebugAdapter implements vscode.DebugAdapter {

switch (message.type) {
case 'request':
this.hubConnection.send("message", JSON.stringify(message));
this.hubConnection?.send("message", JSON.stringify(message));
break;
case 'response':
this.sendMessage.fire(message);
Expand All @@ -92,6 +98,6 @@ export class UniversalDebugAdapter implements vscode.DebugAdapter {
}

dispose() {
this.hubConnection.stop();
this.hubConnection?.stop();
}
}

0 comments on commit 3ea747e

Please sign in to comment.