|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | + |
| 8 | +/** |
| 9 | + * This tracks a debug session until C# dev kit is loaded (if present) and STOPS tracking any existing |
| 10 | + * debug session after C# dev kit is loaded. |
| 11 | + * The idea is to avoid a race condition if a debugging session starts before C# dev kit is initialized, |
| 12 | + * since the brokered service pipe name will be sent with a 'launch' command. |
| 13 | + * If the extension loads during an existing session, rather than not initializing any C# dev kit services (such as hot reload), |
| 14 | + * this sends a custom request to the engine with the brokered service pipe name in order to initialize it. |
| 15 | + */ |
| 16 | +export class ProvisionalDebugSessionTracker { |
| 17 | + private _sessions: Set<vscode.DebugSession> | undefined = new Set<vscode.DebugSession>(); |
| 18 | + |
| 19 | + private _onDidStartDebugSession: vscode.Disposable | undefined; |
| 20 | + private _onDidTerminateDebugSession: vscode.Disposable | undefined; |
| 21 | + |
| 22 | + private _brokeredServicePipeName: string | undefined; |
| 23 | + |
| 24 | + /** |
| 25 | + * Initializes the debug session handlers. |
| 26 | + */ |
| 27 | + initializeDebugSessionHandlers(context: vscode.ExtensionContext): void { |
| 28 | + this._onDidStartDebugSession = vscode.debug.onDidStartDebugSession(this.onDidStartDebugSession.bind(this)); |
| 29 | + |
| 30 | + this._onDidTerminateDebugSession = vscode.debug.onDidTerminateDebugSession((session: vscode.DebugSession) => { |
| 31 | + this.onDidTerminateDebugSession(session); |
| 32 | + }); |
| 33 | + |
| 34 | + context.subscriptions.push(this._onDidStartDebugSession); |
| 35 | + context.subscriptions.push(this._onDidTerminateDebugSession); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Tracks a debug session until it is terminated. |
| 40 | + * @param session Debug session. |
| 41 | + */ |
| 42 | + onDidStartDebugSession(session: vscode.DebugSession): void { |
| 43 | + if (session.type !== 'coreclr') { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + this._sessions?.add(session); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Notifies that a debug session has been terminated. |
| 52 | + */ |
| 53 | + onDidTerminateDebugSession(session: vscode.DebugSession): void { |
| 54 | + this._sessions?.delete(session); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * If there is any active debug session, this notifies the engine that csdevkit was loaded. |
| 59 | + * @param csDevKitPipeName Brokered service pipe name activated by {@link CSharpDevKitExports}. |
| 60 | + */ |
| 61 | + async onCsDevKitInitialized(csDevKitPipeName: string): Promise<void> { |
| 62 | + this._brokeredServicePipeName = csDevKitPipeName; |
| 63 | + |
| 64 | + const sessions = this._sessions; |
| 65 | + if (sessions != undefined) { |
| 66 | + // Debugging session already started, send a custom DAP request to the engine. |
| 67 | + sessions.forEach((s) => s.customRequest('initializeBrokeredServicePipeName', csDevKitPipeName)); |
| 68 | + } |
| 69 | + |
| 70 | + // Since C# dev kit was initialized, we no longer need to track debugging sessions. |
| 71 | + this.cleanup(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Fetches the brokered service pipe name from C# dev kit, if available. |
| 76 | + */ |
| 77 | + getBrokeredServicePipeName(): string | undefined { |
| 78 | + return this._brokeredServicePipeName; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * No longer tracks any debugging session going forward. |
| 83 | + */ |
| 84 | + cleanup(): void { |
| 85 | + this._sessions?.clear(); |
| 86 | + this._sessions = undefined; |
| 87 | + |
| 88 | + this._onDidStartDebugSession?.dispose(); |
| 89 | + this._onDidTerminateDebugSession?.dispose(); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export const debugSessionTracker = new ProvisionalDebugSessionTracker(); |
0 commit comments