Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cancel MutationObserver listener when disconnected #8666

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,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 @@ -213,6 +213,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 @@ -233,11 +237,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