Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

renderer.ts

393 lines10.7 KB

1import {
2 InvalidPersistedCollectionConfigError,
3 SingleProcessCoordinator,
4} from '@tanstack/db-sqlite-persistence-core'
5import { ElectronCollectionCoordinator } from './electron-coordinator'
6import {
7 DEFAULT_ELECTRON_PERSISTENCE_CHANNEL,
8 ELECTRON_PERSISTENCE_PROTOCOL_VERSION,
9} from './protocol'
10import type {
11 PersistedCollectionCoordinator,
12 PersistedCollectionMode,
13 PersistedCollectionPersistence,
14 PersistedIndexSpec,
15 PersistedTx,
16 SQLitePullSinceResult,
17} from '@tanstack/db-sqlite-persistence-core'
18import type {
19 ElectronPersistedKey,
20 ElectronPersistedRow,
21 ElectronPersistenceInvoke,
22 ElectronPersistenceMethod,
23 ElectronPersistencePayloadMap,
24 ElectronPersistenceRequest,
25 ElectronPersistenceRequestEnvelope,
26 ElectronPersistenceResolution,
27 ElectronPersistenceResponseEnvelope,
28 ElectronPersistenceResultMap,
29} from './protocol'
30import type { LoadSubsetOptions } from '@tanstack/db'
31​
32const DEFAULT_REQUEST_TIMEOUT_MS = 5_000
33let nextRequestId = 1
34​
35function createRequestId(): string {
36 const requestId = nextRequestId
37 nextRequestId++
38 return `electron-persistence-${requestId}`
40​
41function withTimeout<T>(
42 promise: Promise<T>,
43 timeoutMs: number,
44 timeoutMessage: string,
45): Promise<T> {
46 if (timeoutMs <= 0) {
47 return promise
48 }
49​
50 return new Promise<T>((resolve, reject) => {
51 const timer = setTimeout(() => {
52 reject(new InvalidPersistedCollectionConfigError(timeoutMessage))
53 }, timeoutMs)
54​
55 promise.then(
56 (value) => {
57 clearTimeout(timer)
58 resolve(value)
59 },
60 (error: unknown) => {
61 clearTimeout(timer)
62 reject(error)
63 },
64 )
65 })
67​
68function assertValidResponse(
69 response: ElectronPersistenceResponseEnvelope,
70 request: ElectronPersistenceRequestEnvelope,
71): void {
72 if (response.v !== ELECTRON_PERSISTENCE_PROTOCOL_VERSION) {
73 throw new InvalidPersistedCollectionConfigError(
74 `Unexpected electron persistence protocol version "${response.v}" in response`,
75 )
76 }
77​
78 if (response.requestId !== request.requestId) {
79 throw new InvalidPersistedCollectionConfigError(
80 `Mismatched electron persistence response request id. Expected "${request.requestId}", received "${response.requestId}"`,
81 )
82 }
83​
84 if (response.method !== request.method) {
85 throw new InvalidPersistedCollectionConfigError(
86 `Mismatched electron persistence response method. Expected "${request.method}", received "${response.method}"`,
87 )
88 }
90​
91function createSerializableLoadSubsetOptions(
92 subsetOptions: LoadSubsetOptions,
93): LoadSubsetOptions {
94 const { subscription: _subscription, ...serializableOptions } = subsetOptions
95 return serializableOptions
97​
98type RendererRequestExecutor = <TMethod extends ElectronPersistenceMethod>(
99 method: TMethod,
100 collectionId: string,
101 payload: ElectronPersistencePayloadMap[TMethod],
102 resolution?: ElectronPersistenceResolution,
103) => Promise<ElectronPersistenceResultMap[TMethod]>
104​
105function createRendererRequestExecutor(options: {
106 invoke: ElectronPersistenceInvoke
107 channel?: string
108 timeoutMs?: number
109}): RendererRequestExecutor {
110 const channel = options.channel ?? DEFAULT_ELECTRON_PERSISTENCE_CHANNEL
111 const timeoutMs = options.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS
112​
113 return async <TMethod extends ElectronPersistenceMethod>(
114 method: TMethod,
115 collectionId: string,
116 payload: ElectronPersistencePayloadMap[TMethod],
117 resolution?: ElectronPersistenceResolution,
118 ) => {
119 const request = {
120 v: ELECTRON_PERSISTENCE_PROTOCOL_VERSION,
121 requestId: createRequestId(),
122 collectionId,
123 method,
124 resolution,
125 payload,
126 } as ElectronPersistenceRequest
127​
128 const response = await withTimeout(
129 options.invoke(channel, request),
130 timeoutMs,
131 `Electron persistence request timed out (method=${method}, collection=${collectionId}, timeoutMs=${timeoutMs})`,
132 )
133 assertValidResponse(response, request)
134​
135 if (!response.ok) {
136 const remoteError = new InvalidPersistedCollectionConfigError(
137 `${response.error.name}: ${response.error.message}`,
138 )
139 if (typeof response.error.stack === `string`) {
140 remoteError.stack = response.error.stack
141 }
142 if (typeof response.error.code === `string`) {
143 ;(remoteError as Error & { code?: string }).code = response.error.code
144 }
145 throw remoteError
146 }
147​
148 return response.result as ElectronPersistenceResultMap[TMethod]
149 }
151​
152type ElectronRendererResolvedAdapter =
153 PersistedCollectionPersistence[`adapter`] & {
154 loadCollectionMetadata: (
155 collectionId: string,
156 ) => Promise<Array<{ key: string; value: unknown }>>
157 scanRows: (
158 collectionId: string,
159 options?: { metadataOnly?: boolean },
160 ) => Promise<
161 Array<{
162 key: string | number
163 value: Record<string, unknown>
164 metadata?: unknown
165 }>
166 >
167 pullSince: (
168 collectionId: string,
169 fromRowVersion: number,
170 ) => Promise<SQLitePullSinceResult<string | number>>
171 getStreamPosition: (collectionId: string) => Promise<{
172 latestTerm: number
173 latestSeq: number
174 latestRowVersion: number
175 }>
176 }
177​
178function createResolvedRendererAdapter(
179 executeRequest: RendererRequestExecutor,
180 resolution?: ElectronPersistenceResolution,
181): ElectronRendererResolvedAdapter {
182 return {
183 loadSubset: async (
184 collectionId: string,
185 subsetOptions: LoadSubsetOptions,
186 ctx?: { requiredIndexSignatures?: ReadonlyArray<string> },
187 ) => {
188 const result = await executeRequest(
189 `loadSubset`,
190 collectionId,
191 {
192 options: createSerializableLoadSubsetOptions(subsetOptions),
193 ctx,
194 },
195 resolution,
196 )
197​
198 return result as Array<{
199 key: string | number
200 value: Record<string, unknown>
201 }>
202 },
203 applyCommittedTx: async (
204 collectionId: string,
205 tx: PersistedTx<Record<string, unknown>, string | number>,
206 ): Promise<void> => {
207 await executeRequest(
208 `applyCommittedTx`,
209 collectionId,
210 {
211 tx: tx as PersistedTx<ElectronPersistedRow, ElectronPersistedKey>,
212 },
213 resolution,
214 )
215 },
216 loadCollectionMetadata: async (
217 collectionId: string,
218 ): Promise<Array<{ key: string; value: unknown }>> => {
219 return executeRequest(
220 `loadCollectionMetadata`,
221 collectionId,
222 {},
223 resolution,
224 )
225 },
226 scanRows: async (
227 collectionId: string,
228 options?: { metadataOnly?: boolean },
229 ): Promise<
230 Array<{
231 key: string | number
232 value: Record<string, unknown>
233 metadata?: unknown
234 }>
235 > => {
236 const result = await executeRequest(
237 `scanRows`,
238 collectionId,
239 { options },
240 resolution,
241 )
242 return result as Array<{
243 key: string | number
244 value: Record<string, unknown>
245 metadata?: unknown
246 }>
247 },
248 ensureIndex: async (
249 collectionId: string,
250 signature: string,
251 spec: PersistedIndexSpec,
252 ): Promise<void> => {
253 await executeRequest(
254 `ensureIndex`,
255 collectionId,
256 {
257 signature,
258 spec,
259 },
260 resolution,
261 )
262 },
263 markIndexRemoved: async (
264 collectionId: string,
265 signature: string,
266 ): Promise<void> => {
267 await executeRequest(
268 `markIndexRemoved`,
269 collectionId,
270 {
271 signature,
272 },
273 resolution,
274 )
275 },
276 pullSince: async (
277 collectionId: string,
278 fromRowVersion: number,
279 ): Promise<SQLitePullSinceResult<string | number>> => {
280 const result = await executeRequest(
281 `pullSince`,
282 collectionId,
283 {
284 fromRowVersion,
285 },
286 resolution,
287 )
288 return result as SQLitePullSinceResult<string | number>
289 },
290 getStreamPosition: async (
291 collectionId: string,
292 ): Promise<{
293 latestTerm: number
294 latestSeq: number
295 latestRowVersion: number
296 }> => {
297 return executeRequest(`getStreamPosition`, collectionId, {}, resolution)
298 },
299 }
301​
302export type ElectronIpcRendererLike = {
303 invoke: (
304 channel: string,
305 request: ElectronPersistenceRequestEnvelope,
306 ) => Promise<ElectronPersistenceResponseEnvelope>
308​
309export type ElectronSQLitePersistenceOptions = {
310 invoke?: ElectronPersistenceInvoke
311 ipcRenderer?: ElectronIpcRendererLike
312 channel?: string
313 timeoutMs?: number
314 coordinator?: PersistedCollectionCoordinator
316​
317function resolveInvoke(
318 options: ElectronSQLitePersistenceOptions,
319): ElectronPersistenceInvoke {
320 if (options.invoke) {
321 return options.invoke
322 }
323​
324 if (options.ipcRenderer) {
325 return (channel, request) => options.ipcRenderer!.invoke(channel, request)
326 }
327​
328 throw new InvalidPersistedCollectionConfigError(
329 `Electron renderer persistence requires either invoke or ipcRenderer`,
330 )
332​
333export function createElectronSQLitePersistence(
334 options: ElectronSQLitePersistenceOptions,
335): PersistedCollectionPersistence {
336 const invoke = resolveInvoke(options)
337 const coordinator = options.coordinator ?? new SingleProcessCoordinator()
338 const executeRequest = createRendererRequestExecutor({
339 invoke,
340 channel: options.channel,
341 timeoutMs: options.timeoutMs,
342 })
343 const adapterCache = new Map<string, ElectronRendererResolvedAdapter>()
344​
345 const getAdapterForCollection = (
346 mode: PersistedCollectionMode,
347 schemaVersion: number | undefined,
348 ) => {
349 const schemaVersionKey =
350 schemaVersion === undefined ? `schema:default` : `schema:${schemaVersion}`
351 const cacheKey = `mode:${mode}|${schemaVersionKey}`
352 const cachedAdapter = adapterCache.get(cacheKey)
353 if (cachedAdapter) {
354 return cachedAdapter
355 }
356​
357 const adapter = createResolvedRendererAdapter(executeRequest, {
358 mode,
359 schemaVersion,
360 })
361 adapterCache.set(cacheKey, adapter)
362​
363 // Wire the adapter into the coordinator so it can handle
364 // leader-side RPCs (applyCommittedTx, pullSince, getStreamPosition, etc.)
365 if (coordinator instanceof ElectronCollectionCoordinator) {
366 coordinator.setAdapter(adapter)
367 }
368​
369 return adapter
370 }
371​
372 const createCollectionPersistence = (
373 mode: PersistedCollectionMode,
374 schemaVersion: number | undefined,
375 ): PersistedCollectionPersistence => ({
376 adapter: getAdapterForCollection(mode, schemaVersion),
377 coordinator,
378 })
379​
380 const defaultPersistence = createCollectionPersistence(
381 `sync-absent`,
382 undefined,
383 )
384​
385 return {
386 ...defaultPersistence,
387 resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
388 createCollectionPersistence(mode, schemaVersion),
389 // Backward compatible fallback for older callers.
390 resolvePersistenceForMode: (mode) =>
391 createCollectionPersistence(mode, undefined),
392 }