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