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

fix: cleanup event listeners on unmount #1793

Merged
merged 1 commit into from Oct 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/vueWrapper.ts
Expand Up @@ -35,6 +35,7 @@ export class VueWrapper<
private rootVM: ComponentPublicInstance | undefined | null
private __app: App | null
private __setProps: ((props: Record<string, unknown>) => void) | undefined
private cleanUpCallbacks: Array<() => void> = []

constructor(
app: App | null,
Expand Down Expand Up @@ -149,8 +150,12 @@ export class VueWrapper<
// @see https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md#fallthrough-control
if (emits.includes(eventName)) continue

element.addEventListener(eventName, (...args) => {
const eventListener: EventListener = (...args) => {
recordEvent(vm.$, eventName, args)
}
element.addEventListener(eventName, eventListener)
this.cleanUpCallbacks.push(() => {
element.removeEventListener(eventName, eventListener)
})
}
}
Expand Down Expand Up @@ -214,6 +219,9 @@ export class VueWrapper<
// Clear emitted events cache for this component instance
removeEventHistory(this.vm)

this.cleanUpCallbacks.forEach((cb) => cb())
this.cleanUpCallbacks = []

this.__app.unmount()
}
}
Expand Down