@tanstack/electron-db-sqlite-persistence 0.2.8 → 0.2.10

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/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 {
@@ -8,11 +9,15 @@ import {
8
9
  ELECTRON_PERSISTENCE_PROTOCOL_VERSION,
9
10
  } from './protocol'
10
11
  import type {
12
+ CommittedTxAnchor,
13
+ PersistedCacheGenerationClaim,
11
14
  PersistedCollectionCoordinator,
12
15
  PersistedCollectionMode,
13
16
  PersistedCollectionPersistence,
14
17
  PersistedIndexSpec,
18
+ PersistedStorageTarget,
15
19
  PersistedTx,
20
+ ReconciledCommittedTx,
16
21
  SQLitePullSinceResult,
17
22
  } from '@tanstack/db-sqlite-persistence-core'
18
23
  import type {
@@ -97,7 +102,7 @@ function createSerializableLoadSubsetOptions(
97
102
 
98
103
  type RendererRequestExecutor = <TMethod extends ElectronPersistenceMethod>(
99
104
  method: TMethod,
100
- collectionId: string,
105
+ target: string | PersistedStorageTarget,
101
106
  payload: ElectronPersistencePayloadMap[TMethod],
102
107
  resolution?: ElectronPersistenceResolution,
103
108
  ) => Promise<ElectronPersistenceResultMap[TMethod]>
@@ -106,22 +111,49 @@ function createRendererRequestExecutor(options: {
106
111
  invoke: ElectronPersistenceInvoke
107
112
  channel?: string
108
113
  timeoutMs?: number
114
+ claimCollections: Map<string, string>
109
115
  }): RendererRequestExecutor {
110
116
  const channel = options.channel ?? DEFAULT_ELECTRON_PERSISTENCE_CHANNEL
111
117
  const timeoutMs = options.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS
112
118
 
113
119
  return async <TMethod extends ElectronPersistenceMethod>(
114
120
  method: TMethod,
115
- collectionId: string,
121
+ target: string | PersistedStorageTarget,
116
122
  payload: ElectronPersistencePayloadMap[TMethod],
117
123
  resolution?: ElectronPersistenceResolution,
118
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
+ }
119
147
  const request = {
120
148
  v: ELECTRON_PERSISTENCE_PROTOCOL_VERSION,
121
149
  requestId: createRequestId(),
122
150
  collectionId,
151
+ storageTarget,
123
152
  method,
124
- resolution,
153
+ resolution:
154
+ managedTarget || isGenerationRequest
155
+ ? { ...resolution, mode: `sync-present`, logicalCollectionId }
156
+ : resolution,
125
157
  payload,
126
158
  } as ElectronPersistenceRequest
127
159
 
@@ -139,8 +171,19 @@ function createRendererRequestExecutor(options: {
139
171
  if (typeof response.error.stack === `string`) {
140
172
  remoteError.stack = response.error.stack
141
173
  }
142
- if (typeof response.error.code === `string`) {
143
- ;(remoteError as Error & { code?: string }).code = response.error.code
174
+ if (
175
+ typeof response.error.code === `string` ||
176
+ typeof response.error.code === `number`
177
+ ) {
178
+ ;(remoteError as Error & { code?: string | number }).code =
179
+ response.error.code
180
+ }
181
+ if (response.error.path !== undefined) {
182
+ ;(
183
+ remoteError as Error & {
184
+ path?: string | ReadonlyArray<string | number>
185
+ }
186
+ ).path = response.error.path
144
187
  }
145
188
  throw remoteError
146
189
  }
@@ -152,11 +195,13 @@ function createRendererRequestExecutor(options: {
152
195
  type ElectronRendererResolvedAdapter =
153
196
  PersistedCollectionPersistence[`adapter`] & {
154
197
  loadCollectionMetadata: (
155
- collectionId: string,
198
+ target: PersistedStorageTarget,
199
+ ctx?: { cacheGenerationClaimId?: string },
156
200
  ) => Promise<Array<{ key: string; value: unknown }>>
157
201
  scanRows: (
158
- collectionId: string,
202
+ target: PersistedStorageTarget,
159
203
  options?: { metadataOnly?: boolean },
204
+ ctx?: { cacheGenerationClaimId?: string },
160
205
  ) => Promise<
161
206
  Array<{
162
207
  key: string | number
@@ -165,10 +210,23 @@ type ElectronRendererResolvedAdapter =
165
210
  }>
166
211
  >
167
212
  pullSince: (
168
- collectionId: string,
213
+ target: PersistedStorageTarget,
169
214
  fromRowVersion: number,
215
+ ctx?: { cacheGenerationClaimId?: string },
170
216
  ) => Promise<SQLitePullSinceResult<string | number>>
171
- getStreamPosition: (collectionId: string) => Promise<{
217
+ getStreamPosition: (
218
+ target: PersistedStorageTarget,
219
+ ctx?: { cacheGenerationClaimId?: string },
220
+ ) => Promise<{
221
+ latestTerm: number
222
+ latestSeq: number
223
+ latestRowVersion: number
224
+ }>
225
+ reserveLeadershipTerm: (
226
+ target: PersistedStorageTarget,
227
+ observedTerm: number,
228
+ ctx?: { cacheGenerationClaimId?: string },
229
+ ) => Promise<{
172
230
  latestTerm: number
173
231
  latestSeq: number
174
232
  latestRowVersion: number
@@ -177,20 +235,29 @@ type ElectronRendererResolvedAdapter =
177
235
 
178
236
  function createResolvedRendererAdapter(
179
237
  executeRequest: RendererRequestExecutor,
238
+ claimCollections: Map<string, string>,
239
+ managedCacheGenerations: boolean,
180
240
  resolution?: ElectronPersistenceResolution,
181
241
  ): ElectronRendererResolvedAdapter {
182
- return {
242
+ const adapter: ElectronRendererResolvedAdapter = {
183
243
  loadSubset: async (
184
- collectionId: string,
244
+ target: PersistedStorageTarget,
185
245
  subsetOptions: LoadSubsetOptions,
186
- ctx?: { requiredIndexSignatures?: ReadonlyArray<string> },
246
+ ctx?: {
247
+ requiredIndexSignatures?: ReadonlyArray<string>
248
+ cacheGenerationClaimId?: string
249
+ },
187
250
  ) => {
251
+ const access = resolvePersistedStorageTarget(
252
+ target,
253
+ ctx?.cacheGenerationClaimId,
254
+ )
188
255
  const result = await executeRequest(
189
256
  `loadSubset`,
190
- collectionId,
257
+ target,
191
258
  {
192
259
  options: createSerializableLoadSubsetOptions(subsetOptions),
193
- ctx,
260
+ ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
194
261
  },
195
262
  resolution,
196
263
  )
@@ -201,45 +268,79 @@ function createResolvedRendererAdapter(
201
268
  }>
202
269
  },
203
270
  loadResumeSnapshot: async (
204
- collectionId: string,
271
+ target: PersistedStorageTarget,
205
272
  ctx?: {
206
273
  requiredIndexSignatures?: ReadonlyArray<string>
207
274
  includeRows?: boolean
275
+ cacheGenerationClaimId?: string
208
276
  },
209
277
  ) => {
278
+ const access = resolvePersistedStorageTarget(
279
+ target,
280
+ ctx?.cacheGenerationClaimId,
281
+ )
210
282
  return executeRequest(
211
283
  `loadResumeSnapshot`,
212
- collectionId,
213
- { ctx },
284
+ target,
285
+ { ctx: { ...ctx, cacheGenerationClaimId: access.claimId } },
214
286
  resolution,
215
287
  )
216
288
  },
217
289
  applyCommittedTx: async (
218
- collectionId: string,
290
+ target: PersistedStorageTarget,
219
291
  tx: PersistedTx<Record<string, unknown>, string | number>,
220
292
  ): Promise<void> => {
293
+ const access = resolvePersistedStorageTarget(
294
+ target,
295
+ tx.cacheGenerationClaimId,
296
+ )
221
297
  await executeRequest(
222
298
  `applyCommittedTx`,
223
- collectionId,
299
+ target,
224
300
  {
225
- tx: tx as PersistedTx<ElectronPersistedRow, ElectronPersistedKey>,
301
+ tx: { ...tx, cacheGenerationClaimId: access.claimId } as PersistedTx<
302
+ ElectronPersistedRow,
303
+ ElectronPersistedKey
304
+ >,
226
305
  },
227
306
  resolution,
228
307
  )
229
308
  },
309
+ reconcileCommittedTx: async (
310
+ target: PersistedStorageTarget,
311
+ tx: PersistedTx<Record<string, unknown>, string | number>,
312
+ anchor: CommittedTxAnchor,
313
+ ): Promise<ReconciledCommittedTx> => {
314
+ const access = resolvePersistedStorageTarget(
315
+ target,
316
+ tx.cacheGenerationClaimId,
317
+ )
318
+ return executeRequest(
319
+ `reconcileCommittedTx`,
320
+ target,
321
+ { tx: { ...tx, cacheGenerationClaimId: access.claimId }, anchor },
322
+ resolution,
323
+ )
324
+ },
230
325
  loadCollectionMetadata: async (
231
- collectionId: string,
326
+ target: PersistedStorageTarget,
327
+ ctx?: { cacheGenerationClaimId?: string },
232
328
  ): Promise<Array<{ key: string; value: unknown }>> => {
329
+ const access = resolvePersistedStorageTarget(
330
+ target,
331
+ ctx?.cacheGenerationClaimId,
332
+ )
233
333
  return executeRequest(
234
334
  `loadCollectionMetadata`,
235
- collectionId,
236
- {},
335
+ target,
336
+ { ctx: { ...ctx, cacheGenerationClaimId: access.claimId } },
237
337
  resolution,
238
338
  )
239
339
  },
240
340
  scanRows: async (
241
- collectionId: string,
341
+ target: PersistedStorageTarget,
242
342
  options?: { metadataOnly?: boolean },
343
+ ctx?: { cacheGenerationClaimId?: string },
243
344
  ): Promise<
244
345
  Array<{
245
346
  key: string | number
@@ -247,10 +348,14 @@ function createResolvedRendererAdapter(
247
348
  metadata?: unknown
248
349
  }>
249
350
  > => {
351
+ const access = resolvePersistedStorageTarget(
352
+ target,
353
+ ctx?.cacheGenerationClaimId,
354
+ )
250
355
  const result = await executeRequest(
251
356
  `scanRows`,
252
- collectionId,
253
- { options },
357
+ target,
358
+ { options, ctx: { ...ctx, cacheGenerationClaimId: access.claimId } },
254
359
  resolution,
255
360
  )
256
361
  return result as Array<{
@@ -260,57 +365,167 @@ function createResolvedRendererAdapter(
260
365
  }>
261
366
  },
262
367
  ensureIndex: async (
263
- collectionId: string,
368
+ target: PersistedStorageTarget,
264
369
  signature: string,
265
370
  spec: PersistedIndexSpec,
371
+ ctx?: { cacheGenerationClaimId?: string },
266
372
  ): Promise<void> => {
373
+ const access = resolvePersistedStorageTarget(
374
+ target,
375
+ ctx?.cacheGenerationClaimId,
376
+ )
267
377
  await executeRequest(
268
378
  `ensureIndex`,
269
- collectionId,
379
+ target,
270
380
  {
271
381
  signature,
272
382
  spec,
383
+ ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
273
384
  },
274
385
  resolution,
275
386
  )
276
387
  },
277
388
  markIndexRemoved: async (
278
- collectionId: string,
389
+ target: PersistedStorageTarget,
279
390
  signature: string,
391
+ ctx?: { cacheGenerationClaimId?: string },
280
392
  ): Promise<void> => {
393
+ const access = resolvePersistedStorageTarget(
394
+ target,
395
+ ctx?.cacheGenerationClaimId,
396
+ )
281
397
  await executeRequest(
282
398
  `markIndexRemoved`,
283
- collectionId,
399
+ target,
284
400
  {
285
401
  signature,
402
+ ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
286
403
  },
287
404
  resolution,
288
405
  )
289
406
  },
290
407
  pullSince: async (
291
- collectionId: string,
408
+ target: PersistedStorageTarget,
292
409
  fromRowVersion: number,
410
+ ctx?: { cacheGenerationClaimId?: string },
293
411
  ): Promise<SQLitePullSinceResult<string | number>> => {
412
+ const access = resolvePersistedStorageTarget(
413
+ target,
414
+ ctx?.cacheGenerationClaimId,
415
+ )
294
416
  const result = await executeRequest(
295
417
  `pullSince`,
296
- collectionId,
418
+ target,
297
419
  {
298
420
  fromRowVersion,
421
+ ctx: { ...ctx, cacheGenerationClaimId: access.claimId },
299
422
  },
300
423
  resolution,
301
424
  )
302
425
  return result as SQLitePullSinceResult<string | number>
303
426
  },
304
427
  getStreamPosition: async (
305
- collectionId: string,
428
+ target: PersistedStorageTarget,
429
+ ctx?: { cacheGenerationClaimId?: string },
306
430
  ): Promise<{
307
431
  latestTerm: number
308
432
  latestSeq: number
309
433
  latestRowVersion: number
310
434
  }> => {
311
- return executeRequest(`getStreamPosition`, collectionId, {}, resolution)
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
+ )
312
445
  },
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`,
486
+ collectionId,
487
+ { storageCollectionId, claimId },
488
+ resolution,
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
313
527
  }
528
+ return adapter
314
529
  }
315
530
 
316
531
  export type ElectronIpcRendererLike = {
@@ -326,6 +541,8 @@ export type ElectronSQLitePersistenceOptions = {
326
541
  channel?: string
327
542
  timeoutMs?: number
328
543
  coordinator?: PersistedCollectionCoordinator
544
+ /** Set false when a custom main-process adapter does not manage cache generations. */
545
+ managedCacheGenerations?: boolean
329
546
  }
330
547
 
331
548
  function resolveInvoke(
@@ -349,29 +566,32 @@ export function createElectronSQLitePersistence(
349
566
  ): PersistedCollectionPersistence {
350
567
  const invoke = resolveInvoke(options)
351
568
  const coordinator = options.coordinator ?? new SingleProcessCoordinator()
569
+ const claimCollections = new Map<string, string>()
352
570
  const executeRequest = createRendererRequestExecutor({
353
571
  invoke,
354
572
  channel: options.channel,
355
573
  timeoutMs: options.timeoutMs,
574
+ claimCollections,
356
575
  })
357
576
  const adapterCache = new Map<string, ElectronRendererResolvedAdapter>()
358
577
 
359
578
  const getAdapterForCollection = (
579
+ collectionId: string | undefined,
360
580
  mode: PersistedCollectionMode,
361
581
  schemaVersion: number | undefined,
362
582
  ) => {
363
- const schemaVersionKey =
364
- schemaVersion === undefined ? `schema:default` : `schema:${schemaVersion}`
365
- const cacheKey = `mode:${mode}|${schemaVersionKey}`
583
+ const cacheKey = JSON.stringify([collectionId, mode, schemaVersion])
366
584
  const cachedAdapter = adapterCache.get(cacheKey)
367
585
  if (cachedAdapter) {
368
586
  return cachedAdapter
369
587
  }
370
588
 
371
- const adapter = createResolvedRendererAdapter(executeRequest, {
372
- mode,
373
- schemaVersion,
374
- })
589
+ const adapter = createResolvedRendererAdapter(
590
+ executeRequest,
591
+ claimCollections,
592
+ options.managedCacheGenerations ?? true,
593
+ { mode, schemaVersion, logicalCollectionId: collectionId },
594
+ )
375
595
  adapterCache.set(cacheKey, adapter)
376
596
  return adapter
377
597
  }
@@ -381,7 +601,7 @@ export function createElectronSQLitePersistence(
381
601
  mode: PersistedCollectionMode,
382
602
  schemaVersion: number | undefined,
383
603
  ): PersistedCollectionPersistence => {
384
- const adapter = getAdapterForCollection(mode, schemaVersion)
604
+ const adapter = getAdapterForCollection(collectionId, mode, schemaVersion)
385
605
  if (coordinator instanceof ElectronCollectionCoordinator) {
386
606
  if (collectionId === undefined) {
387
607
  coordinator.setAdapter(adapter)
@@ -1,33 +0,0 @@
1
- import { ElectronPersistenceMethod, ElectronSerializedError } from './protocol.cjs';
2
- type ElectronPersistenceErrorOptions = {
3
- code?: string;
4
- cause?: unknown;
5
- };
6
- export declare class ElectronPersistenceError extends Error {
7
- readonly code: string | 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 {};
@@ -1,33 +0,0 @@
1
- import { ElectronPersistenceMethod, ElectronSerializedError } from './protocol.js';
2
- type ElectronPersistenceErrorOptions = {
3
- code?: string;
4
- cause?: unknown;
5
- };
6
- export declare class ElectronPersistenceError extends Error {
7
- readonly code: string | 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
8
- cause?: unknown
9
- }
10
-
11
- export class ElectronPersistenceError extends Error {
12
- readonly code: string | 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
- }