Skip to content

Commit

Permalink
perf(custom-element): cancel MutationObserver listener when disconn…
Browse files Browse the repository at this point in the history
…ected (vuejs#8666)
  • Loading branch information
Alfred-Skyblue authored Jul 12, 2023
1 parent 70c3ac7 commit 24d98f0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {},
Expand Down Expand Up @@ -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!)
Expand All @@ -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
Expand Down

0 comments on commit 24d98f0

Please sign in to comment.