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