Skip to content

Commit 1ae7a28

Browse files
authored
perf: reduce initial performance overhead (#595)
1 parent 9e223c8 commit 1ae7a28

File tree

9 files changed

+58
-11
lines changed

9 files changed

+58
-11
lines changed

packages/chrome-extension/src/devtools-background.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ function createPanelOnVueDetected() {
2323
'icons/128.png',
2424
'pages/devtools-panel.html',
2525
(panel) => {
26-
panel.onShown.addListener((window) => {
27-
26+
panel.onShown.addListener(() => {
27+
chrome.devtools.inspectedWindow.eval(`window.__VUE_DEVTOOLS_UPDATE_CLIENT_DETECTED__({ chrome: true })`)
28+
})
29+
panel.onHidden.addListener(() => {
30+
chrome.devtools.inspectedWindow.eval(`window.__VUE_DEVTOOLS_UPDATE_CLIENT_DETECTED__({ chrome: false })`)
2831
})
2932
},
3033
)

packages/client/src/App.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ onUnmounted(() => {
118118
rpc.value.toggleClientConnected(false)
119119
rpc.functions.off(DevToolsMessagingEvents.ACTIVE_APP_UNMOUNTED, onActiveAppUnmounted)
120120
})
121+
122+
function toggleDevToolsClientVisible(params: { visible: boolean, host: string }) {
123+
const { host, visible } = params
124+
rpc.value.updateDevToolsClientDetected({
125+
[host]: visible,
126+
})
127+
}
121128
</script>
122129

123130
<template>
@@ -129,7 +136,7 @@ onUnmounted(() => {
129136
:class="isUtilityView ? 'flex' : sidebarExpanded ? 'grid grid-cols-[250px_1fr]' : 'grid grid-cols-[50px_1fr]'"
130137
h-full h-screen of-hidden font-sans bg-base
131138
>
132-
<SideNav v-if="!isUtilityView" of-x-hidden of-y-auto />
139+
<SideNav v-if="!isUtilityView" of-x-hidden of-y-auto @toggle-devtools-client-visible="toggleDevToolsClientVisible" />
133140
<Splitpanes
134141
h-full of-hidden
135142
@resize="splitScreenSize = $event.map((v) => v.size)"

packages/client/src/components/common/SideNav.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<script setup lang="ts">
22
import { VueDropdown } from '@vue/devtools-ui'
33
4+
const emit = defineEmits(['toggleDevtoolsClientVisible'])
45
const showDocking = ref(false)
56
const showMoreTabs = ref(false)
67
const panel = ref()
78
const buttonDocking = ref<HTMLButtonElement>()
89
const buttonMoreTabs = ref<HTMLButtonElement>()
910
const sidebarExpanded = computed(() => devtoolsClientState.value.expandSidebar)
1011
const sidebarScrollable = computed(() => devtoolsClientState.value.scrollableSidebar)
11-
1212
const { enabledTabs, flattenedTabs } = useAllTabs()
1313
1414
const ITEM_HEIGHT = 45
@@ -50,6 +50,17 @@ useResizeObserver(containerRef, () => {
5050
// This is a hack to force the dropdown to reposition itself
5151
dropdownDistance.value = dropdownDistance.value === 6 ? 6.01 : 6
5252
})
53+
54+
const hostEnv = useHostEnv()
55+
useIntersectionObserver(
56+
containerRef,
57+
([{ isIntersecting }]) => {
58+
emit('toggleDevtoolsClientVisible', {
59+
visible: isIntersecting,
60+
host: hostEnv,
61+
})
62+
},
63+
)
5364
</script>
5465

5566
<template>

packages/core/src/rpc/global.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DevToolsContextHookKeys, DevToolsMessagingHookKeys, devtools, devtoolsRouter, devtoolsRouterInfo, getActiveInspectors, getInspector, getInspectorActions, getInspectorInfo, getInspectorNodeActions, getRpcClient, getRpcServer, stringify, toggleClientConnected } from '@vue/devtools-kit'
1+
import { DevToolsContextHookKeys, DevToolsMessagingHookKeys, devtools, devtoolsRouter, devtoolsRouterInfo, getActiveInspectors, getInspector, getInspectorActions, getInspectorInfo, getInspectorNodeActions, getRpcClient, getRpcServer, stringify, toggleClientConnected, updateDevToolsClientDetected } from '@vue/devtools-kit'
22
import { createHooks } from 'hookable'
33
import type { DevToolsV6PluginAPIHookKeys, DevToolsV6PluginAPIHookPayloads, OpenInEditorOptions } from '@vue/devtools-kit'
44

@@ -148,6 +148,9 @@ export const functions = {
148148
unhighlight() {
149149
devtools.ctx.hooks.callHook(DevToolsContextHookKeys.COMPONENT_UNHIGHLIGHT)
150150
},
151+
updateDevToolsClientDetected(params: Record<string, boolean>) {
152+
updateDevToolsClientDetected(params)
153+
},
151154
// listen to devtools server events
152155
initDevToolsServerListener() {
153156
const rpcServer = getRpcServer<RPCFunctions>()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { target } from '@vue/devtools-shared'
2+
import { devtoolsState } from '../../ctx/state'
3+
import { toggleHighPerfMode } from '../high-perf-mode'
4+
5+
export function updateDevToolsClientDetected(params: Record<string, boolean>) {
6+
devtoolsState.devtoolsClientDetected = {
7+
...devtoolsState.devtoolsClientDetected,
8+
...params,
9+
}
10+
const devtoolsClientVisible = Object.values(devtoolsState.devtoolsClientDetected).some(Boolean)
11+
toggleHighPerfMode(!devtoolsClientVisible)
12+
}
13+
14+
target.__VUE_DEVTOOLS_UPDATE_CLIENT_DETECTED__ ??= updateDevToolsClientDetected

packages/devtools-kit/src/ctx/state.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export interface DevToolsState {
1515
tabs: CustomTab[]
1616
commands: CustomCommand[]
1717
highPerfModeEnabled: boolean
18+
devtoolsClientDetected: {
19+
[key: string]: boolean
20+
}
1821
}
1922

2023
global.__VUE_DEVTOOLS_KIT_APP_RECORDS__ ??= []
@@ -33,7 +36,8 @@ function initStateFactory() {
3336
activeAppRecordId: '',
3437
tabs: [],
3538
commands: [],
36-
highPerfModeEnabled: false,
39+
highPerfModeEnabled: true,
40+
devtoolsClientDetected: {},
3741
}
3842
}
3943
global[STATE_KEY] ??= initStateFactory()

packages/devtools-kit/src/hook/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { target } from '@vue/devtools-shared'
22
import type { HookKeys, Hookable } from 'hookable'
33
import { createHooks } from 'hookable'
44
import { DevToolsEvent, DevToolsHook, DevToolsHooks, VueHooks } from '../types'
5+
import { devtoolsState } from '../ctx'
56

67
export { VueHooks } from '../types'
78

@@ -87,7 +88,7 @@ export function subscribeDevToolsHook() {
8788

8889
// component added hook
8990
hook.on<DevToolsEvent[DevToolsHooks.COMPONENT_ADDED]>(DevToolsHooks.COMPONENT_ADDED, async (app, uid, parentUid, component) => {
90-
if (app?._instance?.type?.devtools?.hide)
91+
if (app?._instance?.type?.devtools?.hide || devtoolsState.highPerfModeEnabled)
9192
return
9293

9394
if (!app || (typeof uid !== 'number' && !uid) || !component)
@@ -98,15 +99,15 @@ export function subscribeDevToolsHook() {
9899

99100
// component updated hook
100101
hook.on<DevToolsEvent[DevToolsHooks.COMPONENT_UPDATED]>(DevToolsHooks.COMPONENT_UPDATED, (app, uid, parentUid, component) => {
101-
if (!app || (typeof uid !== 'number' && !uid) || !component)
102+
if (!app || (typeof uid !== 'number' && !uid) || !component || devtoolsState.highPerfModeEnabled)
102103
return
103104

104105
devtoolsHooks.callHook(DevToolsHooks.COMPONENT_UPDATED, app, uid, parentUid, component)
105106
})
106107

107108
// component removed hook
108109
hook.on<DevToolsEvent[DevToolsHooks.COMPONENT_REMOVED]>(DevToolsHooks.COMPONENT_REMOVED, async (app, uid, parentUid, component) => {
109-
if (!app || (typeof uid !== 'number' && !uid) || !component)
110+
if (!app || (typeof uid !== 'number' && !uid) || !component || devtoolsState.highPerfModeEnabled)
110111
return
111112

112113
devtoolsHooks.callHook(DevToolsHooks.COMPONENT_REMOVED, app, uid, parentUid, component)

packages/devtools-kit/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export { parse, stringify } from './shared'
1919
export { formatInspectorStateValue, getInspectorStateValueType, getRaw, toEdit, toSubmit } from './core/component/state/format'
2020
export { UNDEFINED, INFINITY, NAN, NEGATIVE_INFINITY } from './core/component/state/constants'
2121
export { isPlainObject } from './core/component/state/is'
22+
export { updateDevToolsClientDetected } from './core/devtools-client/detected'
2223

2324
export const devtools = {
2425
hook,

packages/firefox-extension/src/devtools-background.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ function createPanelOnVueDetected() {
2323
'icons/128.png',
2424
'devtools-panel.html',
2525
(panel) => {
26-
panel.onShown.addListener((window) => {
27-
26+
panel.onShown.addListener(() => {
27+
chrome.devtools.inspectedWindow.eval(`window.__VUE_DEVTOOLS_UPDATE_CLIENT_DETECTED__({ chrome: true })`)
28+
})
29+
panel.onHidden.addListener(() => {
30+
chrome.devtools.inspectedWindow.eval(`window.__VUE_DEVTOOLS_UPDATE_CLIENT_DETECTED__({ chrome: false })`)
2831
})
2932
},
3033
)

0 commit comments

Comments
 (0)