Open History to see the last commit
1import { getContext, setContext } from 'svelte'
2import type { QueryClient } from '@tanstack/query-core'
3import type { Box } from './containers.svelte'
4β
5const _contextKey = Symbol('QueryClient')
6β
7/** Retrieves a Client from Svelte's context */
8export const getQueryClientContext = (): QueryClient => {
9 const client = getContext<QueryClient | undefined>(_contextKey)
10 if (!client) {
11 throw new Error(
12 'No QueryClient was found in Svelte context. Did you forget to wrap your component with QueryClientProvider?',
13 )
14 }
15β
16 return client
17}
18β
19/** Sets a QueryClient on Svelte's context */
20export const setQueryClientContext = (client: QueryClient): void => {
21 setContext(_contextKey, client)
22}
23β
24const _isRestoringContextKey = Symbol('isRestoring')
25β
26/** Retrieves a `isRestoring` from Svelte's context */
27export const getIsRestoringContext = (): Box<boolean> => {
28 try {
29 const isRestoring = getContext<Box<boolean> | undefined>(
30 _isRestoringContextKey,
31 )
32 return isRestoring ?? { current: false }
33 } catch (error) {
34 return { current: false }
35 }
36}
37β
38/** Sets a `isRestoring` on Svelte's context */
39export const setIsRestoringContext = (isRestoring: Box<boolean>): void => {
40 setContext(_isRestoringContextKey, isRestoring)
41}