Use server naming for shared API/events

This commit is contained in:
Shantur Rathore
2025-11-21 00:04:01 +00:00
parent d6fdef68d9
commit 2ff51c1866
9 changed files with 31 additions and 31 deletions

View File

@@ -80,7 +80,7 @@ async function request<T>(path: string, init?: RequestInit): Promise<T> {
}
export const cliApi = {
export const serverApi = {
fetchWorkspaces(): Promise<WorkspaceDescriptor[]> {
return request<WorkspaceDescriptor[]>("/api/workspaces")
},

View File

@@ -1,5 +1,5 @@
import type { WorkspaceEventPayload, WorkspaceEventType } from "../../../server/src/api-types"
import { cliApi } from "./api-client"
import { serverApi } from "./api-client"
const RETRY_BASE_DELAY = 1000
const RETRY_MAX_DELAY = 10000
@@ -13,7 +13,7 @@ function logSse(message: string, context?: Record<string, unknown>) {
console.log(`${SSE_PREFIX} ${message}`)
}
class CliEvents {
class ServerEvents {
private handlers = new Map<WorkspaceEventType | "*", Set<(event: WorkspaceEventPayload) => void>>()
private source: EventSource | null = null
private retryDelay = RETRY_BASE_DELAY
@@ -27,7 +27,7 @@ class CliEvents {
this.source.close()
}
logSse("Connecting to backend events stream")
this.source = cliApi.connectEvents((event) => this.dispatch(event), () => this.scheduleReconnect())
this.source = serverApi.connectEvents((event) => this.dispatch(event), () => this.scheduleReconnect())
this.source.onopen = () => {
logSse("Events stream connected")
this.retryDelay = RETRY_BASE_DELAY
@@ -62,4 +62,4 @@ class CliEvents {
}
}
export const cliEvents = new CliEvents()
export const serverEvents = new ServerEvents()

View File

@@ -1,5 +1,5 @@
import type { ServerMeta } from "../../../server/src/api-types"
import { cliApi } from "./api-client"
import { serverApi } from "./api-client"
let cachedMeta: ServerMeta | null = null
let pendingMeta: Promise<ServerMeta> | null = null
@@ -11,7 +11,7 @@ export async function getServerMeta(): Promise<ServerMeta> {
if (pendingMeta) {
return pendingMeta
}
pendingMeta = cliApi.fetchServerMeta().then((meta) => {
pendingMeta = serverApi.fetchServerMeta().then((meta) => {
cachedMeta = meta
pendingMeta = null
return meta

View File

@@ -1,6 +1,6 @@
import type { AppConfig, InstanceData } from "../../../server/src/api-types"
import { cliApi } from "./api-client"
import { cliEvents } from "./cli-events"
import { serverApi } from "./api-client"
import { serverEvents } from "./server-events"
export type ConfigData = AppConfig
@@ -35,12 +35,12 @@ export class ServerStorage {
private instanceLoadPromises = new Map<string, Promise<InstanceData>>()
constructor() {
cliEvents.on("config.appChanged", (event) => {
serverEvents.on("config.appChanged", (event) => {
if (event.type !== "config.appChanged") return
this.setConfigCache(event.config)
})
cliEvents.on("instance.dataChanged", (event) => {
serverEvents.on("instance.dataChanged", (event) => {
if (event.type !== "instance.dataChanged") return
this.setInstanceDataCache(event.instanceId, event.data)
})
@@ -52,7 +52,7 @@ export class ServerStorage {
}
if (!this.loadPromise) {
this.loadPromise = cliApi
this.loadPromise = serverApi
.fetchConfig()
.then((config) => {
this.setConfigCache(config)
@@ -67,7 +67,7 @@ export class ServerStorage {
}
async updateConfig(next: ConfigData): Promise<ConfigData> {
const nextConfig = await cliApi.updateConfig(next)
const nextConfig = await serverApi.updateConfig(next)
this.setConfigCache(nextConfig)
return nextConfig
}
@@ -79,7 +79,7 @@ export class ServerStorage {
}
if (!this.instanceLoadPromises.has(instanceId)) {
const promise = cliApi
const promise = serverApi
.readInstanceData(instanceId)
.then((data) => {
const normalized = this.normalizeInstanceData(data)
@@ -98,12 +98,12 @@ export class ServerStorage {
async saveInstanceData(instanceId: string, data: InstanceData): Promise<void> {
const normalized = this.normalizeInstanceData(data)
await cliApi.writeInstanceData(instanceId, normalized)
await serverApi.writeInstanceData(instanceId, normalized)
this.setInstanceDataCache(instanceId, normalized)
}
async deleteInstanceData(instanceId: string): Promise<void> {
await cliApi.deleteInstanceData(instanceId)
await serverApi.deleteInstanceData(instanceId)
this.setInstanceDataCache(instanceId, DEFAULT_INSTANCE_DATA)
}