Skip to content

Commit e96d6ed

Browse files
kevin-dpicehaunter
authored andcommitted
Unify the hasUpToDate and hasSubsetEnd variables into a commitPoint variable
1 parent c9f9643 commit e96d6ed

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

packages/electric-db-collection/src/electric.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,8 +1006,8 @@ function createElectricSync<T extends Row<unknown>>(
10061006
})
10071007

10081008
unsubscribeStream = stream.subscribe((messages: Array<Message<T>>) => {
1009-
let hasUpToDate = false
1010-
let hasSubsetEnd = false
1009+
// Track commit point type - up-to-date takes precedence as it also triggers progressive mode atomic swap
1010+
let commitPoint: `up-to-date` | `subset-end` | null = null
10111011

10121012
// Clear the current batch buffer at the START of processing a new batch
10131013
// This preserves messages from the previous batch until new ones arrive,
@@ -1089,11 +1089,14 @@ function createElectricSync<T extends Row<unknown>>(
10891089
if (!isBufferingInitialSync()) {
10901090
newSnapshots.push(parseSnapshotMessage(message))
10911091
}
1092-
} else if (isSubsetEndMessage(message)) {
1093-
// subset-end marks the end of an injected subset snapshot - treat like up-to-date for commit
1094-
hasSubsetEnd = true
10951092
} else if (isUpToDateMessage(message)) {
1096-
hasUpToDate = true
1093+
// up-to-date takes precedence - also triggers progressive mode atomic swap
1094+
commitPoint = `up-to-date`
1095+
} else if (isSubsetEndMessage(message)) {
1096+
// subset-end triggers commit but not progressive mode atomic swap
1097+
if (commitPoint !== `up-to-date`) {
1098+
commitPoint = `subset-end`
1099+
}
10971100
} else if (isMustRefetchMessage(message)) {
10981101
debug(
10991102
`${collectionId ? `[${collectionId}] ` : ``}Received must-refetch message, starting transaction with truncate`,
@@ -1112,16 +1115,15 @@ function createElectricSync<T extends Row<unknown>>(
11121115
loadSubsetDedupe?.reset()
11131116

11141117
// Reset flags so we continue accumulating changes until next up-to-date
1115-
hasUpToDate = false
1116-
hasSubsetEnd = false
1118+
commitPoint = null
11171119
hasReceivedUpToDate = false // Reset for progressive mode (isBufferingInitialSync will reflect this)
11181120
bufferedMessages.length = 0 // Clear buffered messages
11191121
}
11201122
}
11211123

1122-
if (hasUpToDate || hasSubsetEnd) {
1123-
// PROGRESSIVE MODE: Atomic swap on first up-to-date
1124-
if (isBufferingInitialSync() && hasUpToDate) {
1124+
if (commitPoint !== null) {
1125+
// PROGRESSIVE MODE: Atomic swap on first up-to-date (not subset-end)
1126+
if (isBufferingInitialSync() && commitPoint === `up-to-date`) {
11251127
debug(
11261128
`${collectionId ? `[${collectionId}] ` : ``}Progressive mode: Performing atomic swap with ${bufferedMessages.length} buffered messages`,
11271129
)
@@ -1173,14 +1175,10 @@ function createElectricSync<T extends Row<unknown>>(
11731175
transactionStarted = false
11741176
}
11751177
}
1176-
1177-
if (hasUpToDate || (hasSubsetEnd && syncMode === `on-demand`)) {
1178-
// Mark the collection as ready now that sync is up to date
1179-
wrappedMarkReady(isBufferingInitialSync())
1180-
}
1178+
wrappedMarkReady(isBufferingInitialSync())
11811179

11821180
// Track that we've received the first up-to-date for progressive mode
1183-
if (hasUpToDate) {
1181+
if (commitPoint === `up-to-date`) {
11841182
hasReceivedUpToDate = true
11851183
}
11861184

@@ -1211,12 +1209,11 @@ function createElectricSync<T extends Row<unknown>>(
12111209
return seen
12121210
})
12131211

1214-
// Resolve all matched pending matches on up-to-date or snapshot-end in on-demand mode
1212+
// Resolve all matched pending matches on up-to-date or subset-end
12151213
// Set batchCommitted BEFORE resolving to avoid timing window where late awaitMatch
12161214
// calls could register as "matched" after resolver pass already ran
1217-
if (hasUpToDate || (hasSnapshotEnd && syncMode === `on-demand`)) {
1218-
batchCommitted.setState(() => true)
1219-
}
1215+
batchCommitted.setState(() => true)
1216+
12201217
resolveMatchedPendingMatches()
12211218
}
12221219
})

0 commit comments

Comments
 (0)