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

devtools.ts

209 lines5.5 KB

1/* istanbul ignore file */
2​
3import { setupDevtoolsPlugin } from '@vue/devtools-api'
4import { rankItem } from '@tanstack/match-sorter-utils'
5import {
6 getQueryStateLabel,
7 getQueryStatusBg,
8 getQueryStatusFg,
9 sortFns,
10} from './utils'
11import type { CustomInspectorNode } from '@vue/devtools-api'
12import type { Query, QueryCacheNotifyEvent } from '@tanstack/query-core'
13import type { QueryClient } from '../queryClient'
14​
15const pluginId = 'vue-query'
16const pluginName = 'Vue Query'
17​
18export function setupDevtools(app: any, queryClient: QueryClient) {
19 setupDevtoolsPlugin(
20 {
21 id: pluginId,
22 label: pluginName,
23 packageName: 'vue-query',
24 homepage: 'https://tanstack.com/query/v4',
25 logo: 'https://vue-query.vercel.app/vue-query.svg',
26 app,
27 settings: {
28 baseSort: {
29 type: 'choice',
30 component: 'button-group',
31 label: 'Sort Cache Entries',
32 options: [
33 {
34 label: 'ASC',
35 value: 1,
36 },
37 {
38 label: 'DESC',
39 value: -1,
40 },
41 ],
42 defaultValue: 1,
43 },
44 sortFn: {
45 type: 'choice',
46 label: 'Sort Function',
47 options: Object.keys(sortFns).map((key) => ({
48 label: key,
49 value: key,
50 })),
51 defaultValue: Object.keys(sortFns)[0]!,
52 },
53 },
54 },
55 (api) => {
56 const queryCache = queryClient.getQueryCache()
57​
58 api.addInspector({
59 id: pluginId,
60 label: pluginName,
61 icon: 'api',
62 nodeActions: [
63 {
64 icon: 'cloud_download',
65 tooltip: 'Refetch',
66 action: (queryHash: string) => {
67 queryCache.get(queryHash)?.fetch()
68 },
69 },
70 {
71 icon: 'alarm',
72 tooltip: 'Invalidate',
73 action: (queryHash: string) => {
74 const query = queryCache.get(queryHash) as Query
75 queryClient.invalidateQueries(query.queryKey)
76 },
77 },
78 {
79 icon: 'settings_backup_restore',
80 tooltip: 'Reset',
81 action: (queryHash: string) => {
82 queryCache.get(queryHash)?.reset()
83 },
84 },
85 {
86 icon: 'delete',
87 tooltip: 'Remove',
88 action: (queryHash: string) => {
89 const query = queryCache.get(queryHash) as Query
90 queryCache.remove(query)
91 },
92 },
93 ],
94 })
95​
96 api.addTimelineLayer({
97 id: pluginId,
98 label: pluginName,
99 color: 0xffd94c,
100 })
101​
102 queryCache.subscribe((event) => {
103 api.sendInspectorTree(pluginId)
104 api.sendInspectorState(pluginId)
105​
106 const queryEvents: QueryCacheNotifyEvent['type'][] = [
107 'added',
108 'removed',
109 'updated',
110 ]
111 if (
112 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
113 event &&
114 queryEvents.includes(event.type)
115 ) {
116 api.addTimelineEvent({
117 layerId: pluginId,
118 event: {
119 title: event.type,
120 subtitle: event.query.queryHash,
121 time: api.now(),
122 data: {
123 queryHash: event.query.queryHash,
124 ...event,
125 },
126 },
127 })
128 }
129 })
130​
131 api.on.getInspectorTree((payload) => {
132 if (payload.inspectorId === pluginId) {
133 const queries = queryCache.getAll()
134 const settings = api.getSettings()
135​
136 const filtered = payload.filter
137 ? queries.filter(
138 (item) => rankItem(item.queryHash, payload.filter).passed,
139 )
140 : [...queries]
141​
142 const sorted = filtered.sort(
143 (a, b) => sortFns[settings.sortFn]!(a, b) * settings.baseSort,
144 )
145​
146 const nodes: CustomInspectorNode[] = sorted.map((query) => {
147 const stateLabel = getQueryStateLabel(query)
148​
149 return {
150 id: query.queryHash,
151 label: query.queryHash,
152 tags: [
153 {
154 label: `${stateLabel} [${query.getObserversCount()}]`,
155 textColor: getQueryStatusFg(query),
156 backgroundColor: getQueryStatusBg(query),
157 },
158 ],
159 }
160 })
161 payload.rootNodes = nodes
162 }
163 })
164​
165 api.on.getInspectorState((payload) => {
166 if (payload.inspectorId === pluginId) {
167 const query = queryCache.get(payload.nodeId)
168​
169 if (!query) {
170 return
171 }
172​
173 payload.state = {
174 ' Query Details': [
175 {
176 key: 'Query key',
177 value: query.queryHash as string,
178 },
179 {
180 key: 'Query status',
181 value: getQueryStateLabel(query),
182 },
183 {
184 key: 'Observers',
185 value: query.getObserversCount(),
186 },
187 {
188 key: 'Last Updated',
189 value: new Date(query.state.dataUpdatedAt).toLocaleTimeString(),
190 },
191 ],
192 'Data Explorer': [
193 {
194 key: 'Data',
195 value: query.state.data,
196 },
197 ],
198 'Query Explorer': [
199 {
200 key: 'Query',
201 value: query,
202 },
203 ],
204 }
205 }
206 })
207 },
208 )