Skip to content

Commit cfe007d

Browse files
committed
GC tanstack query when subscription is unsubscribed
1 parent 21f9f10 commit cfe007d

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
@@ -238,10 +238,5 @@ export class DeduplicatedLoadSubset {
238238
* would reflect the mutated values rather than what was actually loaded.
239239
*/
240240
export function cloneOptions(options: LoadSubsetOptions): LoadSubsetOptions {
241-
return {
242-
where: options.where,
243-
orderBy: options.orderBy,
244-
limit: options.limit,
245-
// Note: We don't clone subscription as it's not part of predicate matching
246-
}
241+
return { ...options }
247242
}

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)