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

Function loadSubset

packages/electric-db-collection/src/electric.ts:443–537  ·  view source on GitHub ↗
(opts: LoadSubsetOptions)

Source from the content-addressed store, hash-verified

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)
448
449 if (!isBufferingInitialSync()) {
450 debug(`${logPrefix}Ignoring snapshot - sync completed while fetching`)
451 return
452 }
453
454 if (rows.length > 0) {
455 begin()
456 for (const row of rows) {
457 write({
458 type: `insert`,
459 value: row.value,
460 metadata: { ...row.headers },
461 })
462 }
463 commit()
464 debug(`${logPrefix}Applied snapshot with ${rows.length} rows`)
465 }
466 } catch (error) {
467 if (handleSnapshotError(error, `fetchSnapshot`)) {
468 return
469 }
470 throw error
471 }
472 return
473 }
474
475 if (syncMode === `progressive`) {
476 return
477 }
478
479 const { cursor, where, orderBy, limit } = opts
480
481 // When the stream is already up-to-date, it may be in a long-poll wait.
482 // Forcing a disconnect-and-refresh ensures requestSnapshot gets a response
483 // from a fresh server round-trip rather than waiting for the current poll to end.
484 // If the refresh fails (e.g., PauseLock held during subscriber processing in
485 // join pipelines), we fall through to requestSnapshot which still works.
486 if (stream.isUpToDate) {
487 try {
488 await stream.forceDisconnectAndRefresh()
489 } catch (error) {
490 if (handleSnapshotError(error, `forceDisconnectAndRefresh`)) {
491 return
492 }
493 debug(
494 `${logPrefix}forceDisconnectAndRefresh failed, proceeding to requestSnapshot: %o`,
495 error,
496 )
497 }
498 }
499
500 try {

Callers

nothing calls this directly

Calls 6

compileSQLFunction · 0.90
andFunction · 0.90
isBufferingInitialSyncFunction · 0.85
debugFunction · 0.85
handleSnapshotErrorFunction · 0.85
requestSnapshotMethod · 0.80

Tested by

no test coverage detected