Skip to content
Source
Sign inSign up
TanStack/query

fix(svelte-query): unsubscribe the observer when its effect root is disposed #11597

ClosedNeekoras(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/11597/headopened View on GitHub

Showing 3 changed files with 196 additions and 0 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/svelte-query-effect-root-cleanup.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/svelte-query': patch
+---
+​
+fix(svelte-query): unsubscribe the observer when an `$effect.root` holding a query is disposed
5 additions and 0 deletionspackages/svelte-query/src/createBaseQuery.svelte.tsModified
View file
@@ -92,6 +92,11 @@
return unsubscribe
},
+ // ...and because the above only returns a cleanup from its second run onwards, tear the initial
+ // subscription down with an effect of its own, so disposing an `$effect.root` unsubscribes it.
+ try {
+ $effect.pre(() => () => unsubscribe())
+ } catch (e) {}
// ...and finally also cleanup via onDestroy because that one runs on the server whereas $effect.pre does not.
// (in a try-catch because it theoretically can be called in a non-component context - that should not happen
// but it would be a breaking change technically to error out here. SSR-safe because this wouldn't be called during SSR if it was not in a component)
186 additions and 0 deletionspackages/svelte-query/tests/createQuery/observer-cleanup.svelte.test.tsAdded
View file
@@ -0,0 +1,186 @@
+import { render } from '@testing-library/svelte'
+import { flushSync } from 'svelte'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import {
+ QueryClient,
+ createInfiniteQuery,