Open History to see the last commit
1import type {
2 RefreshResult,
3 Report,
4 UINode,
5 UIEvent,
6 ReportState,
7 UIUpdate,
8} from './types'
9
10/**
11 * Apply a UIEvent to a ReportState, returning a new immutable state.
12 * This pure function can be used to process report events from the server.
13 */
14export function applyUIEvent(state: ReportState, event: UIEvent): ReportState {
15 const { nodes, rootIds, report } = state
16 const newNodes = new Map(nodes)
17 let newRootIds = [...rootIds]
18
19 switch (event.op) {
20 case 'add': {
21 const node: UINode = {
22 id: event.id,
23 type: event.type,
24 props: event.props,
25 children: [],
26 handlers: event.handlers,
27 subscriptions: event.subscriptions,
28 dataSource: event.dataSource,
29 }
30
31 newNodes.set(event.id, node)
32
33 if (event.parentId) {
34 const parent = newNodes.get(event.parentId)
35 if (parent) {
36 newNodes.set(event.parentId, {
37 ...parent,
38 children: [...parent.children, event.id],
39 })
40 }
41 } else {
42 newRootIds = [...newRootIds, event.id]
43 }
44 break
45 }
46
47 case 'update': {
48 const node = newNodes.get(event.id)
49 if (node) {
50 newNodes.set(event.id, {
51 ...node,
52 props: { ...node.props, ...event.props },
53 })
54 }
55 break
56 }
57
58 case 'remove': {
59 // Collect all descendant IDs recursively
60 const toRemove = new Set<string>()
61 const collect = (id: string) => {
62 toRemove.add(id)
63 const node = newNodes.get(id)
64 node?.children.forEach(collect)
65 }
66 collect(event.id)
67
68 // Remove nodes
69 toRemove.forEach((id) => newNodes.delete(id))
70
71 // Remove from parent's children
72 newNodes.forEach((node, id) => {
73 const filtered = node.children.filter(
74 (childId) => !toRemove.has(childId),
75 )
76 if (filtered.length !== node.children.length) {
77 newNodes.set(id, { ...node, children: filtered })
78 }
79 })
80
81 // Remove from root
82 newRootIds = newRootIds.filter((id) => !toRemove.has(id))
83 break
84 }
85
86 case 'reorder': {
87 if (event.parentId) {
88 const parent = newNodes.get(event.parentId)
89 if (parent) {
90 newNodes.set(event.parentId, {
91 ...parent,
92 children: event.childIds,
93 })
94 }
95 } else {
96 newRootIds = event.childIds
97 }
98 break
99 }
100 }
101
102 return {
103 report: { ...report, updatedAt: Date.now() },
104 nodes: newNodes,
105 rootIds: newRootIds,
106 }
107}
108
109/**
110 * Create an empty ReportState from a Report
111 */
112export function createEmptyReportState(report: Report): ReportState {
113 return {
114 report,
115 nodes: new Map(),
116 rootIds: [],
117 }
118}
119
120/**
121 * Apply handler-driven UI updates to a ReportState.
122 */
123export function applyUIUpdates(
124 state: ReportState,
125 updates: UIUpdate[],
126): ReportState {
127 let nextState = state
128
129 for (const update of updates) {
130 if (update.type === 'update') {
131 nextState = applyUIEvent(nextState, {
132 op: 'update',
133 id: update.componentId,
134 props: update.props ?? {},
135 })
136 } else if (update.type === 'remove') {
137 nextState = applyUIEvent(nextState, {
138 op: 'remove',
139 id: update.componentId,
140 })
141 }
142 }
143
144 return nextState
145}
146
147/**
148 * Apply refresh results from subscription invalidation.
149 * Merges the new props from each result into the corresponding component.
150 */
151export function applyRefreshResults(
152 state: ReportState,
153 results: RefreshResult[],
154): ReportState {
155 let nextState = state
156
157 for (const result of results) {
158 if (!result.success || !result.props) continue
159
160 nextState = applyUIEvent(nextState, {
161 op: 'update',
162 id: result.componentId,
163 props: result.props,
164 })
165 }
166
167 return nextState
168}