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

Function createLocalOnlySync

packages/db/src/local-only.ts:294–382  ·  view source on GitHub ↗

* Internal function to create Local-only sync configuration with transaction confirmation * * This captures the sync functions and provides synchronous confirmation of operations. * It creates a loopback sync that immediately confirms all optimistic operations, * making them permanent in the col

(
  initialData?: Array<T>,
)

Source from the content-addressed store, hash-verified

292 * @returns Object with sync configuration and confirmOperationsSync function
293 */
294function createLocalOnlySync<T extends object, TKey extends string | number>(
295 initialData?: Array<T>,
296) {
297 // Capture sync functions and collection for transaction confirmation
298 let syncBegin: (() => void) | null = null
299 let syncWrite: ((message: { type: OperationType; value: T }) => void) | null =
300 null
301 let syncCommit: (() => void) | null = null
302 let collection: Collection<T, TKey, LocalOnlyCollectionUtils> | null = null
303
304 const sync: SyncConfig<T, TKey> = {
305 /**
306 * Sync function that captures sync parameters and applies initial data
307 * @param params - Sync parameters containing begin, write, and commit functions
308 * @returns Unsubscribe function (empty since no ongoing sync is needed)
309 */
310 sync: (params) => {
311 const { begin, write, commit, markReady } = params
312
313 // Capture sync functions and collection for later use
314 syncBegin = begin
315 syncWrite = write
316 syncCommit = commit
317 collection = params.collection
318 params.collection._state.isLocalOnly = true
319
320 // Apply initial data if provided
321 if (initialData && initialData.length > 0) {
322 // Mark initial data as local so $origin is 'local' for local-only collections
323 for (const item of initialData) {
324 const key = params.collection.getKeyFromItem(item)
325 params.collection._state.pendingLocalChanges.add(key)
326 }
327
328 begin()
329 initialData.forEach((item) => {
330 write({
331 type: `insert`,
332 value: item,
333 })
334 })
335 commit()
336 }
337
338 // Mark collection as ready since local-only collections are immediately ready
339 markReady()
340
341 // Return empty unsubscribe function - no ongoing sync needed
342 return () => {}
343 },
344 /**
345 * Get sync metadata - returns empty object for local-only collections
346 * @returns Empty metadata object
347 */
348 getSyncMetadata: () => ({}),
349 }
350
351 /**

Callers 1

Calls 3

getKeyFromItemMethod · 0.80
addMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected