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

Clear dangling EventListeners and Detached Nodes when a controller is removed from the DOM #592

Merged
merged 1 commit into from Oct 31, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/core/binding_observer.ts
Expand Up @@ -7,7 +7,7 @@ import { Token, ValueListObserver, ValueListObserverDelegate } from "../mutation

export interface BindingObserverDelegate extends ErrorHandler {
bindingConnected(binding: Binding): void
bindingDisconnected(binding: Binding): void
bindingDisconnected(binding: Binding, clearEventListeners?: boolean): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a scenario where we want to invoke this bindingDisconnected method without clearing the listeners?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I added this conditional after I've seen this test to fail: When an attribute is replaced with a new one bindingDisconnected is triggered (via tokensUnmatched) but if we clear the listener, in this case, we lose the options initially set ("once" in this case) so the test will fail.

}

export class BindingObserver implements ValueListObserverDelegate<Action> {
Expand Down Expand Up @@ -72,7 +72,7 @@ export class BindingObserver implements ValueListObserverDelegate<Action> {
}

private disconnectAllActions() {
this.bindings.forEach((binding) => this.delegate.bindingDisconnected(binding))
this.bindings.forEach((binding) => this.delegate.bindingDisconnected(binding, true))
this.bindingsByAction.clear()
}

Expand Down
20 changes: 19 additions & 1 deletion src/core/dispatcher.ts
Expand Up @@ -41,8 +41,9 @@ export class Dispatcher implements BindingObserverDelegate {
this.fetchEventListenerForBinding(binding).bindingConnected(binding)
}

bindingDisconnected(binding: Binding) {
bindingDisconnected(binding: Binding, clearEventListeners: boolean = false) {
this.fetchEventListenerForBinding(binding).bindingDisconnected(binding)
if (clearEventListeners) this.clearEventListenersForBinding(binding)
}

// Error handling
Expand All @@ -51,6 +52,23 @@ export class Dispatcher implements BindingObserverDelegate {
this.application.handleError(error, `Error ${message}`, detail)
}

private clearEventListenersForBinding(binding: Binding) {
const eventListener = this.fetchEventListenerForBinding(binding)
if (!eventListener.hasBindings()) {
eventListener.disconnect()
this.removeMappedEventListenerFor(binding)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe could extract a couple of methods to clarify logic. I also added a method EventListener.hasBindings:

private clearEventListenersForBinding(binding: Binding) {
  const eventListener = this.fetchEventListenerForBinding(binding)
  if (!eventListener.hasBindings()) {
    this.removeMappedEventListenerFor(binding)
    eventListener.disconnect()
  }
}

private removeMappedEventListenerFor(binding: Binding) {
  const { eventTarget, eventName, eventOptions } = binding
  const eventListenerMap = this.fetchEventListenerMapForEventTarget(eventTarget)
  const cacheKey = this.cacheKey(eventName, eventOptions)

  eventListenerMap.delete(cacheKey)
  if (eventListenerMap.size == 0) this.eventListenerMaps.delete(eventTarget)
}


private removeMappedEventListenerFor(binding: Binding) {
const { eventTarget, eventName, eventOptions } = binding
const eventListenerMap = this.fetchEventListenerMapForEventTarget(eventTarget)
const cacheKey = this.cacheKey(eventName, eventOptions)

eventListenerMap.delete(cacheKey)
if (eventListenerMap.size == 0) this.eventListenerMaps.delete(eventTarget)
}

private fetchEventListenerForBinding(binding: Binding): EventListener {
const { eventTarget, eventName, eventOptions } = binding
return this.fetchEventListener(eventTarget, eventName, eventOptions)
Expand Down
4 changes: 4 additions & 0 deletions src/core/event_listener.ts
Expand Up @@ -43,6 +43,10 @@ export class EventListener implements EventListenerObject {
}
}

hasBindings() {
return this.unorderedBindings.size > 0
}

get bindings(): Binding[] {
return Array.from(this.unorderedBindings).sort((left, right) => {
const leftIndex = left.index,
Expand Down
22 changes: 22 additions & 0 deletions src/tests/modules/core/memory_tests.ts
@@ -0,0 +1,22 @@
import { ControllerTestCase } from "../../cases/controller_test_case"

export default class MemoryTests extends ControllerTestCase() {
controllerElement!: Element

async setup() {
this.controllerElement = this.controller.element
}

fixtureHTML = `
<div data-controller="${this.identifier}">
<button data-action="${this.identifier}#doLog">Log</button>
<button data-action="${this.identifier}#doAlert">Alert</button>
</div>
`

async "test removing a controller clears dangling eventListeners"() {
this.assert.equal(this.application.dispatcher.eventListeners.length, 2)
await this.fixtureElement.removeChild(this.controllerElement)
this.assert.equal(this.application.dispatcher.eventListeners.length, 0)
}
}