Skip to content

Commit af22f38

Browse files
committed
GC tanstack query when subscription is unsubscribed
1 parent e95024a commit af22f38

File tree

3 files changed

+193
-216
lines changed

3 files changed

+193
-216
lines changed

packages/db/src/query/subset-dedupe.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,5 @@ export class DeduplicatedLoadSubset {
239239
* would reflect the mutated values rather than what was actually loaded.
240240
*/
241241
export function cloneOptions(options: LoadSubsetOptions): LoadSubsetOptions {
242-
return {
243-
where: options.where,
244-
orderBy: options.orderBy,
245-
limit: options.limit,
246-
// Note: We don't clone subscription as it's not part of predicate matching
247-
}
242+
return { ...options }
248243
}

packages/query-db-collection/src/query.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,13 @@ export function queryCollectionOptions(
562562
subscribeToQuery(localObserver, hashedQueryKey)
563563
}
564564

565+
// Tell tanstack query to GC the query when the subscription is unsubscribed
566+
// The subscription is unsubscribed when the live query is GCed.
567+
const subscription = opts.subscription
568+
subscription?.once(`unsubscribed`, () => {
569+
queryClient.removeQueries({ queryKey: key, exact: true })
570+
})
571+
565572
return readyPromise
566573
}
567574

@@ -821,10 +828,6 @@ export function queryCollectionOptions(
821828
? undefined
822829
: new DeduplicatedLoadSubset(createQueryFromOpts, createLocalQuery)
823830

824-
// TODO: run the tests, probably some will fail bc different requests are made now
825-
// so fix the test expectations
826-
// then also add all the new dedup tests that Sam also added to the Electric collection
827-
828831
return {
829832
loadSubset: loadSubsetDedupe?.loadSubset,
830833
cleanup,

0 commit comments

Comments
 (0)