Skip to content

Commit 24d98f0

Browse files
perf(custom-element): cancel MutationObserver listener when disconnected (#8666)
1 parent 70c3ac7 commit 24d98f0

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

packages/runtime-dom/src/apiCustomElement.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export class VueElement extends BaseClass {
178178
private _resolved = false
179179
private _numberProps: Record<string, true> | null = null
180180
private _styles?: HTMLStyleElement[]
181-
181+
private _ob?: MutationObserver | null = null
182182
constructor(
183183
private _def: InnerComponentDef,
184184
private _props: Record<string, any> = {},
@@ -215,6 +215,10 @@ export class VueElement extends BaseClass {
215215

216216
disconnectedCallback() {
217217
this._connected = false
218+
if (this._ob) {
219+
this._ob.disconnect()
220+
this._ob = null
221+
}
218222
nextTick(() => {
219223
if (!this._connected) {
220224
render(null, this.shadowRoot!)
@@ -235,11 +239,13 @@ export class VueElement extends BaseClass {
235239
}
236240

237241
// watch future attr changes
238-
new MutationObserver(mutations => {
242+
this._ob = new MutationObserver(mutations => {
239243
for (const m of mutations) {
240244
this._setAttr(m.attributeName!)
241245
}
242-
}).observe(this, { attributes: true })
246+
})
247+
248+
this._ob.observe(this, { attributes: true })
243249

244250
const resolve = (def: InnerComponentDef, isAsync = false) => {
245251
const { props, styles } = def

0 commit comments

Comments
 (0)