Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

BroadcastQueryClientProvider.svelte

57 lines1.4 KB

1<script lang="ts">
2 import { broadcastQueryClientRestore } from '@tanstack/query-broadcast-client-experimental'
3 import {
4 QueryClientProvider,
5 getIsRestoringContext,
6 setIsRestoringContext,
7 } from '@tanstack/svelte-query'
8 import { box } from './utils.svelte.js'
9 import type { BroadcastQueryClientRestoreOptions } from '@tanstack/query-broadcast-client-experimental'
10 import type { QueryClientProviderProps } from '@tanstack/svelte-query'
11​
12 type BroadcastQueryClientProviderProps = QueryClientProviderProps & {
13 broadcastOptions: Omit<BroadcastQueryClientRestoreOptions, 'queryClient'>
14 }
15​
16 let {
17 client,
18 children,
19 broadcastOptions,
20 ...props
21 }: BroadcastQueryClientProviderProps = $props()
22​
23 const parentIsRestoring = getIsRestoringContext()
24 const isRestoring = box(true)
25​
26 setIsRestoringContext({
27 get current() {
28 return parentIsRestoring.current || isRestoring.current
29 },
30 })
31​
32 const options = $derived({
33 ...broadcastOptions,
34 queryClient: client,
35 })
36​
37 $effect(() => {
38 isRestoring.current = true
39 const [cleanup, restorePromise] = broadcastQueryClientRestore(options)
40 let mounted = true
41​
42 restorePromise.then(() => {
43 if (mounted) {
44 isRestoring.current = false
45 }
46 })
47​
48 return () => {
49 mounted = false
50 cleanup()
51 }
52 })
53</script>
54​
55<QueryClientProvider {client} {...props}>
56 {@render children?.()}
57</QueryClientProvider>