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

devtools.ts

255 lines6.8 KB

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(query)
94 },
95 },
96 {
97 icon: 'settings_backup_restore',
98 tooltip: 'Reset',
99 action: (queryHash: string) => {
100 queryCache.get(queryHash)?.reset()
101 },
102 },
103 {
104 icon: 'delete',
105 tooltip: 'Remove',
106 action: (queryHash: string) => {
107 const query = queryCache.get(queryHash) as Query
108 queryCache.remove(query)
109 },
110 },
111 {
112 icon: 'hourglass_empty',
113 tooltip: 'Force loading',
114 action: (queryHash: string) => {
115 const query = queryCache.get(queryHash) as Query
116​
117 query.setState({
118 data: undefined,
119 status: 'pending',
120 })
121 },
122 },
123 {
124 icon: 'error_outline',
125 tooltip: 'Force error',
126 action: (queryHash: string) => {
127 const query = queryCache.get(queryHash) as Query
128​
129 query.setState({
130 data: undefined,
131 status: 'error',
132 error: new Error('Unknown error from devtools'),
133 })
134 },
135 },
136 ],
137 })
138​
139 api.addTimelineLayer({
140 id: pluginId,
141 label: pluginName,
142 color: 0xffd94c,
143 })
144​
145 queryCache.subscribe((event) => {
146 api.sendInspectorTree(pluginId)
147 api.sendInspectorState(pluginId)
148​
149 const queryEvents: Array<QueryCacheNotifyEvent['type']> = [
150 'added',
151 'removed',
152 'updated',
153 ]
154​
155 if (queryEvents.includes(event.type)) {
156 api.addTimelineEvent({
157 layerId: pluginId,
158 event: {
159 title: event.type,
160 subtitle: event.query.queryHash,
161 time: api.now(),
162 data: {
163 queryHash: event.query.queryHash,
164 ...event,
165 },
166 },
167 })
168 }
169 })
170​
171 api.on.setPluginSettings((payload) => {
172 if (payload.key === 'onlineMode') {
173 onlineManager.setOnline(Boolean(payload.newValue))
174 }
175 })
176​
177 api.on.getInspectorTree((payload) => {
178 if (payload.inspectorId === pluginId) {
179 const queries = queryCache.getAll()
180 const settings = api.getSettings()
181​
182 const filtered = payload.filter
183 ? queries.filter(
184 (item) => rankItem(item.queryHash, payload.filter).passed,
185 )
186 : [...queries]
187​
188 const sorted = filtered.sort(
189 (a, b) => sortFns[settings.sortFn]!(a, b) * settings.baseSort,
190 )
191​
192 const nodes: Array<CustomInspectorNode> = sorted.map((query) => {
193 const stateLabel = getQueryStateLabel(query)
194​
195 return {
196 id: query.queryHash,
197 label: query.queryHash,
198 tags: [
199 {
200 label: `${stateLabel} [${query.getObserversCount()}]`,
201 textColor: getQueryStatusFg(query),
202 backgroundColor: getQueryStatusBg(query),
203 },
204 ],
205 }
206 })
207 payload.rootNodes = nodes
208 }
209 })
210​
211 api.on.getInspectorState((payload) => {
212 if (payload.inspectorId === pluginId) {
213 const query = queryCache.get(payload.nodeId)
214​
215 if (!query) {
216 return
217 }
218​
219 payload.state = {
220 ' Query Details': [
221 {
222 key: 'Query key',
223 value: query.queryHash,
224 },
225 {
226 key: 'Query status',
227 value: getQueryStateLabel(query),
228 },
229 {
230 key: 'Observers',
231 value: query.getObserversCount(),
232 },
233 {
234 key: 'Last Updated',
235 value: new Date(query.state.dataUpdatedAt).toLocaleTimeString(),
236 },
237 ],
238 'Data Explorer': [
239 {
240 key: 'Data',
241 value: query.state.data,
242 },
243 ],
244 'Query Explorer': [
245 {
246 key: 'Query',
247 value: query,
248 },
249 ],
250 }
251 }
252 })
253 },
254 )