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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.