]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(devtools): avoid memory leak caused by devtools event buffer
authorEvan You <yyx990803@gmail.com>
Wed, 28 Sep 2022 10:19:19 +0000 (18:19 +0800)
committerEvan You <yyx990803@gmail.com>
Wed, 28 Sep 2022 10:19:19 +0000 (18:19 +0800)
fix #6591

packages/runtime-core/src/devtools.ts

index 36c5763dcf14966d2db5ed9c8a0dbfaa3b2163af..83d7483df39560faf1351e17bd55b2117085e83a 100644 (file)
@@ -28,6 +28,7 @@ interface DevtoolsHook {
   once: (event: string, handler: Function) => void
   off: (event: string, handler: Function) => void
   appRecords: AppRecord[]
+  _buffer: any[][]
 }
 
 export let devtools: DevtoolsHook
@@ -101,8 +102,26 @@ export const devtoolsComponentAdded = /*#__PURE__*/ createDevtoolsComponentHook(
 export const devtoolsComponentUpdated =
   /*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_UPDATED)
 
-export const devtoolsComponentRemoved =
-  /*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_REMOVED)
+const _devtoolsComponentRemoved = /*#__PURE__*/ createDevtoolsComponentHook(
+  DevtoolsHooks.COMPONENT_REMOVED
+)
+
+export const devtoolsComponentRemoved = (
+  component: ComponentInternalInstance
+) => {
+  if (devtools && devtools._buffer.length) {
+    let wasBuffered = false
+    devtools._buffer = devtools._buffer.filter(item => {
+      if (item.some(arg => arg === component)) {
+        wasBuffered = true
+        return false
+      }
+      return true
+    })
+    if (wasBuffered) return
+  }
+  _devtoolsComponentRemoved(component)
+}
 
 function createDevtoolsComponentHook(hook: DevtoolsHooks) {
   return (component: ComponentInternalInstance) => {