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

Function makeQueryResultHandler

packages/query-db-collection/src/query.ts:1354–1403  ·  view source on GitHub ↗
(queryKey: QueryKey)

Source from the content-addressed store, hash-verified

1352
1353 // eslint-disable-next-line no-shadow
1354 const makeQueryResultHandler = (queryKey: QueryKey) => {
1355 const hashedQueryKey = hashKey(queryKey)
1356 const handleQueryResult: UpdateHandler = (result) => {
1357 if (result.isSuccess) {
1358 // Skip processing this result while data refreshes are deferred.
1359 // Optimistic state covers the gap. Once the barrier resolves,
1360 // trigger a fresh refetch to get authoritative data.
1361 if (collection.deferDataRefresh) {
1362 collection.deferDataRefresh.then(() => {
1363 const observer = state.observers.get(hashedQueryKey)
1364 if (observer) {
1365 observer.refetch().catch(() => {
1366 // Errors handled by the next handleQueryResult invocation
1367 })
1368 }
1369 })
1370 return
1371 }
1372
1373 if (retainedQueriesPendingRevalidation.has(hashedQueryKey)) {
1374 void reconcileSuccessfulResult(queryKey, result).catch((error) => {
1375 console.error(
1376 `[QueryCollection] Error reconciling query ${String(queryKey)}:`,
1377 error,
1378 )
1379 })
1380 } else {
1381 applySuccessfulResult(queryKey, result)
1382 }
1383 } else if (result.isError) {
1384 const isNewError =
1385 result.errorUpdatedAt !== state.lastErrorUpdatedAt ||
1386 result.error !== state.lastError
1387 if (isNewError) {
1388 state.lastError = result.error
1389 state.errorCount++
1390 state.lastErrorUpdatedAt = result.errorUpdatedAt
1391 }
1392
1393 console.error(
1394 `[QueryCollection] Error observing query ${String(queryKey)}:`,
1395 result.error,
1396 )
1397
1398 // Mark collection as ready even on error to avoid blocking apps
1399 markReady()
1400 }
1401 }
1402 return handleQueryResult
1403 }
1404
1405 const isSubscribed = (hashedQueryKey: string) => {
1406 return unsubscribes.has(hashedQueryKey)

Callers 2

subscribeToQueryFunction · 0.85
internalSyncFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected