Open History to see the last commit
1import { setupDevtoolsPlugin } from '@vue/devtools-api'
2import { rankItem } from '@tanstack/match-sorter-utils'
3import { onlineManager } from '@tanstack/query-core'
4import {
5 getQueryStateLabel,
6 getQueryStatusBg,
7 getQueryStatusFg,
8 sortFns,
9} from './utils'
10import type { CustomInspectorNode } from '@vue/devtools-api'
11import type { Query, QueryCacheNotifyEvent } from '@tanstack/query-core'
12import type { QueryClient } from '../queryClient'
13β
14const pluginId = 'vue-query'
15const pluginName = 'Vue Query'
16β
17/**
18 * Registers the Vue Query plugin with the Vue devtools, adding a query
19 * inspector and a timeline layer for query cache events.
20 *
21 * @param app - The Vue app instance.
22 * @param queryClient - The client whose query cache is shown in the devtools.
23 */
24export function setupDevtools(app: any, queryClient: QueryClient) {
25 setupDevtoolsPlugin(
26 {
27 id: pluginId,
28 label: pluginName,
29 packageName: 'vue-query',
30 homepage: 'https://tanstack.com/query/latest',
31 logo: 'https://raw.githubusercontent.com/TanStack/query/main/packages/vue-query/media/vue-query.svg',
32 app,
33 settings: {
34 baseSort: {
35 type: 'choice',
36 component: 'button-group',
37 label: 'Sort Cache Entries',
38 options: [
39 {
40 label: 'ASC',
41 value: 1,
42 },
43 {
44 label: 'DESC',
45 value: -1,
46 },
47 ],
48 defaultValue: 1,
49 },
50 sortFn: {
51 type: 'choice',
52 label: 'Sort Function',
53 options: Object.keys(sortFns).map((key) => ({
54 label: key,
55 value: key,
56 })),
57 defaultValue: Object.keys(sortFns)[0]!,
58 },
59 onlineMode: {
60 type: 'choice',
61 component: 'button-group',
62 label: 'Online mode',
63 options: [
64 {
65 label: 'Online',
66 value: 1,
67 },
68 {
69 label: 'Offline',
70 value: 0,
71 },
72 ],
73 defaultValue: 1,
74 },
75 },
76 },
77 (api) => {
78 const initialSettings = api.getSettings()
79 onlineManager.setOnline(Boolean(initialSettings.onlineMode.valueOf()))
80β
81 const queryCache = queryClient.getQueryCache()
82β
83 api.addInspector({
84 id: pluginId,
85 label: pluginName,
86 icon: 'api',
87 nodeActions: [
88 {
89 icon: 'file_download',
90 tooltip: 'Refetch',
91 action: (queryHash: string) => {
92 queryCache.get(queryHash)?.fetch()
93 },
94 },
95 {
96 icon: 'alarm',
97 tooltip: 'Invalidate',
98 action: (queryHash: string) => {
99 const query = queryCache.get(queryHash) as Query
100 queryClient.invalidateQueries({
101 queryKey: query.queryKey,
102 exact: true,
103 })
104 },
105 },
106 {
107 icon: 'settings_backup_restore',
108 tooltip: 'Reset',
109 action: (queryHash: string) => {
110 queryCache.get(queryHash)?.reset()
111 },
112 },
113 {
114 icon: 'delete',
115 tooltip: 'Remove',
116 action: (queryHash: string) => {
117 const query = queryCache.get(queryHash) as Query
118 queryCache.remove(query)
119 },
120 },
121 {
122 icon: 'hourglass_empty',
123 tooltip: 'Force loading',
124 action: (queryHash: string) => {
125 const query = queryCache.get(queryHash) as Query
126β
127 query.setState({
128 data: undefined,
129 status: 'pending',
130 })
131 },
132 },
133 {
134 icon: 'error_outline',
135 tooltip: 'Force error',
136 action: (queryHash: string) => {
137 const query = queryCache.get(queryHash) as Query
138β
139 query.setState({
140 data: undefined,
141 status: 'error',
142 error: new Error('Unknown error from devtools'),
143 })
144 },
145 },
146 ],
147 })
148β
149 api.addTimelineLayer({
150 id: pluginId,
151 label: pluginName,
152 color: 0xffd94c,
153 })
154β
155 queryCache.subscribe((event) => {
156 api.sendInspectorTree(pluginId)
157 api.sendInspectorState(pluginId)
158β
159 const queryEvents: Array<QueryCacheNotifyEvent['type']> = [
160 'added',
161 'removed',
162 'updated',
163 ]
164β
165 if (queryEvents.includes(event.type)) {
166 api.addTimelineEvent({
167 layerId: pluginId,
168 event: {
169 title: event.type,
170 subtitle: event.query.queryHash,
171 time: api.now(),
172 data: {
173 queryHash: event.query.queryHash,
174 ...event,
175 },
176 },
177 })
178 }
179 })
180β
181 api.on.setPluginSettings((payload) => {
182 if (payload.key === 'onlineMode') {
183 onlineManager.setOnline(Boolean(payload.newValue))
184 }
185 })
186β
187 api.on.getInspectorTree((payload) => {
188 if (payload.inspectorId === pluginId) {
189 const queries = queryCache.getAll()
190 const settings = api.getSettings()
191β
192 const filtered = payload.filter
193 ? queries.filter(
194 (item) => rankItem(item.queryHash, payload.filter).passed,
195 )
196 : [...queries]
197β
198 const sorted = filtered.sort(
199 (a, b) => sortFns[settings.sortFn]!(a, b) * settings.baseSort,
200 )
201β
202 const nodes: Array<CustomInspectorNode> = sorted.map((query) => {
203 const stateLabel = getQueryStateLabel(query)
204β
205 return {
206 id: query.queryHash,
207 label: query.queryHash,
208 tags: [
209 {
210 label: `${stateLabel} [${query.getObserversCount()}]`,
211 textColor: getQueryStatusFg(query),
212 backgroundColor: getQueryStatusBg(query),
213 },
214 ],
215 }
216 })
217 payload.rootNodes = nodes
218 }
219 })
220β
221 api.on.getInspectorState((payload) => {
222 if (payload.inspectorId === pluginId) {
223 const query = queryCache.get(payload.nodeId)
224β
225 if (!query) {
226 return
227 }
228β
229 payload.state = {
230 ' Query Details': [
231 {
232 key: 'Query key',
233 value: query.queryHash,
234 },
235 {
236 key: 'Query status',
237 value: getQueryStateLabel(query),
238 },
239 {
240 key: 'Observers',
241 value: query.getObserversCount(),
242 },
243 {
244 key: 'Last Updated',
245 value: new Date(query.state.dataUpdatedAt).toLocaleTimeString(),
246 },
247 ],
248 'Data Explorer': [
249 {
250 key: 'Data',
251 value: query.state.data,
252 },
253 ],
254 'Query Explorer': [
255 {
256 key: 'Query',
257 value: query,
258 },
259 ],
260 }
261 }
262 })
263 },
264 )
265}