Skip to content

Commit

Permalink
fix: cleanup event listeners on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
freakzlike authored and cexbrayat committed Oct 4, 2022
1 parent 5c1dceb commit 149e11f
Showing 1 changed file with 9 additions and 1 deletion.
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

0 comments on commit 149e11f

Please sign in to comment.