MCPcopy Create free account
hub / github.com/TanStack/db / createLoadSubsetDedupe

Function createLoadSubsetDedupe

packages/electric-db-collection/src/electric.ts:390–540  ·  view source on GitHub ↗

* Creates a deduplicated loadSubset handler for progressive/on-demand modes * Returns null for eager mode, or a DeduplicatedLoadSubset instance for other modes. * Handles fetching snapshots in progressive mode during buffering phase, * and requesting snapshots in on-demand mode. * * When cursor

({
  stream,
  syncMode,
  isBufferingInitialSync,
  begin,
  write,
  commit,
  collectionId,
  encodeColumnName,
  signal,
}: {
  stream: ShapeStream<T>
  syncMode: ElectricSyncMode
  isBufferingInitialSync: () => boolean
  begin: () => void
  write: (mutation: {
    type: `insert` | `update` | `delete`
    value: T
    metadata: Record<string, unknown>
  }) => void
  commit: () => void
  collectionId?: string
  /**
   * Optional function to encode column names (e.g., camelCase to snake_case).
   * This is typically the `encode` function from shapeOptions.columnMapper.
   */
  encodeColumnName?: ColumnEncoder
  /**
   * Abort signal to check if the stream has been aborted during cleanup.
   * When aborted, errors from requestSnapshot are silently ignored.
   */
  signal: AbortSignal
})

Source from the content-addressed store, hash-verified

388 * - One for whereCurrent (rows = cursor, for tie-breaking) without limit
389 */
390function createLoadSubsetDedupe<T extends Row<unknown>>({
391 stream,
392 syncMode,
393 isBufferingInitialSync,
394 begin,
395 write,
396 commit,
397 collectionId,
398 encodeColumnName,
399 signal,
400}: {
401 stream: ShapeStream<T>
402 syncMode: ElectricSyncMode
403 isBufferingInitialSync: () => boolean
404 begin: () => void
405 write: (mutation: {
406 type: `insert` | `update` | `delete`
407 value: T
408 metadata: Record<string, unknown>
409 }) => void
410 commit: () => void
411 collectionId?: string
412 /**
413 * Optional function to encode column names (e.g., camelCase to snake_case).
414 * This is typically the `encode` function from shapeOptions.columnMapper.
415 */
416 encodeColumnName?: ColumnEncoder
417 /**
418 * Abort signal to check if the stream has been aborted during cleanup.
419 * When aborted, errors from requestSnapshot are silently ignored.
420 */
421 signal: AbortSignal
422}): DeduplicatedLoadSubset | null {
423 if (syncMode === `eager`) {
424 return null
425 }
426
427 const compileOptions = encodeColumnName ? { encodeColumnName } : undefined
428 const logPrefix = collectionId ? `[${collectionId}] ` : ``
429
430 /**
431 * Handles errors from snapshot operations. Returns true if the error was
432 * handled (signal aborted during cleanup), false if it should be re-thrown.
433 */
434 function handleSnapshotError(error: unknown, operation: string): boolean {
435 if (signal.aborted) {
436 debug(`${logPrefix}Ignoring ${operation} error during cleanup: %o`, error)
437 return true
438 }
439 debug(`${logPrefix}Error in ${operation}: %o`, error)
440 return false
441 }
442
443 const loadSubset = async (opts: LoadSubsetOptions) => {
444 if (isBufferingInitialSync()) {
445 const snapshotParams = compileSQL<T>(opts, compileOptions)
446 try {
447 const { data: rows } = await stream.fetchSnapshot(snapshotParams)

Callers 1

createElectricSyncFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected