Skip to content

Commit 5ab2eb5

Browse files
committed
Fix error when agent has no metadata
The problem is that we dispose the watcher, which removes it from the array, but then immediately try to get it off the array. It then throws into the catch where it tries to get it off the array again to dispose (again). Use a local object instead of getting it off the array each time.
1 parent f0848ec commit 5ab2eb5

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/workspacesProvider.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ export enum WorkspaceQuery {
1111
All = "",
1212
}
1313

14+
type AgentWatcher = { dispose: () => void; metadata?: AgentMetadataEvent[] }
15+
1416
export class WorkspaceProvider implements vscode.TreeDataProvider<vscode.TreeItem> {
1517
private workspaces: WorkspaceTreeItem[] = []
16-
private agentWatchers: Record<WorkspaceAgent["id"], { dispose: () => void; metadata?: AgentMetadataEvent[] }> = {}
18+
private agentWatchers: Record<WorkspaceAgent["id"], AgentWatcher> = {}
1719
private timeout: NodeJS.Timeout | undefined
1820
private visible = false
1921
private fetching = false
@@ -174,29 +176,35 @@ export class WorkspaceProvider implements vscode.TreeDataProvider<vscode.TreeIte
174176
},
175177
})
176178

177-
this.agentWatchers[agentId] = {
179+
let disposed = false
180+
const watcher: AgentWatcher = {
178181
dispose: () => {
179-
delete this.agentWatchers[agentId]
180-
agentMetadataEventSource.close()
182+
if (!disposed) {
183+
delete this.agentWatchers[agentId]
184+
agentMetadataEventSource.close()
185+
disposed = true
186+
}
181187
},
182188
}
183189

190+
this.agentWatchers[agentId] = watcher
191+
184192
agentMetadataEventSource.addEventListener("data", (event) => {
185193
try {
186194
const dataEvent = JSON.parse(event.data)
187195
const agentMetadata = AgentMetadataEventSchemaArray.parse(dataEvent)
188196

189197
if (agentMetadata.length === 0) {
190-
this.agentWatchers[agentId].dispose()
198+
watcher.dispose()
191199
}
192200

193-
const savedMetadata = this.agentWatchers[agentId].metadata
194-
if (JSON.stringify(savedMetadata) !== JSON.stringify(agentMetadata)) {
195-
this.agentWatchers[agentId].metadata = agentMetadata // overwrite existing metadata
201+
// Overwrite metadata if it changed.
202+
if (JSON.stringify(watcher.metadata) !== JSON.stringify(agentMetadata)) {
203+
watcher.metadata = agentMetadata
196204
this.refresh()
197205
}
198206
} catch (error) {
199-
this.agentWatchers[agentId].dispose()
207+
watcher.dispose()
200208
}
201209
})
202210
}

0 commit comments

Comments
 (0)