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

candidate-applied-once.patch

92 lines3.7 KB

1diff --git a/packages/electric-db-collection/src/electric.ts b/packages/electric-db-collection/src/electric.ts
2index d491c4b78..8e1ea9dae 100644
3--- a/packages/electric-db-collection/src/electric.ts
4+++ b/packages/electric-db-collection/src/electric.ts
5@@ -648,6 +648,7 @@ function createLoadSubsetDedupe<T extends Row<unknown>>({
6 stream,
7 syncMode,
8 isBufferingInitialSync,
9+ fullSnapshotReady,
10 begin,
11 write,
12 commit,
13@@ -661,6 +662,7 @@ function createLoadSubsetDedupe<T extends Row<unknown>>({
14 stream: ShapeStream<T>
15 syncMode: ElectricSyncMode
16 isBufferingInitialSync: () => boolean
17+ fullSnapshotReady?: Promise<void>
18 begin: () => void
19 write: (mutation: {
20 type: `insert` | `update` | `delete`
21@@ -714,6 +716,12 @@ function createLoadSubsetDedupe<T extends Row<unknown>>({
22 }
23 throwIfAborted()
24​
25+ if (fullSnapshotReady) {
26+ await fullSnapshotReady
27+ throwIfAborted()
28+ return
29+ }
31 if (isBufferingInitialSync()) {
32 const snapshotParams = compileSQL<T>(opts, compileOptions)
33 try {
34@@ -1742,6 +1750,18 @@ function createElectricSync<T extends Row<unknown>>(
35 (persistedResumeState.kind === `reset` ||
36 lacksCompletePersistedKeySet ||
37 (!retainsTagState && persistedResumeState.requiresTagState !== false))
38+ // Evaluation prototype: full-mode demand waits for the initial log.
39+ let resolveFullSnapshot: (receipt: SyncAppliedReceipt) => void = () => {}
40+ let rejectFullSnapshot: (error: unknown) => void = () => {}
41+ const fullSnapshotReady =
42+ syncMode === `on-demand` && needsFullSnapshot
43+ ? new Promise<void>((resolve, reject) => {
44+ resolveFullSnapshot = (receipt) =>
45+ resolve(receipt === true ? undefined : receipt)
46+ rejectFullSnapshot = reject
47+ })
48+ : undefined
49+ void fullSnapshotReady?.catch(() => undefined)
50 const canUsePersistedResume =
51 shapeOptions.offset === undefined &&
52 shapeOptions.handle === undefined &&
53@@ -1814,6 +1834,7 @@ function createElectricSync<T extends Row<unknown>>(
54 }
55​
56 abortController.signal.addEventListener(`abort`, () => {
57+ rejectFullSnapshot(new StreamAbortedError(collectionId))
58 lifecycle.retire(lifecycleEpoch)
59 })
60​
61@@ -1839,6 +1860,7 @@ function createElectricSync<T extends Row<unknown>>(
62 (canUsePersistedResume ? persistedResumeState.handle : undefined),
63 signal: abortController.signal,
64 onError: (errorParams) => {
65+ rejectFullSnapshot(errorParams)
66 streamErrorVersion++
67 // Note that Electric sends a 409 error on a `must-refetch` message, but the
68 // ShapeStream handled this and it will not reach this handler, therefor
69@@ -2055,6 +2077,7 @@ function createElectricSync<T extends Row<unknown>>(
70 stream,
71 syncMode,
72 isBufferingInitialSync,
73+ fullSnapshotReady,
74 begin,
75 write,
76 commit,
77@@ -2424,6 +2447,7 @@ function createElectricSync<T extends Row<unknown>>(
78 const readyErrorVersion = streamErrorVersion
79 // Readiness counts accepted rows.
80 const accepted = whenSyncAccepted(applied)
81+ if (commitPoint === `up-to-date`) resolveFullSnapshot(applied)
82 if (accepted === true) {
83 wrappedMarkReady(wasBufferingInitialSync, readyErrorVersion)
84 } else {
85@@ -2511,6 +2535,7 @@ function createElectricSync<T extends Row<unknown>>(
86​
87 pendingResumeBatches.length = 0
88 resumeInvalid = true
89+ rejectFullSnapshot(error)
90 commitResetResumeMetadataImmediately()
91 streamErrorVersion++
92 unsubscribeStream()