Refactor instance metadata handling

This commit is contained in:
Shantur Rathore
2025-12-15 16:08:28 +00:00
parent 14497f2082
commit ff5c698131
6 changed files with 101 additions and 59 deletions

View File

@@ -0,0 +1,35 @@
import { createSignal } from "solid-js"
import type { InstanceMetadata } from "../types/instance"
const [metadataMap, setMetadataMap] = createSignal<Map<string, InstanceMetadata | undefined>>(new Map())
function getInstanceMetadata(instanceId: string): InstanceMetadata | undefined {
return metadataMap().get(instanceId)
}
function setInstanceMetadata(instanceId: string, metadata: InstanceMetadata | undefined): void {
setMetadataMap((prev) => {
const next = new Map(prev)
if (metadata === undefined) {
next.delete(instanceId)
} else {
next.set(instanceId, metadata)
}
return next
})
}
function mergeInstanceMetadata(instanceId: string, updates: InstanceMetadata): void {
setMetadataMap((prev) => {
const next = new Map(prev)
const existing = next.get(instanceId) ?? {}
next.set(instanceId, { ...existing, ...updates })
return next
})
}
function clearInstanceMetadata(instanceId: string): void {
setInstanceMetadata(instanceId, undefined)
}
export { metadataMap, getInstanceMetadata, setInstanceMetadata, mergeInstanceMetadata, clearInstanceMetadata }

View File

@@ -20,6 +20,7 @@ import { setHasInstances } from "./ui"
import { messageStoreBus } from "./message-v2/bus"
import { clearCacheForInstance } from "../lib/global-cache"
import { getLogger } from "../lib/logger"
import { mergeInstanceMetadata, clearInstanceMetadata } from "./instance-metadata"
const log = getLogger("api")
@@ -290,6 +291,7 @@ function removeInstance(id: string) {
removeLogContainer(id)
clearCommands(id)
clearPermissionQueue(id)
clearInstanceMetadata(id)
if (activeInstanceId() === id) {
setActiveInstanceId(nextActiveId)
@@ -570,17 +572,7 @@ sseManager.onLspUpdated = async (instanceId) => {
if (!lspStatus) {
return
}
const instance = instances().get(instanceId)
if (!instance) {
log.warn("[LSP] Instance disappeared before metadata update", { instanceId })
return
}
updateInstance(instanceId, {
metadata: {
...(instance.metadata ?? {}),
lspStatus,
},
})
mergeInstanceMetadata(instanceId, { lspStatus })
} catch (error) {
log.error("Failed to refresh LSP status", error)
}