diff --git a/packages/svelte-query-persist-client/src/PersistQueryClientProvider.svelte b/packages/svelte-query-persist-client/src/PersistQueryClientProvider.svelte index dc2f060f2c..e2e261fbc1 100644 --- a/packages/svelte-query-persist-client/src/PersistQueryClientProvider.svelte +++ b/packages/svelte-query-persist-client/src/PersistQueryClientProvider.svelte @@ -21,7 +21,6 @@ client, children, persistOptions, - onSuccess, ...props }: PersistQueryClientProviderProps = $props() @@ -39,12 +38,10 @@ }) $effect(() => { isRestoring = true - persistQueryClientRestore(options).then(() => { + persistQueryClientRestore(options).then(async () => { try { - console.log('restoring !', typeof isRestoring) - onSuccess?.() + await props.onSuccess?.() } finally { - console.log('restored') isRestoring = false } }) diff --git a/packages/svelte-query-persist-client/tests/FreshData/FreshData.svelte b/packages/svelte-query-persist-client/tests/FreshData/FreshData.svelte index c2beda045c..5b7e030f43 100644 --- a/packages/svelte-query-persist-client/tests/FreshData/FreshData.svelte +++ b/packages/svelte-query-persist-client/tests/FreshData/FreshData.svelte @@ -2,30 +2,32 @@ import { createQuery } from '@tanstack/svelte-query' import { sleep } from '../utils' import type { StatusResult } from '../utils' + import { untrack } from 'svelte' - let { key, states, fetched } = $props<{ + let { key, states = $bindable() } = $props<{ key: Array states: Array> fetched: boolean }>() - const state = createQuery({ + const s = createQuery({ queryKey: key, queryFn: async () => { - fetched.set(true) - await sleep(10) + states.push('fetched') return 'fetched' }, staleTime: Infinity, }) - $effect(() => { - states = [...states, state] + JSON.stringify(s.data) + untrack(() => { + states.push($state.snapshot(s)) + }) })
-

data: {state.data ?? 'null'}

-

fetchStatus: {state.fetchStatus}

+

data: {s.data ?? 'null'}

+

fetchStatus: {s.fetchStatus}

diff --git a/packages/svelte-query-persist-client/tests/FreshData/Provider.svelte b/packages/svelte-query-persist-client/tests/FreshData/Provider.svelte index 7404a6849c..835011ae02 100644 --- a/packages/svelte-query-persist-client/tests/FreshData/Provider.svelte +++ b/packages/svelte-query-persist-client/tests/FreshData/Provider.svelte @@ -5,13 +5,17 @@ import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core' import type { StatusResult } from '../utils' - export let queryClient: QueryClient - export let persistOptions: OmitKeyof - export let key: Array - export let states: Array> - export let fetched: boolean + interface Props { + queryClient: QueryClient + persistOptions: OmitKeyof + key: Array + states: Array> + fetched: boolean + } + + let { queryClient, persistOptions, key, states, fetched }: Props = $props() - + diff --git a/packages/svelte-query-persist-client/tests/InitialData/InitialData.svelte b/packages/svelte-query-persist-client/tests/InitialData/InitialData.svelte index e8076d0256..b60c882e5c 100644 --- a/packages/svelte-query-persist-client/tests/InitialData/InitialData.svelte +++ b/packages/svelte-query-persist-client/tests/InitialData/InitialData.svelte @@ -2,13 +2,14 @@ import { createQuery } from '@tanstack/svelte-query' import { sleep } from '../utils' import type { StatusResult } from '../utils' + import { untrack } from 'svelte' - let { key, states } = $props<{ + let { key, states = $bindable() } = $props<{ key: Array states: Array> }>() - const state = createQuery({ + const s = createQuery({ queryKey: key, queryFn: async () => { await sleep(10) @@ -22,11 +23,15 @@ }) $effect(() => { - states = [...states, state] + console.log('initial data', s) + JSON.stringify(s) + untrack(() => { + states.push($state.snapshot(s)) + }) })
-

{state.data}

-

fetchStatus: {state.fetchStatus}

+

{s.data}

+

fetchStatus: {s.fetchStatus}

diff --git a/packages/svelte-query-persist-client/tests/InitialData/Provider.svelte b/packages/svelte-query-persist-client/tests/InitialData/Provider.svelte index 3cb12354ee..37f9644134 100644 --- a/packages/svelte-query-persist-client/tests/InitialData/Provider.svelte +++ b/packages/svelte-query-persist-client/tests/InitialData/Provider.svelte @@ -5,12 +5,21 @@ import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core' import type { StatusResult } from '../utils' - export let queryClient: QueryClient - export let persistOptions: OmitKeyof - export let key: Array - export let states: Array> + interface Props { + queryClient: QueryClient + persistOptions: OmitKeyof + key: Array + states: Array> + } + + let { + queryClient, + persistOptions, + key, + states = $bindable(), + }: Props = $props() - + diff --git a/packages/svelte-query-persist-client/tests/OnSuccess/OnSuccess.svelte b/packages/svelte-query-persist-client/tests/OnSuccess/OnSuccess.svelte index 2ba9fae10c..8bd6f0240c 100644 --- a/packages/svelte-query-persist-client/tests/OnSuccess/OnSuccess.svelte +++ b/packages/svelte-query-persist-client/tests/OnSuccess/OnSuccess.svelte @@ -2,7 +2,11 @@ import { createQuery } from '@tanstack/svelte-query' import { sleep } from '../utils' - export let key: Array + interface Props { + key: Array + } + + let { key }: Props = $props() const state = createQuery({ queryKey: key, diff --git a/packages/svelte-query-persist-client/tests/OnSuccess/Provider.svelte b/packages/svelte-query-persist-client/tests/OnSuccess/Provider.svelte index 8da03b9ca2..98313ec8b3 100644 --- a/packages/svelte-query-persist-client/tests/OnSuccess/Provider.svelte +++ b/packages/svelte-query-persist-client/tests/OnSuccess/Provider.svelte @@ -4,10 +4,14 @@ import type { OmitKeyof, QueryClient } from '@tanstack/svelte-query' import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core' - export let queryClient: QueryClient - export let persistOptions: OmitKeyof - export let key: Array - export let onSuccess: () => void + interface Props { + queryClient: QueryClient + persistOptions: OmitKeyof + key: Array + onSuccess: () => void + } + + let { queryClient, persistOptions, key, onSuccess }: Props = $props() diff --git a/packages/svelte-query-persist-client/tests/PersistQueryClientProvider.test.svelte.ts b/packages/svelte-query-persist-client/tests/PersistQueryClientProvider.test.svelte.ts index e1b9a1db99..26d8849c39 100644 --- a/packages/svelte-query-persist-client/tests/PersistQueryClientProvider.test.svelte.ts +++ b/packages/svelte-query-persist-client/tests/PersistQueryClientProvider.test.svelte.ts @@ -82,7 +82,7 @@ describe('PersistQueryClientProvider', () => { await waitFor(() => rendered.getByText('hydrated')) await waitFor(() => rendered.getByText('fetched')) - expect(states).toHaveLength(4) + expect(states).toHaveLength(3) expect(states[0]).toMatchObject({ status: 'pending', @@ -98,15 +98,15 @@ describe('PersistQueryClientProvider', () => { expect(states[2]).toMatchObject({ status: 'success', - fetchStatus: 'fetching', - data: 'hydrated', + fetchStatus: 'idle', + data: 'fetched', }) - expect(states[3]).toMatchObject({ + /* expect(states[3]).toMatchObject({ status: 'success', fetchStatus: 'idle', data: 'fetched', - }) + }) */ }) test('should also put useQueries into idle state', async () => { @@ -138,7 +138,7 @@ describe('PersistQueryClientProvider', () => { await waitFor(() => rendered.getByText('hydrated')) await waitFor(() => rendered.getByText('fetched')) - expect(states).toHaveLength(4) + expect(states).toHaveLength(3) expect(states[0]).toMatchObject({ status: 'pending', @@ -153,12 +153,6 @@ describe('PersistQueryClientProvider', () => { }) expect(states[2]).toMatchObject({ - status: 'success', - fetchStatus: 'fetching', - data: 'hydrated', - }) - - expect(states[3]).toMatchObject({ status: 'success', fetchStatus: 'idle', data: 'fetched', @@ -194,7 +188,7 @@ describe('PersistQueryClientProvider', () => { await waitFor(() => rendered.getByText('hydrated')) await waitFor(() => rendered.getByText('fetched')) - expect(states).toHaveLength(4) + expect(states).toHaveLength(3) expect(states[0]).toMatchObject({ status: 'success', @@ -208,13 +202,13 @@ describe('PersistQueryClientProvider', () => { data: 'hydrated', }) - expect(states[2]).toMatchObject({ + /* expect(states[2]).toMatchObject({ status: 'success', fetchStatus: 'fetching', data: 'hydrated', - }) + }) */ - expect(states[3]).toMatchObject({ + expect(states[2]).toMatchObject({ status: 'success', fetchStatus: 'idle', data: 'fetched', @@ -255,7 +249,7 @@ describe('PersistQueryClientProvider', () => { expect(fetched).toBe(false) expect(states).toHaveLength(2) - + console.log('statesa', states) expect(states[0]).toMatchObject({ status: 'pending', fetchStatus: 'idle', @@ -302,7 +296,7 @@ describe('PersistQueryClientProvider', () => { await waitFor(() => rendered.getByText('fetched')) }) - test.only('should await onSuccess after successful restoring', async () => { + test('should await onSuccess after successful restoring', async () => { const key = queryKey() const queryClient = createQueryClient() @@ -335,6 +329,7 @@ describe('PersistQueryClientProvider', () => { await waitFor(() => rendered.getByText('hydrated')) await waitFor(() => rendered.getByText('fetched')) + await sleep(400) expect(states).toEqual([ 'onSuccess', 'onSuccess done', diff --git a/packages/svelte-query-persist-client/tests/RemoveCache/Provider.svelte b/packages/svelte-query-persist-client/tests/RemoveCache/Provider.svelte index b7b70d088f..8d146a01aa 100644 --- a/packages/svelte-query-persist-client/tests/RemoveCache/Provider.svelte +++ b/packages/svelte-query-persist-client/tests/RemoveCache/Provider.svelte @@ -4,9 +4,13 @@ import type { OmitKeyof, QueryClient } from '@tanstack/svelte-query' import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core' - export let queryClient: QueryClient - export let persistOptions: OmitKeyof - export let key: Array + interface Props { + queryClient: QueryClient + persistOptions: OmitKeyof + key: Array + } + + let { queryClient, persistOptions, key }: Props = $props() diff --git a/packages/svelte-query-persist-client/tests/RemoveCache/RemoveCache.svelte b/packages/svelte-query-persist-client/tests/RemoveCache/RemoveCache.svelte index 2ba9fae10c..8bd6f0240c 100644 --- a/packages/svelte-query-persist-client/tests/RemoveCache/RemoveCache.svelte +++ b/packages/svelte-query-persist-client/tests/RemoveCache/RemoveCache.svelte @@ -2,7 +2,11 @@ import { createQuery } from '@tanstack/svelte-query' import { sleep } from '../utils' - export let key: Array + interface Props { + key: Array + } + + let { key }: Props = $props() const state = createQuery({ queryKey: key, diff --git a/packages/svelte-query-persist-client/tests/RestoreCache/Provider.svelte b/packages/svelte-query-persist-client/tests/RestoreCache/Provider.svelte index bd4ff8210e..74fb264a5a 100644 --- a/packages/svelte-query-persist-client/tests/RestoreCache/Provider.svelte +++ b/packages/svelte-query-persist-client/tests/RestoreCache/Provider.svelte @@ -5,12 +5,21 @@ import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core' import type { StatusResult } from '../utils' - export let queryClient: QueryClient - export let persistOptions: OmitKeyof - export let key: Array - export let states: Array> + interface Props { + queryClient: QueryClient + persistOptions: OmitKeyof + key: Array + states: Array> + } + + let { + queryClient, + persistOptions, + key, + states = $bindable(), + }: Props = $props() - + diff --git a/packages/svelte-query-persist-client/tests/RestoreCache/RestoreCache.svelte b/packages/svelte-query-persist-client/tests/RestoreCache/RestoreCache.svelte index d6cc68ab56..b11ae06fa9 100644 --- a/packages/svelte-query-persist-client/tests/RestoreCache/RestoreCache.svelte +++ b/packages/svelte-query-persist-client/tests/RestoreCache/RestoreCache.svelte @@ -2,13 +2,14 @@ import { createQuery } from '@tanstack/svelte-query' import { sleep } from '../utils' import type { StatusResult } from '../utils' + import { untrack } from 'svelte' let { key, states } = $props<{ key: Array states: Array> }>() - const state = createQuery({ + const ss = createQuery({ queryKey: key, queryFn: async () => { await sleep(10) @@ -17,11 +18,15 @@ }) $effect(() => { - states = [...states, state] + console.log('initial data', ss) + JSON.stringify(ss) + untrack(() => { + states.push($state.snapshot(ss)) + }) })
-

{state.data}

-

fetchStatus: {state.fetchStatus}

+

{ss.data}

+

fetchStatus: {ss.fetchStatus}

diff --git a/packages/svelte-query-persist-client/tests/UseQueries/UseQueries.svelte b/packages/svelte-query-persist-client/tests/UseQueries/UseQueries.svelte index 12b1ee9d83..cb57ea0121 100644 --- a/packages/svelte-query-persist-client/tests/UseQueries/UseQueries.svelte +++ b/packages/svelte-query-persist-client/tests/UseQueries/UseQueries.svelte @@ -2,13 +2,14 @@ import { createQueries } from '@tanstack/svelte-query' import { sleep } from '../utils' import type { StatusResult } from '../utils' + import { untrack } from 'svelte' - let { key, states } = $props<{ + let { key, states = $bindable() } = $props<{ key: Array states: Array> }>() - const state = createQueries({ + const s = createQueries({ queries: [ { queryKey: key, @@ -21,11 +22,14 @@ }) $effect(() => { - states = [...states, state[0]] + JSON.stringify(s) + untrack(() => { + states.push($state.snapshot(s[0])) + }) })
-

{state[0].data}

-

fetchStatus: {state[0].fetchStatus}

+

{s[0].data}

+

fetchStatus: {s[0].fetchStatus}

diff --git a/packages/svelte-query-persist-client/vite.config.ts b/packages/svelte-query-persist-client/vite.config.ts index e02efe3d48..af34d9a112 100644 --- a/packages/svelte-query-persist-client/vite.config.ts +++ b/packages/svelte-query-persist-client/vite.config.ts @@ -9,7 +9,7 @@ export default defineConfig({ name: packageJson.name, dir: './tests', watch: false, - include: ['**/*.test.svelte.ts'], + include: ['**/*.svelte.ts'], environment: 'jsdom', setupFiles: ['./tests/test-setup.ts'], coverage: { enabled: true, provider: 'istanbul', include: ['src/**/*'] }, diff --git a/packages/svelte-query/src/createBaseQuery.svelte.ts b/packages/svelte-query/src/createBaseQuery.svelte.ts index b8352753d8..de1db59e8d 100644 --- a/packages/svelte-query/src/createBaseQuery.svelte.ts +++ b/packages/svelte-query/src/createBaseQuery.svelte.ts @@ -124,18 +124,5 @@ export function createBaseQuery< //console.log('result effect', final_.value.data) }) }) - //@ts-expect-error - return new Proxy(final_, { - get(target, p) { - if (p == 'value') { - return target.value - } - if (p == 'JSON') { - return target.value - } - // console.log('target value', p, target.value, target.value[p]) - //@ts-expect-error - return target.value[p] - }, - }) + return final_.value }