MCPcopy Index your code
hub / github.com/TanStack/db / startSync

Method startSync

packages/db/src/collection/sync.ts:78–273  ·  view source on GitHub ↗

* Start the sync process for this collection * This is called when the collection is first accessed or preloaded

()

Source from the content-addressed store, hash-verified

76 * This is called when the collection is first accessed or preloaded
77 */
78 public startSync(): void {
79 if (
80 this.lifecycle.status !== `idle` &&
81 this.lifecycle.status !== `cleaned-up`
82 ) {
83 return // Already started or in progress
84 }
85
86 this.lifecycle.setStatus(`loading`)
87
88 try {
89 const syncRes = normalizeSyncFnResult(
90 this.config.sync.sync({
91 collection: this.collection,
92 begin: (options?: { immediate?: boolean }) => {
93 this.state.pendingSyncedTransactions.push({
94 committed: false,
95 operations: [],
96 deletedKeys: new Set(),
97 rowMetadataWrites: new Map(),
98 collectionMetadataWrites: new Map(),
99 immediate: options?.immediate,
100 })
101 },
102 write: (
103 messageWithOptionalKey: ChangeMessageOrDeleteKeyMessage<
104 TOutput,
105 TKey
106 >,
107 ) => {
108 const pendingTransaction =
109 this.state.pendingSyncedTransactions[
110 this.state.pendingSyncedTransactions.length - 1
111 ]
112 if (!pendingTransaction) {
113 throw new NoPendingSyncTransactionWriteError()
114 }
115 if (pendingTransaction.committed) {
116 throw new SyncTransactionAlreadyCommittedWriteError()
117 }
118
119 let key: TKey | undefined = undefined
120 if (`key` in messageWithOptionalKey) {
121 key = messageWithOptionalKey.key
122 } else {
123 key = this.config.getKey(messageWithOptionalKey.value)
124 }
125
126 if (this.state.pendingLocalChanges.has(key)) {
127 this.state.pendingLocalOrigins.add(key)
128 }
129
130 let messageType = messageWithOptionalKey.type
131
132 // Check if an item with this key already exists when inserting
133 if (messageWithOptionalKey.type === `insert`) {
134 const insertingIntoExistingSynced = this.state.syncedData.has(key)
135 const hasPendingDeleteForKey =

Callers 5

preloadMethod · 0.95
addSubscriberMethod · 0.80
constructorMethod · 0.80
startSyncImmediateMethod · 0.80

Calls 11

createSyncMetadataApiMethod · 0.95
deepEqualsFunction · 0.90
markReadyMethod · 0.80
normalizeSyncFnResultFunction · 0.70
setStatusMethod · 0.45
getKeyMethod · 0.45
hasMethod · 0.45
addMethod · 0.45
getMethod · 0.45
setMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected