| 8 | export type RuntimeStatus = "running" | "stopped" | "unknown" | "unsupported"; |
| 9 | |
| 10 | export class RuntimeStatusStore { |
| 11 | private statuses = new MapStore<string, RuntimeStatus | null>(); |
| 12 | private statusCache = new Map<string, RuntimeStatus | null>(); |
| 13 | private client: APIClient | null = null; |
| 14 | private immediateUpdateQueued = false; |
| 15 | private workspaceMetadata = new Map<string, FrontendWorkspaceMetadata>(); |
| 16 | private isActive = true; |
| 17 | private readonly refreshController: RefreshController; |
| 18 | |
| 19 | constructor() { |
| 20 | this.refreshController = new RefreshController({ |
| 21 | onRefresh: () => this.updateRuntimeStatuses(), |
| 22 | onRefreshError: (failure) => { |
| 23 | console.error("[RuntimeStatusStore] refresh failed:", failure.errorMessage); |
| 24 | }, |
| 25 | refreshOnFocus: true, |
| 26 | focusDebounceMs: 500, |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | setClient(client: APIClient | null): void { |
| 31 | this.client = client; |
| 32 | |
| 33 | if (!client) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (this.workspaceMetadata.size > 0) { |
| 38 | this.refreshController.requestImmediate(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | subscribe = this.statuses.subscribeAny; |
| 43 | |
| 44 | subscribeKey = (workspaceId: string, listener: () => void) => { |
| 45 | const unsubscribe = this.statuses.subscribeKey(workspaceId, listener); |
| 46 | |
| 47 | if (!this.immediateUpdateQueued && this.isActive && this.client) { |
| 48 | this.immediateUpdateQueued = true; |
| 49 | queueMicrotask(() => { |
| 50 | this.immediateUpdateQueued = false; |
| 51 | this.refreshController.requestImmediate(); |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | return unsubscribe; |
| 56 | }; |
| 57 | |
| 58 | getStatus(workspaceId: string): RuntimeStatus | null { |
| 59 | if (!this.statuses.has(workspaceId)) { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | return this.statuses.get(workspaceId, () => { |
| 64 | return this.statusCache.get(workspaceId) ?? null; |
| 65 | }); |
| 66 | } |
| 67 |
nothing calls this directly
no test coverage detected