-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathstate.ts
234 lines (202 loc) · 6.6 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import type { AppRecord, CustomCommand, CustomTab } from '../types'
import { target as global, isUrlString } from '@vue/devtools-shared'
import { debounce } from 'perfect-debounce'
import { devtoolsContext } from '.'
import { getTimelineLayersStateFromStorage } from '../core/timeline/storage'
import { DevToolsMessagingHookKeys } from './hook'
export interface DevToolsAppRecords extends AppRecord {}
export interface DevToolsState {
connected: boolean
clientConnected: boolean
vitePluginDetected: boolean
appRecords: DevToolsAppRecords[]
activeAppRecordId: string
tabs: CustomTab[]
commands: CustomCommand[]
highPerfModeEnabled: boolean
devtoolsClientDetected: {
[key: string]: boolean
}
perfUniqueGroupId: number
timelineLayersState: Record<string, boolean>
}
global.__VUE_DEVTOOLS_KIT_APP_RECORDS__ ??= []
global.__VUE_DEVTOOLS_KIT_ACTIVE_APP_RECORD__ ??= {}
global.__VUE_DEVTOOLS_KIT_ACTIVE_APP_RECORD_ID__ ??= ''
global.__VUE_DEVTOOLS_KIT_CUSTOM_TABS__ ??= []
global.__VUE_DEVTOOLS_KIT_CUSTOM_COMMANDS__ ??= []
const STATE_KEY = '__VUE_DEVTOOLS_KIT_GLOBAL_STATE__'
function initStateFactory() {
return {
connected: false,
clientConnected: false,
vitePluginDetected: true,
appRecords: [],
activeAppRecordId: '',
tabs: [],
commands: [],
highPerfModeEnabled: true,
devtoolsClientDetected: {},
perfUniqueGroupId: 0,
timelineLayersState: getTimelineLayersStateFromStorage(),
}
}
global[STATE_KEY] ??= initStateFactory()
export const callStateUpdatedHook = debounce((state: DevToolsState) => {
devtoolsContext.hooks.callHook(DevToolsMessagingHookKeys.DEVTOOLS_STATE_UPDATED, { state })
})
export const callConnectedUpdatedHook = debounce((state: DevToolsState, oldState: DevToolsState) => {
devtoolsContext.hooks.callHook(DevToolsMessagingHookKeys.DEVTOOLS_CONNECTED_UPDATED, { state, oldState })
})
export const devtoolsAppRecords = new Proxy<DevToolsAppRecords[] & { value: DevToolsAppRecords[] }>(global.__VUE_DEVTOOLS_KIT_APP_RECORDS__, {
get(_target, prop, receiver) {
if (prop === 'value')
return global.__VUE_DEVTOOLS_KIT_APP_RECORDS__
return global.__VUE_DEVTOOLS_KIT_APP_RECORDS__[prop]
},
})
export const addDevToolsAppRecord = (app: AppRecord) => {
global.__VUE_DEVTOOLS_KIT_APP_RECORDS__ = [
...global.__VUE_DEVTOOLS_KIT_APP_RECORDS__,
app,
]
}
export const removeDevToolsAppRecord = (app: AppRecord['app']) => {
global.__VUE_DEVTOOLS_KIT_APP_RECORDS__ = devtoolsAppRecords.value.filter(record => record.app !== app)
}
export const activeAppRecord = new Proxy<AppRecord & { value: AppRecord, id: string }>(global.__VUE_DEVTOOLS_KIT_ACTIVE_APP_RECORD__, {
get(_target, prop, receiver) {
if (prop === 'value')
return global.__VUE_DEVTOOLS_KIT_ACTIVE_APP_RECORD__
else if (prop === 'id')
return global.__VUE_DEVTOOLS_KIT_ACTIVE_APP_RECORD_ID__
return global.__VUE_DEVTOOLS_KIT_ACTIVE_APP_RECORD__[prop]
},
})
// @TODO: refactor
function updateAllStates() {
callStateUpdatedHook({
...global[STATE_KEY],
appRecords: devtoolsAppRecords.value,
activeAppRecordId: activeAppRecord.id,
tabs: global.__VUE_DEVTOOLS_KIT_CUSTOM_TABS__,
commands: global.__VUE_DEVTOOLS_KIT_CUSTOM_COMMANDS__,
})
}
export function setActiveAppRecord(app: AppRecord) {
global.__VUE_DEVTOOLS_KIT_ACTIVE_APP_RECORD__ = app
updateAllStates()
}
export function setActiveAppRecordId(id: string) {
global.__VUE_DEVTOOLS_KIT_ACTIVE_APP_RECORD_ID__ = id
updateAllStates()
}
export const devtoolsState: DevToolsState = new Proxy(global[STATE_KEY], {
get(target, property) {
if (property === 'appRecords') {
return devtoolsAppRecords
}
else if (property === 'activeAppRecordId') {
return activeAppRecord.id
}
else if (property === 'tabs') {
return global.__VUE_DEVTOOLS_KIT_CUSTOM_TABS__
}
else if (property === 'commands') {
return global.__VUE_DEVTOOLS_KIT_CUSTOM_COMMANDS__
}
return global[STATE_KEY][property]
},
deleteProperty(target, property) {
delete target[property]
return true
},
set(target, property, value) {
const oldState = { ...global[STATE_KEY] }
target[property] = value
global[STATE_KEY][property] = value
return true
},
})
export function resetDevToolsState() {
Object.assign(global[STATE_KEY], initStateFactory())
}
export function updateDevToolsState(state: Partial<DevToolsState>) {
const oldState = {
...global[STATE_KEY],
appRecords: devtoolsAppRecords.value,
activeAppRecordId: activeAppRecord.id,
}
if (
(oldState.connected !== state.connected && state.connected)
|| (oldState.clientConnected !== state.clientConnected && state.clientConnected)
) {
callConnectedUpdatedHook(global[STATE_KEY], oldState)
}
Object.assign(global[STATE_KEY], state)
updateAllStates()
}
export function onDevToolsConnected(fn: () => void) {
return new Promise<void>((resolve) => {
if (devtoolsState.connected) {
fn()
resolve()
}
devtoolsContext.hooks.hook(DevToolsMessagingHookKeys.DEVTOOLS_CONNECTED_UPDATED, ({ state }) => {
if (state.connected) {
fn()
resolve()
}
})
})
}
const resolveIcon = (icon?: string) => {
if (!icon)
return
if (icon.startsWith('baseline-')) {
return `custom-ic-${icon}`
}
// devtools internal custom tab icons are starts with `i-` prefix, render as it is set in unocss safelist
// or if it's a url
if (icon.startsWith('i-') || isUrlString(icon))
return icon
// for custom-tab, we use `custom-ic-` prefix
return `custom-ic-baseline-${icon}`
}
export function addCustomTab(tab: CustomTab) {
const tabs = global.__VUE_DEVTOOLS_KIT_CUSTOM_TABS__
if (tabs.some((t: CustomTab) => t.name === tab.name))
return
tabs.push({
...tab,
icon: resolveIcon(tab.icon),
})
updateAllStates()
}
export function addCustomCommand(action: CustomCommand) {
const commands = global.__VUE_DEVTOOLS_KIT_CUSTOM_COMMANDS__
if (commands.some((t: CustomCommand) => t.id === action.id))
return
commands.push({
...action,
icon: resolveIcon(action.icon),
children: action.children
? action.children.map((child: CustomCommand) => ({
...child,
icon: resolveIcon(child.icon),
}))
: undefined,
})
updateAllStates()
}
export function removeCustomCommand(actionId: string) {
const commands = global.__VUE_DEVTOOLS_KIT_CUSTOM_COMMANDS__
const index = commands.findIndex(t => t.id === actionId)
if (index === -1)
return
commands.splice(index, 1)
updateAllStates()
}
export function toggleClientConnected(state: boolean) {
updateDevToolsState({ clientConnected: state })
}