Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zhihengGet committed Jul 25, 2024
1 parent 63fd463 commit f0aa74c
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/svelte/svelte-melt/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
})
</script>

isReTORING:{isRes()}
isRestoring:{isRes()}
<!-- <h1>testing list query cache update</h1>
<Simple />
<h1>testing DerivedQuery cache update</h1> -->
Expand Down
2 changes: 1 addition & 1 deletion examples/svelte/svelte-melt/src/routes/cacheUpdate.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<button
onclick={() => {
update.mutate()
}}>update cache</button
}}>update cache {update.status}</button
>

{data.fetchStatus}
Expand Down
14 changes: 14 additions & 0 deletions packages/react-query/src/useQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
QueryObserverOptions,
ThrowOnError,
} from '@tanstack/query-core'
import { queryOptions } from './queryOptions'

// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
// `placeholderData` function always gets undefined passed
Expand Down Expand Up @@ -344,3 +345,16 @@ export function useQueries<

return getCombinedResult(trackResult())
}

const r = useQueries({
queries: [{ queryKey: ['1'], queryFn: (v) => 1 }],
combine: (v) => v.map((v) => v),
})
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})

const queries = useQueries({
queries: [options],
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
OmitKeyof,
QueryClientProviderProps,
} from '@tanstack/svelte-query'
type PersistQueryClientProviderProps = QueryClientProviderProps & {
persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
onSuccess?: () => void
Expand Down
4 changes: 3 additions & 1 deletion packages/svelte-query/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const _isRestoringContextKey = '$$_isRestoring'
/** Retrieves a `isRestoring` from Svelte's context */
export const getIsRestoringContext = (): (() => boolean) => {
try {
const isRestoring = getContext<() => boolean>(_isRestoringContextKey)
const isRestoring = getContext<(() => boolean) | undefined>(
_isRestoringContextKey,
)
return isRestoring ?? (() => false)
} catch (error) {
return () => false
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte-query/src/createQueries.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type GetCreateQueryResult<T> =
unknown extends TError ? DefaultError : TError
>
: // Fallback
QueryObserverResult
never

/**
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
Expand Down Expand Up @@ -221,13 +221,13 @@ export function createQueries<
)

const defaultedQueriesStore = $derived(() => {
return queriesStore().map((opts: QueryObserverOptions) => {
return queriesStore().map((opts) => {
const defaultedOptions = client.defaultQueryOptions(opts)
// Make sure the results are already in fetching state before subscribing or updating options
defaultedOptions._optimisticResults = isRestoring()
? 'isRestoring'
: 'optimistic'
return defaultedOptions
return defaultedOptions as QueryObserverOptions
})
})
const observer = new QueriesObserver<TCombinedResult>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expectTypeOf, test } from 'vitest'
import { skipToken } from '@tanstack/query-core'
import { createQueries, queryOptions } from '../../src/index'
import type { OmitKeyof, QueryObserverResult } from '@tanstack/query-core'
import type { QueryObserverResult } from '@tanstack/query-core'
import type { CreateQueryOptions } from '../../src/index'

describe('createQueries', () => {
Expand All @@ -27,9 +27,7 @@ describe('createQueries', () => {
test('Allow custom hooks using UseQueryOptions', () => {
type Data = string

const useCustomQueries = (
options?: OmitKeyof<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>,
) => {
const useCustomQueries = (options?: CreateQueryOptions<Data>) => {
return createQueries({
queries: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expectTypeOf, test } from 'vitest'
import { createQuery, queryOptions } from '../../src/index'
import type { OmitKeyof } from '@tanstack/query-core'
import type { CreateQueryOptions } from '../../src/index'

describe('createQuery', () => {
Expand Down Expand Up @@ -47,9 +46,7 @@ describe('createQuery', () => {
test('Allow custom hooks using CreateQueryOptions', () => {
type Data = string

const useCustomQuery = (
options?: OmitKeyof<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>,
) => {
const useCustomQuery = (options?: CreateQueryOptions<Data>) => {
return createQuery({
...options,
queryKey: ['todos-key'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ describe('queryOptions', () => {
})

test('Should work when passed to fetchInfiniteQuery', async () => {
const options = infiniteQueryOptions({
const options = {
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})
}

const data = await new QueryClient().fetchInfiniteQuery(options)

Expand Down

0 comments on commit f0aa74c

Please sign in to comment.