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

DevtoolsOfflineProvider.tsx

37 lines1.2 KB

1import { createEffect, createSignal, onCleanup } from 'solid-js'
2import { DevtoolsOfflineContext } from '../contexts/DevtoolsOfflineContext'
3import { useQueryDevtoolsContext } from '../contexts/QueryDevtoolsContext'
4import type { ParentProps } from 'solid-js'
5import type { DevtoolsOfflineState } from '../contexts/types'
6​
7/**
8 * Provides its children with the offline state of the devtools `onlineManager`, and a function
9 * that toggles it.
10 * @param props - The `children` to render.
11 * @returns The `children`, wrapped in the context provider.
12 */
13export function DevtoolsOfflineProvider(props: ParentProps) {
14 const [offline, setOffline] = createSignal(false)
15 const context = useQueryDevtoolsContext()
16​
17 createEffect(() => {
18 const manager = context.onlineManager
19 setOffline(!manager.isOnline())
20 const unsubscribe = manager.subscribe((online) => setOffline(!online))
21 onCleanup(unsubscribe)
22 })
23​
24 const state: DevtoolsOfflineState = {
25 offline,
26 toggleOffline: () => {
27 const manager = context.onlineManager
28 manager.setOnline(!manager.isOnline())
29 },
30 }
31​
32 return (
33 <DevtoolsOfflineContext.Provider value={state}>
34 {props.children}
35 </DevtoolsOfflineContext.Provider>
36 )