-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(svelte-query): state_unsafe_mutation error with useIs...
#9493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lachlancollins
merged 6 commits into
TanStack:svelte-5-adapter
from
hmnd:fix/svelte-query-isfetching
Sep 27, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4da7817
fix(svelte-query): don't wrap observers in derived to avoid state_uns…
hmnd de3a48e
test(svelte-query): wrap (useIs...) tests in QueryClientProvider to t…
hmnd f919a0f
fix(svelte-query): update observers when passed in query client changes
hmnd ac2afcf
fix(svelte-query): simplify creatMutation sub/unsub
hmnd 000016c
Merge branch 'svelte-5-adapter' into fix/svelte-query-isfetching
lachlancollins 62798d6
Refactor result handling in createMutation.svelte.ts
lachlancollins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { untrack } from 'svelte' | ||
| // modified from the great https://github.com/svecosystem/runed | ||
| function runEffect( | ||
| flush: 'post' | 'pre', | ||
| effect: () => void | VoidFunction, | ||
| ): void { | ||
| switch (flush) { | ||
| case 'post': | ||
| $effect(effect) | ||
| break | ||
| case 'pre': | ||
| $effect.pre(effect) | ||
| break | ||
| } | ||
| } | ||
| type Getter<T> = () => T | ||
| export const watchChanges = <T>( | ||
| sources: Getter<T> | Array<Getter<T>>, | ||
| flush: 'post' | 'pre', | ||
| effect: ( | ||
| values: T | Array<T>, | ||
| previousValues: T | undefined | Array<T | undefined>, | ||
| ) => void, | ||
| ) => { | ||
| let active = false | ||
| let previousValues: T | undefined | Array<T | undefined> = Array.isArray( | ||
| sources, | ||
| ) | ||
| ? [] | ||
| : undefined | ||
| runEffect(flush, () => { | ||
| const values = Array.isArray(sources) | ||
| ? sources.map((source) => source()) | ||
| : sources() | ||
| if (!active) { | ||
| active = true | ||
| previousValues = values | ||
| return | ||
| } | ||
| const cleanup = untrack(() => effect(values, previousValues)) | ||
| previousValues = values | ||
| return cleanup | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <script lang="ts"> | ||
| import { QueryClient } from '@tanstack/query-core' | ||
| import { QueryClientProvider } from '../src/index.js' | ||
| import { Snippet } from 'svelte' | ||
|
|
||
| const { | ||
| queryClient = new QueryClient(), | ||
| children, | ||
| }: { queryClient?: QueryClient; children: Snippet } = $props() | ||
| </script> | ||
|
|
||
| <QueryClientProvider client={queryClient}> | ||
| {@render children()} | ||
| </QueryClientProvider> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 7 additions & 20 deletions
27
packages/svelte-query/tests/useIsFetching/BaseExample.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,11 @@ | ||
| <script lang="ts"> | ||
| import { QueryClient } from '@tanstack/query-core' | ||
| import { createQuery, useIsFetching } from '../../src/index.js' | ||
| import { sleep } from '@tanstack/query-test-utils' | ||
|
|
||
| const queryClient = new QueryClient() | ||
| let ready = $state(false) | ||
|
|
||
| const isFetching = useIsFetching(undefined, queryClient) | ||
|
|
||
| const query = createQuery( | ||
| () => ({ | ||
| queryKey: ['test'], | ||
| queryFn: () => sleep(10).then(() => 'test'), | ||
| enabled: ready, | ||
| }), | ||
| () => queryClient, | ||
| ) | ||
| import ProviderWrapper from '../ProviderWrapper.svelte' | ||
| import FetchStatus from './FetchStatus.svelte' | ||
| import Query from './Query.svelte' | ||
| </script> | ||
|
|
||
| <button onclick={() => (ready = true)}>setReady</button> | ||
| <ProviderWrapper> | ||
| <FetchStatus /> | ||
|
|
||
| <div>isFetching: {isFetching.current}</div> | ||
| <div>Data: {query.data ?? 'undefined'}</div> | ||
| <Query /> | ||
| </ProviderWrapper> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <script lang="ts"> | ||
| import { useIsFetching } from '../../src/index.js' | ||
| const isFetching = useIsFetching() | ||
| </script> | ||
|
|
||
| <div>isFetching: {isFetching.current}</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <script lang="ts"> | ||
| import { createQuery } from '../../src/index.js' | ||
| import { sleep } from '@tanstack/query-test-utils' | ||
|
|
||
| let ready = $state(false) | ||
|
|
||
| const query = createQuery(() => ({ | ||
| queryKey: ['test'], | ||
| queryFn: async () => { | ||
| await sleep(10) | ||
| return 'test' | ||
| }, | ||
| enabled: ready, | ||
| })) | ||
| </script> | ||
|
|
||
| <button onclick={() => (ready = true)}>setReady</button> | ||
|
|
||
| <div>Data: {query.data ?? 'undefined'}</div> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lachlancollins do you think we need to keep the Svelte peer dep pinned to as low as
5.7.0, or could we bump that to at least5.25.0so that assigning to$derivedlike this is supported? This was part ofeslint --fix, but if we need to stay on the lower version, I'll have to revert this back to a manually managed$state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to bump the peer dependency version!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hmnd I've updated this on the target branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this change(?) might have caused tests to start failing. It's difficult to know though, as the force pushes have erased the history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I found the diff in a branch I cloned earlier. Tests are all passing again, so I think I'll merge this! Thanks heaps for your time @hmnd !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for getting this merged @lachlancollins! Sorry about the force pushes 😅