Open History to see the last commit
1import {
2 createContext,
3 createEffect,
4 createMemo,
5 createSignal,
6 onCleanup,
7 useContext,
8} from 'solid-js'
9import type { Accessor, JSX, Setter } from 'solid-js'
10import type {
11 MutationCache,
12 QueryCache,
13 QueryCacheNotifyEvent,
14} from '@tanstack/query-core'
15import { useQueryDevtoolsContext } from './QueryDevtoolsContext'
16β
17export interface DevtoolsState {
18 selectedQueryHash: Accessor<string | null>
19 setSelectedQueryHash: Setter<string | null>
20 selectedMutationId: Accessor<number | null>
21 setSelectedMutationId: Setter<number | null>
22 panelWidth: Accessor<number>
23 setPanelWidth: Setter<number>
24 offline: Accessor<boolean>
25 setOffline: Setter<boolean>
26 /**
27 * Registries of cache-subscription callbacks for this devtools instance.
28 * Previously these lived at module scope, so a cache notification for one
29 * client's cache invoked every mounted panel's callbacks with the wrong
30 * cache, and unmounting one panel cleared the other panels' registrations.
31 * See https://github.com/TanStack/query/issues/9681
32 */
33 queryCacheMap: Map<
34 (queryCache: Accessor<QueryCache>) => any,
35 {
36 setter: Setter<any>
37 shouldUpdate: (event: QueryCacheNotifyEvent) => boolean
38 }
39 >
40 mutationCacheMap: Map<(mutationCache: Accessor<MutationCache>) => any, Setter<any>>
41}
42β
43const DevtoolsStateContext = createContext<DevtoolsState | undefined>(undefined)
44β
45export function useDevtoolsState(): DevtoolsState {
46 const state = useContext(DevtoolsStateContext)
47 if (!state) {
48 throw new Error(
49 'useDevtoolsState must be used within a DevtoolsStateProvider',
50 )
51 }
52 return state
53}
54β
55export function DevtoolsStateProvider(props: { children: JSX.Element }) {
56 // This state is intentionally created inside the provider component so that
57 // every mounted devtools instance gets its own isolated copy. Previously
58 // these signals lived at module scope, which meant that interacting with
59 // one devtools panel (e.g. selecting a query) leaked into every other
60 // panel on the page. See https://github.com/TanStack/query/issues/9681
61 const onlineManager = createMemo(
62 () => useQueryDevtoolsContext().onlineManager,
63 )
64 const [selectedQueryHash, setSelectedQueryHash] = createSignal<string | null>(
65 null,
66 )
67 const [selectedMutationId, setSelectedMutationId] = createSignal<
68 number | null
69 >(null)
70 const [panelWidth, setPanelWidth] = createSignal(0)
71 const [offline, setOffline] = createSignal(!onlineManager().isOnline())
72 const queryCacheMap: DevtoolsState['queryCacheMap'] = new Map()
73 const mutationCacheMap: DevtoolsState['mutationCacheMap'] = new Map()
74β
75 // The online/offline subscription lives here (rather than in `Devtools`)
76 // so that panel-only instances, which render `ContentView` without
77 // `Devtools`, reflect their configured onlineManager too.
78 createEffect(() => {
79 const manager = onlineManager()
80 setOffline(!manager.isOnline())
81 const unsubscribe = manager.subscribe((online) => {
82 setOffline(!online)
83 })
84 onCleanup(unsubscribe)
85 })
86β
87 return (
88 <DevtoolsStateContext.Provider
89 value={{
90 selectedQueryHash,
91 setSelectedQueryHash,
92 selectedMutationId,
93 setSelectedMutationId,
94 panelWidth,
95 setPanelWidth,
96 offline,
97 setOffline,
98 queryCacheMap,
99 mutationCacheMap,
100 }}
101 >
102 {props.children}
103 </DevtoolsStateContext.Provider>
104 )
105}