Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
zhihengGet committed Jul 17, 2024
1 parent 12b0ed8 commit 24f970c
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
client,
children,
persistOptions,
onSuccess,
...props
}: PersistQueryClientProviderProps = $props()
Expand All @@ -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
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
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<string>
states: Array<StatusResult<string>>
fetched: boolean
Expand All @@ -12,7 +13,7 @@
const state = createQuery({
queryKey: key,
queryFn: async () => {
fetched.set(true)
states.push('fetched')
await sleep(10)
return 'fetched'
},
Expand All @@ -21,7 +22,10 @@
})
$effect(() => {
states = [...states, state]
console.log('abdeff', state)
untrack(() => {
states.push(state.data)
})
})
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let states: Array<StatusResult<string>>
export let fetched: boolean
interface Props {
queryClient: QueryClient
persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
key: Array<string>
states: Array<StatusResult<string>>
fetched: boolean
}
let { queryClient, persistOptions, key, states, fetched }: Props = $props()
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions}>
<FreshData {key} {states} {fetched} />
<FreshData {key} bind:states {fetched} />
</PersistQueryClientProvider>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { sleep } from '../utils'
import type { StatusResult } from '../utils'
let { key, states } = $props<{
let { key, states = $bindable() } = $props<{
key: Array<string>
states: Array<StatusResult<string>>
}>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core'
import type { StatusResult } from '../utils'
export let queryClient: QueryClient
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let states: Array<StatusResult<string>>
interface Props {
queryClient: QueryClient
persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
key: Array<string>
states: Array<StatusResult<string>>
}
let { queryClient, persistOptions, key, states }: Props = $props()
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import { createQuery } from '@tanstack/svelte-query'
import { sleep } from '../utils'
export let key: Array<string>
interface Props {
key: Array<string>
}
let { key }: Props = $props()
const state = createQuery({
queryKey: key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let onSuccess: () => void
interface Props {
queryClient: QueryClient
persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
key: Array<string>
onSuccess: () => void
}
let { queryClient, persistOptions, key, onSuccess }: Props = $props()
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions} {onSuccess}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe('PersistQueryClientProvider', () => {
})
})

test('should not refetch after restoring when data is fresh', async () => {
test.only('should not refetch after restoring when data is fresh', async () => {
const key = queryKey()
const states: Array<StatusResult<string>> = $state([])

Expand Down Expand Up @@ -302,7 +302,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()
Expand Down Expand Up @@ -335,6 +335,7 @@ describe('PersistQueryClientProvider', () => {
await waitFor(() => rendered.getByText('hydrated'))
await waitFor(() => rendered.getByText('fetched'))

await sleep(400)
expect(states).toEqual([
'onSuccess',
'onSuccess done',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
interface Props {
queryClient: QueryClient
persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
key: Array<string>
}
let { queryClient, persistOptions, key }: Props = $props()
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import { createQuery } from '@tanstack/svelte-query'
import { sleep } from '../utils'
export let key: Array<string>
interface Props {
key: Array<string>
}
let { key }: Props = $props()
const state = createQuery({
queryKey: key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core'
import type { StatusResult } from '../utils'
export let queryClient: QueryClient
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let states: Array<StatusResult<string>>
interface Props {
queryClient: QueryClient
persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
key: Array<string>
states: Array<StatusResult<string>>
}
let { queryClient, persistOptions, key, states }: Props = $props()
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { sleep } from '../utils'
import type { StatusResult } from '../utils'
let { key, states } = $props<{
let { key, states = $bindable() } = $props<{
key: Array<string>
states: Array<StatusResult<string>>
}>()
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte-query-persist-client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*'] },
Expand Down

0 comments on commit 24f970c

Please sign in to comment.