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

Function start

packages/trailbase-db-collection/src/trailbase.ts:296–341  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

294 }
295
296 async function start() {
297 const eventStream = await config.recordApi.subscribe(`*`)
298 const reader = (eventReader = eventStream.getReader())
299
300 // Start listening for subscriptions first. Otherwise, we'd risk a gap
301 // between the initial fetch and starting to listen.
302 listen(reader)
303
304 try {
305 // Eager mode: perform initial fetch to populate everything
306 if (internalSyncMode === `eager`) {
307 // Load everything on initial load.
308 await load({})
309 fullSyncCompleted = true
310 }
311 } catch (e) {
312 cancelEventReader()
313 throw e
314 } finally {
315 // Mark ready both if everything went well or if there's an error to
316 // avoid blocking apps waiting for `.preload()` to finish.
317 markReady()
318 }
319
320 // Lastly, start a periodic cleanup task that will be removed when the
321 // reader closes.
322 const periodicCleanupTask = setInterval(() => {
323 seenIds.setState((curr) => {
324 const now = Date.now()
325 let anyExpired = false
326
327 const notExpired = Array.from(curr.entries()).filter(([_, v]) => {
328 const expired = now - v > 300 * 1000
329 anyExpired = anyExpired || expired
330 return !expired
331 })
332
333 if (anyExpired) {
334 return new Map(notExpired)
335 }
336 return curr
337 })
338 }, 120 * 1000)
339
340 reader.closed.finally(() => clearInterval(periodicCleanupTask))
341 }
342
343 start()
344

Callers 1

Calls 8

listenFunction · 0.85
loadFunction · 0.85
cancelEventReaderFunction · 0.85
setStateMethod · 0.80
filterMethod · 0.80
fromMethod · 0.80
subscribeMethod · 0.45
entriesMethod · 0.45

Tested by

no test coverage detected