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

Function applySuccessfulResult

packages/query-db-collection/src/query.ts:1243–1338  ·  view source on GitHub ↗
(
      queryKey: QueryKey,
      result: QueryObserverResult<any, any>,
      persistedBaseline?: Map<
        string | number,
        {
          value: any
          owners: Set<string>
        }
      >,
    )

Source from the content-addressed store, hash-verified

1241 type UpdateHandler = Parameters<QueryObserver[`subscribe`]>[0]
1242
1243 const applySuccessfulResult = (
1244 queryKey: QueryKey,
1245 result: QueryObserverResult<any, any>,
1246 persistedBaseline?: Map<
1247 string | number,
1248 {
1249 value: any
1250 owners: Set<string>
1251 }
1252 >,
1253 ) => {
1254 const hashedQueryKey = hashKey(queryKey)
1255
1256 if (collection.status === `cleaned-up`) {
1257 return
1258 }
1259
1260 // Clear error state
1261 state.lastError = undefined
1262 state.errorCount = 0
1263
1264 const rawData = result.data
1265 const newItemsArray = select ? select(rawData) : rawData
1266
1267 if (
1268 !Array.isArray(newItemsArray) ||
1269 newItemsArray.some((item) => typeof item !== `object`)
1270 ) {
1271 const errorMessage = select
1272 ? `@tanstack/query-db-collection: select() must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`
1273 : `@tanstack/query-db-collection: queryFn must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey)}`
1274
1275 console.error(errorMessage)
1276 return
1277 }
1278
1279 const currentSyncedItems: Map<string | number, any> = new Map(
1280 collection._state.syncedData.entries(),
1281 )
1282 const shouldUsePersistedBaseline = persistedBaseline !== undefined
1283 const previouslyOwnedRows = shouldUsePersistedBaseline
1284 ? new Set(persistedBaseline.keys())
1285 : getHydratedOwnedRowsForQueryBaseline(hashedQueryKey)
1286 const newItemsMap = new Map<string | number, any>()
1287 newItemsArray.forEach((item) => {
1288 const key = getKey(item)
1289 newItemsMap.set(key, item)
1290 })
1291
1292 begin()
1293 if (metadata) {
1294 metadata.collection.delete(
1295 `${QUERY_COLLECTION_GC_PREFIX}${hashedQueryKey}`,
1296 )
1297 }
1298
1299 previouslyOwnedRows.forEach((key) => {
1300 const oldItem = shouldUsePersistedBaseline

Callers 2

handleQueryResultFunction · 0.85

Calls 15

deepEqualsFunction · 0.90
getPersistedOwnersFunction · 0.85
setPersistedOwnersFunction · 0.85
removeRowFunction · 0.85
addRowFunction · 0.85
getKeyFunction · 0.50
entriesMethod · 0.45
keysMethod · 0.45
forEachMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected