@tanstack/electron-db-sqlite-persistence 0.2.9 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -1
- package/dist/cjs/main.cjs +118 -17
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/protocol.cjs +1 -1
- package/dist/cjs/protocol.cjs.map +1 -1
- package/dist/cjs/protocol.d.cts +53 -5
- package/dist/cjs/renderer.cjs +176 -48
- package/dist/cjs/renderer.cjs.map +1 -1
- package/dist/cjs/renderer.d.cts +2 -0
- package/dist/esm/main.js +119 -18
- package/dist/esm/main.js.map +1 -1
- package/dist/esm/protocol.d.ts +53 -5
- package/dist/esm/protocol.js +1 -1
- package/dist/esm/protocol.js.map +1 -1
- package/dist/esm/renderer.d.ts +2 -0
- package/dist/esm/renderer.js +177 -49
- package/dist/esm/renderer.js.map +1 -1
- package/package.json +2 -2
- package/src/main.ts +141 -17
- package/src/protocol.ts +40 -5
- package/src/renderer.ts +229 -49
- package/dist/cjs/errors.d.cts +0 -33
- package/dist/esm/errors.d.ts +0 -33
- package/src/errors.ts +0 -110
package/src/renderer.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
InvalidPersistedCollectionConfigError,
|
|
3
3
|
SingleProcessCoordinator,
|
|
4
|
+
resolvePersistedStorageTarget,
|
|
4
5
|
} from '@tanstack/db-sqlite-persistence-core'
|
|
5
6
|
import { ElectronCollectionCoordinator } from './electron-coordinator'
|
|
6
7
|
import {
|
|
@@ -9,10 +10,12 @@ import {
|
|
|
9
10
|
} from './protocol'
|
|
10
11
|
import type {
|
|
11
12
|
CommittedTxAnchor,
|
|
13
|
+
PersistedCacheGenerationClaim,
|
|
12
14
|
PersistedCollectionCoordinator,
|
|
13
15
|
PersistedCollectionMode,
|
|
14
16
|
PersistedCollectionPersistence,
|
|
15
17
|
PersistedIndexSpec,
|
|
18
|
+
PersistedStorageTarget,
|
|
16
19
|
PersistedTx,
|
|
17
20
|
ReconciledCommittedTx,
|
|
18
21
|
SQLitePullSinceResult,
|
|
@@ -99,7 +102,7 @@ function createSerializableLoadSubsetOptions(
|
|
|
99
102
|
|
|
100
103
|
type RendererRequestExecutor = <TMethod extends ElectronPersistenceMethod>(
|
|
101
104
|
method: TMethod,
|
|
102
|
-
|
|
105
|
+
target: string | PersistedStorageTarget,
|
|
103
106
|
payload: ElectronPersistencePayloadMap[TMethod],
|
|
104
107
|
resolution?: ElectronPersistenceResolution,
|
|
105
108
|
) => Promise<ElectronPersistenceResultMap[TMethod]>
|
|
@@ -108,22 +111,49 @@ function createRendererRequestExecutor(options: {
|
|
|
108
111
|
invoke: ElectronPersistenceInvoke
|
|
109
112
|
channel?: string
|
|
110
113
|
timeoutMs?: number
|
|
114
|
+
claimCollections: Map<string, string>
|
|
111
115
|
}): RendererRequestExecutor {
|
|
112
116
|
const channel = options.channel ?? DEFAULT_ELECTRON_PERSISTENCE_CHANNEL
|
|
113
117
|
const timeoutMs = options.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS
|
|
114
118
|
|
|
115
119
|
return async <TMethod extends ElectronPersistenceMethod>(
|
|
116
120
|
method: TMethod,
|
|
117
|
-
|
|
121
|
+
target: string | PersistedStorageTarget,
|
|
118
122
|
payload: ElectronPersistencePayloadMap[TMethod],
|
|
119
123
|
resolution?: ElectronPersistenceResolution,
|
|
120
124
|
) => {
|
|
125
|
+
const storageTarget = typeof target === `string` ? undefined : target
|
|
126
|
+
const collectionId = storageTarget
|
|
127
|
+
? resolvePersistedStorageTarget(storageTarget).collectionId
|
|
128
|
+
: target
|
|
129
|
+
const isGenerationRequest =
|
|
130
|
+
method === `claimCacheGeneration` ||
|
|
131
|
+
method === `rotateCacheGeneration` ||
|
|
132
|
+
method === `renewCacheGenerationClaim` ||
|
|
133
|
+
method === `releaseCacheGenerationClaim`
|
|
134
|
+
const managedTarget = storageTarget?.kind === `managed`
|
|
135
|
+
const logicalCollectionId =
|
|
136
|
+
resolution?.logicalCollectionId ??
|
|
137
|
+
(managedTarget
|
|
138
|
+
? options.claimCollections.get(storageTarget.claimId)
|
|
139
|
+
: isGenerationRequest
|
|
140
|
+
? collectionId
|
|
141
|
+
: undefined)
|
|
142
|
+
if ((managedTarget || isGenerationRequest) && !logicalCollectionId) {
|
|
143
|
+
throw new InvalidPersistedCollectionConfigError(
|
|
144
|
+
`Electron managed cache request requires a known logical Collection ID`,
|
|
145
|
+
)
|
|
146
|
+
}
|
|
121
147
|
const request = {
|
|
122
148
|
v: ELECTRON_PERSISTENCE_PROTOCOL_VERSION,
|
|
123
149
|
requestId: createRequestId(),
|
|
124
150
|
collectionId,
|
|
151
|
+
storageTarget,
|
|
125
152
|
method,
|
|
126
|
-
resolution
|
|
153
|
+
resolution:
|
|
154
|
+
managedTarget || isGenerationRequest
|
|
155
|
+
? { ...resolution, mode: `sync-present`, logicalCollectionId }
|
|
156
|
+
: resolution,
|
|
127
157
|
payload,
|
|
128
158
|
} as ElectronPersistenceRequest
|
|
129
159
|
|
|
@@ -165,11 +195,13 @@ function createRendererRequestExecutor(options: {
|
|
|
165
195
|
type ElectronRendererResolvedAdapter =
|
|
166
196
|
PersistedCollectionPersistence[`adapter`] & {
|
|
167
197
|
loadCollectionMetadata: (
|
|
168
|
-
|
|
198
|
+
target: PersistedStorageTarget,
|
|
199
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
169
200
|
) => Promise<Array<{ key: string; value: unknown }>>
|
|
170
201
|
scanRows: (
|
|
171
|
-
|
|
202
|
+
target: PersistedStorageTarget,
|
|
172
203
|
options?: { metadataOnly?: boolean },
|
|
204
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
173
205
|
) => Promise<
|
|
174
206
|
Array<{
|
|
175
207
|
key: string | number
|
|
@@ -178,17 +210,22 @@ type ElectronRendererResolvedAdapter =
|
|
|
178
210
|
}>
|
|
179
211
|
>
|
|
180
212
|
pullSince: (
|
|
181
|
-
|
|
213
|
+
target: PersistedStorageTarget,
|
|
182
214
|
fromRowVersion: number,
|
|
215
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
183
216
|
) => Promise<SQLitePullSinceResult<string | number>>
|
|
184
|
-
getStreamPosition: (
|
|
217
|
+
getStreamPosition: (
|
|
218
|
+
target: PersistedStorageTarget,
|
|
219
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
220
|
+
) => Promise<{
|
|
185
221
|
latestTerm: number
|
|
186
222
|
latestSeq: number
|
|
187
223
|
latestRowVersion: number
|
|
188
224
|
}>
|
|
189
225
|
reserveLeadershipTerm: (
|
|
190
|
-
|
|
226
|
+
target: PersistedStorageTarget,
|
|
191
227
|
observedTerm: number,
|
|
228
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
192
229
|
) => Promise<{
|
|
193
230
|
latestTerm: number
|
|
194
231
|
latestSeq: number
|
|
@@ -198,20 +235,29 @@ type ElectronRendererResolvedAdapter =
|
|
|
198
235
|
|
|
199
236
|
function createResolvedRendererAdapter(
|
|
200
237
|
executeRequest: RendererRequestExecutor,
|
|
238
|
+
claimCollections: Map<string, string>,
|
|
239
|
+
managedCacheGenerations: boolean,
|
|
201
240
|
resolution?: ElectronPersistenceResolution,
|
|
202
241
|
): ElectronRendererResolvedAdapter {
|
|
203
|
-
|
|
242
|
+
const adapter: ElectronRendererResolvedAdapter = {
|
|
204
243
|
loadSubset: async (
|
|
205
|
-
|
|
244
|
+
target: PersistedStorageTarget,
|
|
206
245
|
subsetOptions: LoadSubsetOptions,
|
|
207
|
-
ctx?: {
|
|
246
|
+
ctx?: {
|
|
247
|
+
requiredIndexSignatures?: ReadonlyArray<string>
|
|
248
|
+
cacheGenerationClaimId?: string
|
|
249
|
+
},
|
|
208
250
|
) => {
|
|
251
|
+
const access = resolvePersistedStorageTarget(
|
|
252
|
+
target,
|
|
253
|
+
ctx?.cacheGenerationClaimId,
|
|
254
|
+
)
|
|
209
255
|
const result = await executeRequest(
|
|
210
256
|
`loadSubset`,
|
|
211
|
-
|
|
257
|
+
target,
|
|
212
258
|
{
|
|
213
259
|
options: createSerializableLoadSubsetOptions(subsetOptions),
|
|
214
|
-
ctx,
|
|
260
|
+
ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
|
|
215
261
|
},
|
|
216
262
|
resolution,
|
|
217
263
|
)
|
|
@@ -222,57 +268,79 @@ function createResolvedRendererAdapter(
|
|
|
222
268
|
}>
|
|
223
269
|
},
|
|
224
270
|
loadResumeSnapshot: async (
|
|
225
|
-
|
|
271
|
+
target: PersistedStorageTarget,
|
|
226
272
|
ctx?: {
|
|
227
273
|
requiredIndexSignatures?: ReadonlyArray<string>
|
|
228
274
|
includeRows?: boolean
|
|
275
|
+
cacheGenerationClaimId?: string
|
|
229
276
|
},
|
|
230
277
|
) => {
|
|
278
|
+
const access = resolvePersistedStorageTarget(
|
|
279
|
+
target,
|
|
280
|
+
ctx?.cacheGenerationClaimId,
|
|
281
|
+
)
|
|
231
282
|
return executeRequest(
|
|
232
283
|
`loadResumeSnapshot`,
|
|
233
|
-
|
|
234
|
-
{ ctx },
|
|
284
|
+
target,
|
|
285
|
+
{ ctx: { ...ctx, cacheGenerationClaimId: access.claimId } },
|
|
235
286
|
resolution,
|
|
236
287
|
)
|
|
237
288
|
},
|
|
238
289
|
applyCommittedTx: async (
|
|
239
|
-
|
|
290
|
+
target: PersistedStorageTarget,
|
|
240
291
|
tx: PersistedTx<Record<string, unknown>, string | number>,
|
|
241
292
|
): Promise<void> => {
|
|
293
|
+
const access = resolvePersistedStorageTarget(
|
|
294
|
+
target,
|
|
295
|
+
tx.cacheGenerationClaimId,
|
|
296
|
+
)
|
|
242
297
|
await executeRequest(
|
|
243
298
|
`applyCommittedTx`,
|
|
244
|
-
|
|
299
|
+
target,
|
|
245
300
|
{
|
|
246
|
-
tx: tx as PersistedTx<
|
|
301
|
+
tx: { ...tx, cacheGenerationClaimId: access.claimId } as PersistedTx<
|
|
302
|
+
ElectronPersistedRow,
|
|
303
|
+
ElectronPersistedKey
|
|
304
|
+
>,
|
|
247
305
|
},
|
|
248
306
|
resolution,
|
|
249
307
|
)
|
|
250
308
|
},
|
|
251
309
|
reconcileCommittedTx: async (
|
|
252
|
-
|
|
310
|
+
target: PersistedStorageTarget,
|
|
253
311
|
tx: PersistedTx<Record<string, unknown>, string | number>,
|
|
254
312
|
anchor: CommittedTxAnchor,
|
|
255
313
|
): Promise<ReconciledCommittedTx> => {
|
|
314
|
+
const access = resolvePersistedStorageTarget(
|
|
315
|
+
target,
|
|
316
|
+
tx.cacheGenerationClaimId,
|
|
317
|
+
)
|
|
256
318
|
return executeRequest(
|
|
257
319
|
`reconcileCommittedTx`,
|
|
258
|
-
|
|
259
|
-
{ tx, anchor },
|
|
320
|
+
target,
|
|
321
|
+
{ tx: { ...tx, cacheGenerationClaimId: access.claimId }, anchor },
|
|
260
322
|
resolution,
|
|
261
323
|
)
|
|
262
324
|
},
|
|
263
325
|
loadCollectionMetadata: async (
|
|
264
|
-
|
|
326
|
+
target: PersistedStorageTarget,
|
|
327
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
265
328
|
): Promise<Array<{ key: string; value: unknown }>> => {
|
|
329
|
+
const access = resolvePersistedStorageTarget(
|
|
330
|
+
target,
|
|
331
|
+
ctx?.cacheGenerationClaimId,
|
|
332
|
+
)
|
|
266
333
|
return executeRequest(
|
|
267
334
|
`loadCollectionMetadata`,
|
|
268
|
-
|
|
269
|
-
{},
|
|
335
|
+
target,
|
|
336
|
+
{ ctx: { ...ctx, cacheGenerationClaimId: access.claimId } },
|
|
270
337
|
resolution,
|
|
271
338
|
)
|
|
272
339
|
},
|
|
273
340
|
scanRows: async (
|
|
274
|
-
|
|
341
|
+
target: PersistedStorageTarget,
|
|
275
342
|
options?: { metadataOnly?: boolean },
|
|
343
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
276
344
|
): Promise<
|
|
277
345
|
Array<{
|
|
278
346
|
key: string | number
|
|
@@ -280,10 +348,14 @@ function createResolvedRendererAdapter(
|
|
|
280
348
|
metadata?: unknown
|
|
281
349
|
}>
|
|
282
350
|
> => {
|
|
351
|
+
const access = resolvePersistedStorageTarget(
|
|
352
|
+
target,
|
|
353
|
+
ctx?.cacheGenerationClaimId,
|
|
354
|
+
)
|
|
283
355
|
const result = await executeRequest(
|
|
284
356
|
`scanRows`,
|
|
285
|
-
|
|
286
|
-
{ options },
|
|
357
|
+
target,
|
|
358
|
+
{ options, ctx: { ...ctx, cacheGenerationClaimId: access.claimId } },
|
|
287
359
|
resolution,
|
|
288
360
|
)
|
|
289
361
|
return result as Array<{
|
|
@@ -293,64 +365,167 @@ function createResolvedRendererAdapter(
|
|
|
293
365
|
}>
|
|
294
366
|
},
|
|
295
367
|
ensureIndex: async (
|
|
296
|
-
|
|
368
|
+
target: PersistedStorageTarget,
|
|
297
369
|
signature: string,
|
|
298
370
|
spec: PersistedIndexSpec,
|
|
371
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
299
372
|
): Promise<void> => {
|
|
373
|
+
const access = resolvePersistedStorageTarget(
|
|
374
|
+
target,
|
|
375
|
+
ctx?.cacheGenerationClaimId,
|
|
376
|
+
)
|
|
300
377
|
await executeRequest(
|
|
301
378
|
`ensureIndex`,
|
|
302
|
-
|
|
379
|
+
target,
|
|
303
380
|
{
|
|
304
381
|
signature,
|
|
305
382
|
spec,
|
|
383
|
+
ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
|
|
306
384
|
},
|
|
307
385
|
resolution,
|
|
308
386
|
)
|
|
309
387
|
},
|
|
310
388
|
markIndexRemoved: async (
|
|
311
|
-
|
|
389
|
+
target: PersistedStorageTarget,
|
|
312
390
|
signature: string,
|
|
391
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
313
392
|
): Promise<void> => {
|
|
393
|
+
const access = resolvePersistedStorageTarget(
|
|
394
|
+
target,
|
|
395
|
+
ctx?.cacheGenerationClaimId,
|
|
396
|
+
)
|
|
314
397
|
await executeRequest(
|
|
315
398
|
`markIndexRemoved`,
|
|
316
|
-
|
|
399
|
+
target,
|
|
317
400
|
{
|
|
318
401
|
signature,
|
|
402
|
+
ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
|
|
319
403
|
},
|
|
320
404
|
resolution,
|
|
321
405
|
)
|
|
322
406
|
},
|
|
323
407
|
pullSince: async (
|
|
324
|
-
|
|
408
|
+
target: PersistedStorageTarget,
|
|
325
409
|
fromRowVersion: number,
|
|
410
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
326
411
|
): Promise<SQLitePullSinceResult<string | number>> => {
|
|
412
|
+
const access = resolvePersistedStorageTarget(
|
|
413
|
+
target,
|
|
414
|
+
ctx?.cacheGenerationClaimId,
|
|
415
|
+
)
|
|
327
416
|
const result = await executeRequest(
|
|
328
417
|
`pullSince`,
|
|
329
|
-
|
|
418
|
+
target,
|
|
330
419
|
{
|
|
331
420
|
fromRowVersion,
|
|
421
|
+
ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
|
|
332
422
|
},
|
|
333
423
|
resolution,
|
|
334
424
|
)
|
|
335
425
|
return result as SQLitePullSinceResult<string | number>
|
|
336
426
|
},
|
|
337
427
|
getStreamPosition: async (
|
|
338
|
-
|
|
428
|
+
target: PersistedStorageTarget,
|
|
429
|
+
ctx?: { cacheGenerationClaimId?: string },
|
|
339
430
|
): Promise<{
|
|
340
431
|
latestTerm: number
|
|
341
432
|
latestSeq: number
|
|
342
433
|
latestRowVersion: number
|
|
343
434
|
}> => {
|
|
344
|
-
|
|
435
|
+
const access = resolvePersistedStorageTarget(
|
|
436
|
+
target,
|
|
437
|
+
ctx?.cacheGenerationClaimId,
|
|
438
|
+
)
|
|
439
|
+
return executeRequest(
|
|
440
|
+
`getStreamPosition`,
|
|
441
|
+
target,
|
|
442
|
+
{ ctx: { ...ctx, cacheGenerationClaimId: access.claimId } },
|
|
443
|
+
resolution,
|
|
444
|
+
)
|
|
345
445
|
},
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
446
|
+
claimCacheGeneration: async (
|
|
447
|
+
collectionId: string,
|
|
448
|
+
): Promise<PersistedCacheGenerationClaim> => {
|
|
449
|
+
const claim = await executeRequest(
|
|
450
|
+
`claimCacheGeneration`,
|
|
451
|
+
collectionId,
|
|
452
|
+
{},
|
|
453
|
+
resolution,
|
|
454
|
+
)
|
|
455
|
+
claimCollections.set(claim.claimId, collectionId)
|
|
456
|
+
return claim
|
|
457
|
+
},
|
|
458
|
+
rotateCacheGeneration: async (
|
|
459
|
+
collectionId: string,
|
|
460
|
+
claimId: string,
|
|
461
|
+
resetMetadata?: { key: string; value: unknown },
|
|
462
|
+
expectedStorageCollectionId?: string,
|
|
463
|
+
): Promise<PersistedCacheGenerationClaim> => {
|
|
464
|
+
const claim = await executeRequest(
|
|
465
|
+
`rotateCacheGeneration`,
|
|
466
|
+
collectionId,
|
|
467
|
+
{ claimId, resetMetadata, expectedStorageCollectionId },
|
|
468
|
+
resolution,
|
|
469
|
+
)
|
|
470
|
+
claimCollections.delete(claimId)
|
|
471
|
+
claimCollections.set(claim.claimId, collectionId)
|
|
472
|
+
return claim
|
|
473
|
+
},
|
|
474
|
+
renewCacheGenerationClaim: (
|
|
475
|
+
storageCollectionId: string,
|
|
476
|
+
claimId: string,
|
|
477
|
+
): Promise<number | undefined> => {
|
|
478
|
+
const collectionId = claimCollections.get(claimId)
|
|
479
|
+
if (!collectionId) {
|
|
480
|
+
throw new InvalidPersistedCollectionConfigError(
|
|
481
|
+
`Unknown electron persisted cache claim "${claimId}"`,
|
|
482
|
+
)
|
|
483
|
+
}
|
|
484
|
+
return executeRequest(
|
|
485
|
+
`renewCacheGenerationClaim`,
|
|
349
486
|
collectionId,
|
|
350
|
-
{
|
|
487
|
+
{ storageCollectionId, claimId },
|
|
351
488
|
resolution,
|
|
352
|
-
)
|
|
489
|
+
)
|
|
490
|
+
},
|
|
491
|
+
releaseCacheGenerationClaim: async (claimId: string): Promise<void> => {
|
|
492
|
+
const collectionId = claimCollections.get(claimId)
|
|
493
|
+
if (!collectionId) {
|
|
494
|
+
throw new InvalidPersistedCollectionConfigError(
|
|
495
|
+
`Unknown electron persisted cache claim "${claimId}"`,
|
|
496
|
+
)
|
|
497
|
+
}
|
|
498
|
+
await executeRequest(
|
|
499
|
+
`releaseCacheGenerationClaim`,
|
|
500
|
+
collectionId,
|
|
501
|
+
{ claimId },
|
|
502
|
+
resolution,
|
|
503
|
+
)
|
|
504
|
+
claimCollections.delete(claimId)
|
|
505
|
+
},
|
|
506
|
+
reserveLeadershipTerm: async (target, observedTerm, ctx) => {
|
|
507
|
+
const access = resolvePersistedStorageTarget(
|
|
508
|
+
target,
|
|
509
|
+
ctx?.cacheGenerationClaimId,
|
|
510
|
+
)
|
|
511
|
+
return executeRequest(
|
|
512
|
+
`reserveLeadershipTerm`,
|
|
513
|
+
target,
|
|
514
|
+
{
|
|
515
|
+
observedTerm,
|
|
516
|
+
ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
|
|
517
|
+
},
|
|
518
|
+
resolution,
|
|
519
|
+
)
|
|
520
|
+
},
|
|
521
|
+
}
|
|
522
|
+
if (!managedCacheGenerations) {
|
|
523
|
+
adapter.claimCacheGeneration = undefined
|
|
524
|
+
adapter.rotateCacheGeneration = undefined
|
|
525
|
+
adapter.renewCacheGenerationClaim = undefined
|
|
526
|
+
adapter.releaseCacheGenerationClaim = undefined
|
|
353
527
|
}
|
|
528
|
+
return adapter
|
|
354
529
|
}
|
|
355
530
|
|
|
356
531
|
export type ElectronIpcRendererLike = {
|
|
@@ -366,6 +541,8 @@ export type ElectronSQLitePersistenceOptions = {
|
|
|
366
541
|
channel?: string
|
|
367
542
|
timeoutMs?: number
|
|
368
543
|
coordinator?: PersistedCollectionCoordinator
|
|
544
|
+
/** Set false when a custom main-process adapter does not manage cache generations. */
|
|
545
|
+
managedCacheGenerations?: boolean
|
|
369
546
|
}
|
|
370
547
|
|
|
371
548
|
function resolveInvoke(
|
|
@@ -389,29 +566,32 @@ export function createElectronSQLitePersistence(
|
|
|
389
566
|
): PersistedCollectionPersistence {
|
|
390
567
|
const invoke = resolveInvoke(options)
|
|
391
568
|
const coordinator = options.coordinator ?? new SingleProcessCoordinator()
|
|
569
|
+
const claimCollections = new Map<string, string>()
|
|
392
570
|
const executeRequest = createRendererRequestExecutor({
|
|
393
571
|
invoke,
|
|
394
572
|
channel: options.channel,
|
|
395
573
|
timeoutMs: options.timeoutMs,
|
|
574
|
+
claimCollections,
|
|
396
575
|
})
|
|
397
576
|
const adapterCache = new Map<string, ElectronRendererResolvedAdapter>()
|
|
398
577
|
|
|
399
578
|
const getAdapterForCollection = (
|
|
579
|
+
collectionId: string | undefined,
|
|
400
580
|
mode: PersistedCollectionMode,
|
|
401
581
|
schemaVersion: number | undefined,
|
|
402
582
|
) => {
|
|
403
|
-
const
|
|
404
|
-
schemaVersion === undefined ? `schema:default` : `schema:${schemaVersion}`
|
|
405
|
-
const cacheKey = `mode:${mode}|${schemaVersionKey}`
|
|
583
|
+
const cacheKey = JSON.stringify([collectionId, mode, schemaVersion])
|
|
406
584
|
const cachedAdapter = adapterCache.get(cacheKey)
|
|
407
585
|
if (cachedAdapter) {
|
|
408
586
|
return cachedAdapter
|
|
409
587
|
}
|
|
410
588
|
|
|
411
|
-
const adapter = createResolvedRendererAdapter(
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
589
|
+
const adapter = createResolvedRendererAdapter(
|
|
590
|
+
executeRequest,
|
|
591
|
+
claimCollections,
|
|
592
|
+
options.managedCacheGenerations ?? true,
|
|
593
|
+
{ mode, schemaVersion, logicalCollectionId: collectionId },
|
|
594
|
+
)
|
|
415
595
|
adapterCache.set(cacheKey, adapter)
|
|
416
596
|
return adapter
|
|
417
597
|
}
|
|
@@ -421,7 +601,7 @@ export function createElectronSQLitePersistence(
|
|
|
421
601
|
mode: PersistedCollectionMode,
|
|
422
602
|
schemaVersion: number | undefined,
|
|
423
603
|
): PersistedCollectionPersistence => {
|
|
424
|
-
const adapter = getAdapterForCollection(mode, schemaVersion)
|
|
604
|
+
const adapter = getAdapterForCollection(collectionId, mode, schemaVersion)
|
|
425
605
|
if (coordinator instanceof ElectronCollectionCoordinator) {
|
|
426
606
|
if (collectionId === undefined) {
|
|
427
607
|
coordinator.setAdapter(adapter)
|
package/dist/cjs/errors.d.cts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { ElectronPersistenceMethod, ElectronSerializedError } from './protocol.cjs';
|
|
2
|
-
type ElectronPersistenceErrorOptions = {
|
|
3
|
-
code?: string | number;
|
|
4
|
-
cause?: unknown;
|
|
5
|
-
};
|
|
6
|
-
export declare class ElectronPersistenceError extends Error {
|
|
7
|
-
readonly code: string | number | undefined;
|
|
8
|
-
constructor(message: string, options?: ElectronPersistenceErrorOptions);
|
|
9
|
-
}
|
|
10
|
-
export declare class UnknownElectronPersistenceCollectionError extends ElectronPersistenceError {
|
|
11
|
-
readonly collectionId: string;
|
|
12
|
-
constructor(collectionId: string);
|
|
13
|
-
}
|
|
14
|
-
export declare class UnsupportedElectronPersistenceMethodError extends ElectronPersistenceError {
|
|
15
|
-
readonly method: ElectronPersistenceMethod;
|
|
16
|
-
readonly collectionId: string;
|
|
17
|
-
constructor(method: ElectronPersistenceMethod, collectionId: string);
|
|
18
|
-
}
|
|
19
|
-
export declare class ElectronPersistenceProtocolError extends ElectronPersistenceError {
|
|
20
|
-
constructor(message: string, options?: ElectronPersistenceErrorOptions);
|
|
21
|
-
}
|
|
22
|
-
export declare class ElectronPersistenceTimeoutError extends ElectronPersistenceError {
|
|
23
|
-
constructor(message: string);
|
|
24
|
-
}
|
|
25
|
-
export declare class ElectronPersistenceRpcError extends ElectronPersistenceError {
|
|
26
|
-
readonly method: ElectronPersistenceMethod;
|
|
27
|
-
readonly collectionId: string;
|
|
28
|
-
readonly requestId: string;
|
|
29
|
-
readonly remoteName: string;
|
|
30
|
-
constructor(method: ElectronPersistenceMethod, collectionId: string, requestId: string, serializedError: ElectronSerializedError);
|
|
31
|
-
static fromSerialized(method: ElectronPersistenceMethod, collectionId: string, requestId: string, serializedError: ElectronSerializedError): ElectronPersistenceRpcError;
|
|
32
|
-
}
|
|
33
|
-
export {};
|
package/dist/esm/errors.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { ElectronPersistenceMethod, ElectronSerializedError } from './protocol.js';
|
|
2
|
-
type ElectronPersistenceErrorOptions = {
|
|
3
|
-
code?: string | number;
|
|
4
|
-
cause?: unknown;
|
|
5
|
-
};
|
|
6
|
-
export declare class ElectronPersistenceError extends Error {
|
|
7
|
-
readonly code: string | number | undefined;
|
|
8
|
-
constructor(message: string, options?: ElectronPersistenceErrorOptions);
|
|
9
|
-
}
|
|
10
|
-
export declare class UnknownElectronPersistenceCollectionError extends ElectronPersistenceError {
|
|
11
|
-
readonly collectionId: string;
|
|
12
|
-
constructor(collectionId: string);
|
|
13
|
-
}
|
|
14
|
-
export declare class UnsupportedElectronPersistenceMethodError extends ElectronPersistenceError {
|
|
15
|
-
readonly method: ElectronPersistenceMethod;
|
|
16
|
-
readonly collectionId: string;
|
|
17
|
-
constructor(method: ElectronPersistenceMethod, collectionId: string);
|
|
18
|
-
}
|
|
19
|
-
export declare class ElectronPersistenceProtocolError extends ElectronPersistenceError {
|
|
20
|
-
constructor(message: string, options?: ElectronPersistenceErrorOptions);
|
|
21
|
-
}
|
|
22
|
-
export declare class ElectronPersistenceTimeoutError extends ElectronPersistenceError {
|
|
23
|
-
constructor(message: string);
|
|
24
|
-
}
|
|
25
|
-
export declare class ElectronPersistenceRpcError extends ElectronPersistenceError {
|
|
26
|
-
readonly method: ElectronPersistenceMethod;
|
|
27
|
-
readonly collectionId: string;
|
|
28
|
-
readonly requestId: string;
|
|
29
|
-
readonly remoteName: string;
|
|
30
|
-
constructor(method: ElectronPersistenceMethod, collectionId: string, requestId: string, serializedError: ElectronSerializedError);
|
|
31
|
-
static fromSerialized(method: ElectronPersistenceMethod, collectionId: string, requestId: string, serializedError: ElectronSerializedError): ElectronPersistenceRpcError;
|
|
32
|
-
}
|
|
33
|
-
export {};
|
package/src/errors.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ElectronPersistenceMethod,
|
|
3
|
-
ElectronSerializedError,
|
|
4
|
-
} from './protocol'
|
|
5
|
-
|
|
6
|
-
type ElectronPersistenceErrorOptions = {
|
|
7
|
-
code?: string | number
|
|
8
|
-
cause?: unknown
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class ElectronPersistenceError extends Error {
|
|
12
|
-
readonly code: string | number | undefined
|
|
13
|
-
|
|
14
|
-
constructor(message: string, options?: ElectronPersistenceErrorOptions) {
|
|
15
|
-
super(message, { cause: options?.cause })
|
|
16
|
-
this.name = `ElectronPersistenceError`
|
|
17
|
-
this.code = options?.code
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class UnknownElectronPersistenceCollectionError extends ElectronPersistenceError {
|
|
22
|
-
readonly collectionId: string
|
|
23
|
-
|
|
24
|
-
constructor(collectionId: string) {
|
|
25
|
-
super(
|
|
26
|
-
`Unknown electron persistence collection "${collectionId}". Register the collection adapter in the main process host.`,
|
|
27
|
-
{
|
|
28
|
-
code: `UNKNOWN_COLLECTION`,
|
|
29
|
-
},
|
|
30
|
-
)
|
|
31
|
-
this.name = `UnknownElectronPersistenceCollectionError`
|
|
32
|
-
this.collectionId = collectionId
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class UnsupportedElectronPersistenceMethodError extends ElectronPersistenceError {
|
|
37
|
-
readonly method: ElectronPersistenceMethod
|
|
38
|
-
readonly collectionId: string
|
|
39
|
-
|
|
40
|
-
constructor(method: ElectronPersistenceMethod, collectionId: string) {
|
|
41
|
-
super(
|
|
42
|
-
`Method "${method}" is not supported by the electron persistence adapter for collection "${collectionId}".`,
|
|
43
|
-
{
|
|
44
|
-
code: `UNSUPPORTED_METHOD`,
|
|
45
|
-
},
|
|
46
|
-
)
|
|
47
|
-
this.name = `UnsupportedElectronPersistenceMethodError`
|
|
48
|
-
this.method = method
|
|
49
|
-
this.collectionId = collectionId
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export class ElectronPersistenceProtocolError extends ElectronPersistenceError {
|
|
54
|
-
constructor(message: string, options?: ElectronPersistenceErrorOptions) {
|
|
55
|
-
super(message, {
|
|
56
|
-
code: options?.code ?? `INVALID_PROTOCOL`,
|
|
57
|
-
cause: options?.cause,
|
|
58
|
-
})
|
|
59
|
-
this.name = `ElectronPersistenceProtocolError`
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export class ElectronPersistenceTimeoutError extends ElectronPersistenceError {
|
|
64
|
-
constructor(message: string) {
|
|
65
|
-
super(message, {
|
|
66
|
-
code: `TIMEOUT`,
|
|
67
|
-
})
|
|
68
|
-
this.name = `ElectronPersistenceTimeoutError`
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export class ElectronPersistenceRpcError extends ElectronPersistenceError {
|
|
73
|
-
readonly method: ElectronPersistenceMethod
|
|
74
|
-
readonly collectionId: string
|
|
75
|
-
readonly requestId: string
|
|
76
|
-
readonly remoteName: string
|
|
77
|
-
|
|
78
|
-
constructor(
|
|
79
|
-
method: ElectronPersistenceMethod,
|
|
80
|
-
collectionId: string,
|
|
81
|
-
requestId: string,
|
|
82
|
-
serializedError: ElectronSerializedError,
|
|
83
|
-
) {
|
|
84
|
-
super(
|
|
85
|
-
`${serializedError.name}: ${serializedError.message} (method=${method}, collection=${collectionId}, request=${requestId})`,
|
|
86
|
-
{
|
|
87
|
-
code: serializedError.code ?? `REMOTE_ERROR`,
|
|
88
|
-
},
|
|
89
|
-
)
|
|
90
|
-
this.name = `ElectronPersistenceRpcError`
|
|
91
|
-
this.method = method
|
|
92
|
-
this.collectionId = collectionId
|
|
93
|
-
this.requestId = requestId
|
|
94
|
-
this.remoteName = serializedError.name
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
static fromSerialized(
|
|
98
|
-
method: ElectronPersistenceMethod,
|
|
99
|
-
collectionId: string,
|
|
100
|
-
requestId: string,
|
|
101
|
-
serializedError: ElectronSerializedError,
|
|
102
|
-
): ElectronPersistenceRpcError {
|
|
103
|
-
return new ElectronPersistenceRpcError(
|
|
104
|
-
method,
|
|
105
|
-
collectionId,
|
|
106
|
-
requestId,
|
|
107
|
-
serializedError,
|
|
108
|
-
)
|
|
109
|
-
}
|
|
110
|
-
}
|