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

Function cleanupQueryInternal

packages/query-db-collection/src/query.ts:1490–1550  ·  view source on GitHub ↗
(hashedQueryKey: string)

Source from the content-addressed store, hash-verified

1488 * Callers are responsible for ensuring the query is safe to cleanup.
1489 */
1490 const cleanupQueryInternal = (hashedQueryKey: string) => {
1491 unsubscribes.get(hashedQueryKey)?.()
1492 unsubscribes.delete(hashedQueryKey)
1493 cancelPersistedRetentionExpiry(hashedQueryKey)
1494 retainedQueriesPendingRevalidation.delete(hashedQueryKey)
1495
1496 const rowKeys = queryToRows.get(hashedQueryKey) ?? new Set()
1497 const nextOwnersByRow = new Map<string | number, Set<string>>()
1498 const rowsToDelete: Array<any> = []
1499
1500 rowKeys.forEach((rowKey) => {
1501 const queries = rowToQueries.get(rowKey)
1502
1503 if (!queries) {
1504 return
1505 }
1506
1507 const nextOwners = new Set(queries)
1508 nextOwners.delete(hashedQueryKey)
1509 nextOwnersByRow.set(rowKey, nextOwners)
1510
1511 if (nextOwners.size === 0 && collection.has(rowKey)) {
1512 rowsToDelete.push(collection.get(rowKey))
1513 }
1514 })
1515
1516 const shouldWriteMetadata =
1517 metadata !== undefined && nextOwnersByRow.size > 0
1518 const needsTransaction = shouldWriteMetadata || rowsToDelete.length > 0
1519 if (needsTransaction) {
1520 begin()
1521 }
1522
1523 nextOwnersByRow.forEach((owners, rowKey) => {
1524 if (owners.size === 0) {
1525 rowToQueries.delete(rowKey)
1526 } else {
1527 rowToQueries.set(rowKey, owners)
1528 }
1529
1530 if (shouldWriteMetadata) {
1531 setPersistedOwners(rowKey, owners)
1532 }
1533 })
1534
1535 if (rowsToDelete.length > 0) {
1536 rowsToDelete.forEach((row) => {
1537 write({ type: `delete`, value: row })
1538 })
1539 }
1540
1541 if (needsTransaction) {
1542 commit()
1543 }
1544
1545 state.observers.delete(hashedQueryKey)
1546 queryToRows.delete(hashedQueryKey)
1547 hashToQueryKey.delete(hashedQueryKey)

Callers 2

cleanupQueryIfIdleFunction · 0.85
forceCleanupQueryFunction · 0.85

Calls 7

setPersistedOwnersFunction · 0.85
getMethod · 0.45
deleteMethod · 0.45
forEachMethod · 0.45
setMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected