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

context.ts

41 lines1.2 KB

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
18​
19/** Sets a QueryClient on Svelte's context */
20export const setQueryClientContext = (client: QueryClient): void => {
21 setContext(_contextKey, client)
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 }
37​
38/** Sets a `isRestoring` on Svelte's context */
39export const setIsRestoringContext = (isRestoring: Box<boolean>): void => {
40 setContext(_isRestoringContextKey, isRestoring)