Skip to content

Commit 24f4c47

Browse files
committed
fix(devtools): avoid memory leak caused by devtools event buffer
fix #6591
1 parent 551f606 commit 24f4c47

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

packages/runtime-core/src/devtools.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface DevtoolsHook {
2828
once: (event: string, handler: Function) => void
2929
off: (event: string, handler: Function) => void
3030
appRecords: AppRecord[]
31+
_buffer: any[][]
3132
}
3233

3334
export let devtools: DevtoolsHook
@@ -101,8 +102,26 @@ export const devtoolsComponentAdded = /*#__PURE__*/ createDevtoolsComponentHook(
101102
export const devtoolsComponentUpdated =
102103
/*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_UPDATED)
103104

104-
export const devtoolsComponentRemoved =
105-
/*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_REMOVED)
105+
const _devtoolsComponentRemoved = /*#__PURE__*/ createDevtoolsComponentHook(
106+
DevtoolsHooks.COMPONENT_REMOVED
107+
)
108+
109+
export const devtoolsComponentRemoved = (
110+
component: ComponentInternalInstance
111+
) => {
112+
if (devtools && devtools._buffer.length) {
113+
let wasBuffered = false
114+
devtools._buffer = devtools._buffer.filter(item => {
115+
if (item.some(arg => arg === component)) {
116+
wasBuffered = true
117+
return false
118+
}
119+
return true
120+
})
121+
if (wasBuffered) return
122+
}
123+
_devtoolsComponentRemoved(component)
124+
}
106125

107126
function createDevtoolsComponentHook(hook: DevtoolsHooks) {
108127
return (component: ComponentInternalInstance) => {

0 commit comments

Comments
 (0)