-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[WIP] New useQueryState Hook #6758
Open
crutchcorn
wants to merge
9
commits into
main
Choose a base branch
from
use-query-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c4a8a12
feat: add useQueryState hook
crutchcorn b8d6851
test: improve useIsFetching tests
crutchcorn 4f00031
fix: useQueryState impl
TkDodo ff658ee
chore: refactor useMutationState, fix tests
crutchcorn bea1eb5
Merge remote-tracking branch 'origin/main' into use-query-state
TkDodo a164794
chore: prettier
TkDodo f9017e6
Merge branch 'main' into use-query-state
TkDodo 7bf9b4e
test: revert timeout changes
TkDodo 05e18d3
Merge branch 'main' into use-query-state
TkDodo 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 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 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 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 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,13 @@ | ||
'use client' | ||
import * as React from 'react' | ||
import { notifyManager } from '@tanstack/query-core' | ||
|
||
import { useQueryClient } from './QueryClientProvider' | ||
import { useQueryState } from './useQueryState' | ||
import type { QueryClient, QueryFilters } from '@tanstack/query-core' | ||
|
||
export function useIsFetching( | ||
filters?: QueryFilters, | ||
queryClient?: QueryClient, | ||
): number { | ||
const client = useQueryClient(queryClient) | ||
const queryCache = client.getQueryCache() | ||
|
||
return React.useSyncExternalStore( | ||
React.useCallback( | ||
(onStoreChange) => | ||
queryCache.subscribe(notifyManager.batchCalls(onStoreChange)), | ||
[queryCache], | ||
), | ||
() => client.isFetching(filters), | ||
() => client.isFetching(filters), | ||
) | ||
return useQueryState( | ||
{ filters: { ...filters, fetchStatus: 'fetching' } }, | ||
queryClient, | ||
).length | ||
TkDodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains 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 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,67 @@ | ||
'use client' | ||
import * as React from 'react' | ||
|
||
import { notifyManager, replaceEqualDeep } from '@tanstack/query-core' | ||
import { useQueryClient } from './QueryClientProvider' | ||
import type { | ||
DefaultError, | ||
Query, | ||
QueryCache, | ||
QueryClient, | ||
QueryFilters, | ||
QueryKey, | ||
QueryState, | ||
} from '@tanstack/query-core' | ||
|
||
type QueryStateOptions<TResult = QueryState> = { | ||
filters?: QueryFilters | ||
select?: (query: Query<unknown, DefaultError, unknown, QueryKey>) => TResult | ||
} | ||
|
||
function getResult<TResult = QueryState>( | ||
queryCache: QueryCache, | ||
options: QueryStateOptions<TResult>, | ||
): Array<TResult> { | ||
return queryCache | ||
.findAll(options.filters) | ||
.map( | ||
(query): TResult => | ||
(options.select ? options.select(query) : query.state) as TResult, | ||
) | ||
} | ||
|
||
export function useQueryState<TResult = QueryState>( | ||
options: QueryStateOptions<TResult> = {}, | ||
queryClient?: QueryClient, | ||
): Array<TResult> { | ||
const queryCache = useQueryClient(queryClient).getQueryCache() | ||
const optionsRef = React.useRef(options) | ||
const result = React.useRef<Array<TResult>>() | ||
if (!result.current) { | ||
result.current = getResult(queryCache, options) | ||
} | ||
|
||
React.useEffect(() => { | ||
optionsRef.current = options | ||
}) | ||
|
||
return React.useSyncExternalStore( | ||
React.useCallback( | ||
(onStoreChange) => | ||
queryCache.subscribe(notifyManager.batchCalls(onStoreChange)), | ||
[queryCache], | ||
), | ||
() => { | ||
const nextResult = replaceEqualDeep( | ||
result.current, | ||
getResult(queryCache, optionsRef.current), | ||
) | ||
if (result.current !== nextResult) { | ||
result.current = nextResult | ||
} | ||
|
||
return result.current | ||
}, | ||
() => result.current, | ||
)! | ||
} |
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.
I think it's also fine to change the assertion here to 0,1,1,0 instead.