snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
persistQueryClientRestore,
|
||||
persistQueryClientSubscribe,
|
||||
} from '@tanstack/query-persist-client-core'
|
||||
import {
|
||||
QueryClientProvider,
|
||||
setIsRestoringContext,
|
||||
} from '@tanstack/svelte-query'
|
||||
import { box } from './utils.svelte.js'
|
||||
import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core'
|
||||
import type {
|
||||
OmitKeyof,
|
||||
QueryClientProviderProps,
|
||||
} from '@tanstack/svelte-query'
|
||||
|
||||
type PersistQueryClientProviderProps = QueryClientProviderProps & {
|
||||
persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
|
||||
onSuccess?: () => void
|
||||
onError?: () => void
|
||||
}
|
||||
|
||||
let {
|
||||
client,
|
||||
children,
|
||||
persistOptions,
|
||||
...props
|
||||
}: PersistQueryClientProviderProps = $props()
|
||||
|
||||
let isRestoring = box(true)
|
||||
|
||||
setIsRestoringContext(isRestoring)
|
||||
|
||||
const options = $derived({
|
||||
...persistOptions,
|
||||
queryClient: client,
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
return isRestoring.current ? () => {} : persistQueryClientSubscribe(options)
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
isRestoring.current = true
|
||||
persistQueryClientRestore(options)
|
||||
.then(() => props.onSuccess?.())
|
||||
.catch(() => props.onError?.())
|
||||
.finally(() => {
|
||||
isRestoring.current = false
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
<QueryClientProvider {client} {...props}>
|
||||
{@render children()}
|
||||
</QueryClientProvider>
|
||||
3
packages/svelte-query-persist-client/src/index.ts
Normal file
3
packages/svelte-query-persist-client/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
// Re-export core
|
||||
export * from '@tanstack/query-persist-client-core'
|
||||
export { default as PersistQueryClientProvider } from './PersistQueryClientProvider.svelte'
|
||||
14
packages/svelte-query-persist-client/src/utils.svelte.ts
Normal file
14
packages/svelte-query-persist-client/src/utils.svelte.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
type Box<T> = { current: T }
|
||||
|
||||
export function box<T>(initial: T): Box<T> {
|
||||
let current = $state(initial)
|
||||
|
||||
return {
|
||||
get current() {
|
||||
return current
|
||||
},
|
||||
set current(newValue) {
|
||||
current = newValue
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user