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

devtools.ts

204 lines5.4 KB

1/* istanbul ignore file */
2​
3import { setupDevtoolsPlugin } from '@vue/devtools-api'
4import type { CustomInspectorNode } from '@vue/devtools-api'
5import { rankItem } from '@tanstack/match-sorter-utils'
6import type { Query } from '@tanstack/query-core'
7import type { QueryClient } from '../queryClient'
8import {
9 getQueryStateLabel,
10 getQueryStatusBg,
11 getQueryStatusFg,
12 sortFns,
13} from './utils'
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://github.com/DamianOsipiuk/vue-query',
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 if (
107 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
108 event &&
109 ['queryAdded', 'queryRemoved', 'queryUpdated'].includes(event.type)
110 ) {
111 api.addTimelineEvent({
112 layerId: pluginId,
113 event: {
114 title: event.type,
115 subtitle: event.query.queryHash,
116 time: api.now(),
117 data: {
118 queryHash: event.query.queryHash,
119 ...event,
120 },
121 },
122 })
123 }
124 })
125​
126 api.on.getInspectorTree((payload) => {
127 if (payload.inspectorId === pluginId) {
128 const queries = queryCache.getAll()
129 const settings = api.getSettings()
130​
131 const filtered = payload.filter
132 ? queries.filter(
133 (item) => rankItem(item.queryHash, payload.filter).passed,
134 )
135 : [...queries]
136​
137 const sorted = filtered.sort(
138 (a, b) => sortFns[settings.sortFn]!(a, b) * settings.baseSort,
139 )
140​
141 const nodes: CustomInspectorNode[] = sorted.map((query) => {
142 const stateLabel = getQueryStateLabel(query)
143​
144 return {
145 id: query.queryHash,
146 label: query.queryHash,
147 tags: [
148 {
149 label: `${stateLabel} [${query.getObserversCount()}]`,
150 textColor: getQueryStatusFg(query),
151 backgroundColor: getQueryStatusBg(query),
152 },
153 ],
154 }
155 })
156 payload.rootNodes = nodes
157 }
158 })
159​
160 api.on.getInspectorState((payload) => {
161 if (payload.inspectorId === pluginId) {
162 const query = queryCache.get(payload.nodeId)
163​
164 if (!query) {
165 return
166 }
167​
168 payload.state = {
169 ' Query Details': [
170 {
171 key: 'Query key',
172 value: query.queryHash as string,
173 },
174 {
175 key: 'Query status',
176 value: getQueryStateLabel(query),
177 },
178 {
179 key: 'Observers',
180 value: query.getObserversCount(),
181 },
182 {
183 key: 'Last Updated',
184 value: new Date(query.state.dataUpdatedAt).toLocaleTimeString(),
185 },
186 ],
187 'Data Explorer': [
188 {
189 key: 'Data',
190 value: query.state.data,
191 },
192 ],
193 'Query Explorer': [
194 {
195 key: 'Query',
196 value: query,
197 },
198 ],
199 }
200 }
201 })
202 },
203 )