Open History to see the last commit
1import type {
2 Report,
3 ReportState,
4 SignalRegistry,
5 UIEvent,
6 UIUpdate,
7 WatcherSubscription,
8} from './types'
9import {
10 applyUIEvent,
11 applyUIUpdates,
12 createEmptyReportState,
13} from './apply-event'
14import { createSignalRegistry } from './signal-registry'
15
16/**
17 * Extended server-side state that includes signal registry and SSE controller.
18 * These are server-only fields not needed on the client.
19 */
20export interface ServerReportState extends ReportState {
21 signalRegistry: SignalRegistry
22 sseController?: ReadableStreamDefaultController<Uint8Array>
23 watchers: Map<string, WatcherSubscription>
24}
25
26const reportStateStore = new Map<string, ServerReportState>()
27
28export function createReportState(report: Report): ServerReportState {
29 const baseState = createEmptyReportState(report)
30 const state: ServerReportState = {
31 ...baseState,
32 signalRegistry: createSignalRegistry(),
33 watchers: new Map(),
34 }
35 reportStateStore.set(report.id, state)
36 return state
37}
38
39export function deleteReportState(reportId: string) {
40 reportStateStore.delete(reportId)
41}
42
43export function getReportState(reportId: string): ServerReportState | null {
44 return reportStateStore.get(reportId) ?? null
45}
46
47export function getSignalRegistry(reportId: string): SignalRegistry | null {
48 const state = reportStateStore.get(reportId)
49 return state?.signalRegistry ?? null
50}
51
52export function setSSEController(
53 reportId: string,
54 controller: ReadableStreamDefaultController<Uint8Array> | undefined,
55): boolean {
56 const state = reportStateStore.get(reportId)
57 if (!state) {
58 console.warn(`[report-storage] Report not found: ${reportId}`)
59 return false
60 }
61 state.sseController = controller
62 return true
63}
64
65export function getSSEController(
66 reportId: string,
67): ReadableStreamDefaultController<Uint8Array> | undefined {
68 return reportStateStore.get(reportId)?.sseController
69}
70
71// ============================================================================
72// Watcher Management
73// ============================================================================
74
75export function addWatcher(
76 reportId: string,
77 watcher: WatcherSubscription,
78): boolean {
79 const state = reportStateStore.get(reportId)
80 if (!state) {
81 console.warn(`[report-storage] Report not found: ${reportId}`)
82 return false
83 }
84 state.watchers.set(watcher.id, watcher)
85 return true
86}
87
88export function removeWatcher(reportId: string, watcherId: string): boolean {
89 const state = reportStateStore.get(reportId)
90 if (!state) {
91 console.warn(`[report-storage] Report not found: ${reportId}`)
92 return false
93 }
94 return state.watchers.delete(watcherId)
95}
96
97export function getWatcher(
98 reportId: string,
99 watcherId: string,
100): WatcherSubscription | undefined {
101 return reportStateStore.get(reportId)?.watchers.get(watcherId)
102}
103
104export function getWatchersForSignal(
105 reportId: string,
106 signal: string,
107): WatcherSubscription[] {
108 const state = reportStateStore.get(reportId)
109 if (!state) return []
110
111 const watchers: WatcherSubscription[] = []
112 for (const watcher of state.watchers.values()) {
113 if (watcher.signals.includes(signal) && !watcher.fired) {
114 watchers.push(watcher)
115 }
116 }
117 return watchers
118}
119
120export function markWatcherFired(reportId: string, watcherId: string): void {
121 const watcher = reportStateStore.get(reportId)?.watchers.get(watcherId)
122 if (watcher) {
123 watcher.fired = true
124 // Remove if it was a once-only watcher
125 if (watcher.once) {
126 reportStateStore.get(reportId)?.watchers.delete(watcherId)
127 }
128 }
129}
130
131export function getAllWatchers(reportId: string): WatcherSubscription[] {
132 const state = reportStateStore.get(reportId)
133 if (!state) return []
134 return Array.from(state.watchers.values())
135}
136
137export function applyReportUIEvent(reportId: string, event: UIEvent): boolean {
138 const state = reportStateStore.get(reportId)
139 if (!state) {
140 console.warn(`[report-storage] Report not found: ${reportId}`)
141 return false
142 }
143 const newBaseState = applyUIEvent(state, event)
144 reportStateStore.set(reportId, {
145 ...newBaseState,
146 signalRegistry: state.signalRegistry,
147 sseController: state.sseController,
148 watchers: state.watchers,
149 })
150 return true
151}
152
153export function applyReportUIUpdates(
154 reportId: string,
155 updates: UIUpdate[],
156): boolean {
157 const state = reportStateStore.get(reportId)
158 if (!state) {
159 console.warn(`[report-storage] Report not found: ${reportId}`)
160 return false
161 }
162 const newBaseState = applyUIUpdates(state, updates)
163 reportStateStore.set(reportId, {
164 ...newBaseState,
165 signalRegistry: state.signalRegistry,
166 sseController: state.sseController,
167 watchers: state.watchers,
168 })
169 return true
170}