diff --git a/packages/vscode/src/ChatViewProvider.ts b/packages/vscode/src/ChatViewProvider.ts index 63d2efa5..0304baba 100644 --- a/packages/vscode/src/ChatViewProvider.ts +++ b/packages/vscode/src/ChatViewProvider.ts @@ -45,7 +45,7 @@ export class ChatViewProvider implements vscode.WebviewViewProvider { private _cachedStatus: ConnectionStatus = 'connecting'; private _cachedError?: string; private _sseCounter = 0; - private _sseStreams = new Map(); + private _sseStreams = new Map(); private readonly _webviewDevServerUrl: string | null; private _broadcastSelectionDebounce: ReturnType | undefined; private _clearActiveEditorFileTimer: ReturnType | undefined; @@ -107,7 +107,23 @@ export class ChatViewProvider implements vscode.WebviewViewProvider { void this._broadcastActiveEditorFile(); webviewView.onDidDispose(() => { + for (const [streamId, stream] of this._sseStreams) { + if (stream.view !== webviewView) continue; + stream.controller.abort(); + this._sseStreams.delete(streamId); + } + if (this._view !== webviewView) return; + if (this._broadcastSelectionDebounce !== undefined) { + clearTimeout(this._broadcastSelectionDebounce); + this._broadcastSelectionDebounce = undefined; + } + if (this._clearActiveEditorFileTimer !== undefined) { + clearTimeout(this._clearActiveEditorFileTimer); + this._clearActiveEditorFileTimer = undefined; + } + this._lastActiveEditorFilePayload = null; this._clearPendingMessages(); + this._view = undefined; }); webviewView.webview.onDidReceiveMessage(async (message: (BridgeRequest & { _msgId?: string }) | { type: 'bridge:ack'; _msgId: string }) => { @@ -457,7 +473,7 @@ export class ChatViewProvider implements vscode.WebviewViewProvider { }, }); - this._sseStreams.set(streamId, controller); + this._sseStreams.set(streamId, { controller, view: this._view }); start.run .then(() => { @@ -498,9 +514,9 @@ export class ChatViewProvider implements vscode.WebviewViewProvider { const { id, type, payload } = message; const { streamId } = (payload || {}) as { streamId?: string }; if (typeof streamId === 'string' && streamId.length > 0) { - const controller = this._sseStreams.get(streamId); - if (controller) { - controller.abort(); + const stream = this._sseStreams.get(streamId); + if (stream) { + stream.controller.abort(); this._sseStreams.delete(streamId); } }