refactor: track Electron local server startup state

This commit is contained in:
LukeParkerDev
2026-04-16 14:16:22 +10:00
parent d6f2b9f1b7
commit 9d4737fdc9
2 changed files with 55 additions and 11 deletions

View File

@@ -142,7 +142,16 @@ async function initialize() {
const password = randomUUID()
logger.log("spawning sidecar", { url })
const { listener, health } = await spawnLocalServer(hostname, port, password)
localServer.setRuntime({ key: "local:windows", mode: "windows", distro: null })
localServer.setStatus({ kind: "running", step: null })
const { listener, health } = await spawnLocalServer(hostname, port, password).catch((error) => {
localServer.setStatus({
kind: "failed",
step: null,
message: error instanceof Error ? error.message : String(error),
})
throw error
})
server = listener
serverReady.resolve({
url,
@@ -169,9 +178,18 @@ async function initialize() {
delay(30_000).then(() => {
throw new Error("Sidecar health check timed out")
}),
]).catch((error) => {
logger.error("sidecar health check failed", error)
})
])
.then(() => {
localServer.setStatus({ kind: "ready" })
})
.catch((error) => {
localServer.setStatus({
kind: "failed",
step: null,
message: error instanceof Error ? error.message : String(error),
})
logger.error("sidecar health check failed", error)
})
logger.log("loading task finished")
})()

View File

@@ -26,6 +26,11 @@ export function createLocalServerController() {
for (const listener of listeners) listener(event)
}
const update = (next: LocalServerState) => {
state = next
emit({ type: "state", state })
}
return {
getState() {
return state
@@ -33,13 +38,27 @@ export function createLocalServerController() {
setConfig(config: LocalServerConfig) {
const next = normalizeLocalServerConfig(config)
store.set(LOCAL_SERVER_KEY, next)
state = toState(next, state)
emit({ type: "state", state })
update({
...state,
config: next,
})
},
subscribe(listener: (event: LocalServerEvent) => void) {
listeners.add(listener)
return () => listeners.delete(listener)
},
setRuntime(runtime: LocalServerState["runtime"]) {
update({
...state,
runtime,
})
},
setStatus(status: LocalServerState["status"]) {
update({
...state,
status,
})
},
}
}
@@ -50,11 +69,7 @@ function readLocalServerConfig() {
function toState(config: LocalServerConfig, current?: LocalServerState): LocalServerState {
return {
config,
runtime: {
key: localServerKey(config),
mode: config.mode,
distro: config.distro,
},
runtime: current?.runtime ?? windowsRuntime(),
status: current?.status ?? { kind: "idle" },
job: current?.job ?? null,
}
@@ -114,3 +129,14 @@ function localServerKey(config: LocalServerConfig) {
if (!config.distro) return "local:wsl"
return `local:wsl:${config.distro}`
}
function windowsRuntime(): LocalServerState["runtime"] {
return {
key: localServerKey({
...defaultLocalServerConfig(),
mode: "windows",
}),
mode: "windows",
distro: null,
}
}