]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf(custom-element): cancel `MutationObserver` listener when disconnected (#8666)
author丶远方 <yangpanteng@gmail.com>
Wed, 12 Jul 2023 03:13:20 +0000 (11:13 +0800)
committerGitHub <noreply@github.com>
Wed, 12 Jul 2023 03:13:20 +0000 (11:13 +0800)
packages/runtime-dom/src/apiCustomElement.ts

index 2add422586d771647ec0d5531edbbe4d9d93b571..5662b0b535b5cfc170cc2d80e060144f59071da0 100644 (file)
@@ -178,7 +178,7 @@ export class VueElement extends BaseClass {
   private _resolved = false
   private _numberProps: Record<string, true> | null = null
   private _styles?: HTMLStyleElement[]
-
+  private _ob?: MutationObserver | null = null
   constructor(
     private _def: InnerComponentDef,
     private _props: Record<string, any> = {},
@@ -215,6 +215,10 @@ export class VueElement extends BaseClass {
 
   disconnectedCallback() {
     this._connected = false
+    if (this._ob) {
+      this._ob.disconnect()
+      this._ob = null
+    }
     nextTick(() => {
       if (!this._connected) {
         render(null, this.shadowRoot!)
@@ -235,11 +239,13 @@ export class VueElement extends BaseClass {
     }
 
     // watch future attr changes
-    new MutationObserver(mutations => {
+    this._ob = new MutationObserver(mutations => {
       for (const m of mutations) {
         this._setAttr(m.attributeName!)
       }
-    }).observe(this, { attributes: true })
+    })
+
+    this._ob.observe(this, { attributes: true })
 
     const resolve = (def: InnerComponentDef, isAsync = false) => {
       const { props, styles } = def