Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/core/binding_observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Token, ValueListObserver, ValueListObserverDelegate } from "../mutation

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

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, true))
this.bindings.forEach((binding) => this.delegate.bindingDisconnected(binding))
Copy link
Author

Choose a reason for hiding this comment

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

The actual "fix" is adding true to line 70 above. However, this makes all cases true, so instead the option is removed entirely.

this.bindingsByAction.clear()
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export class Dispatcher implements BindingObserverDelegate {
this.fetchEventListenerForBinding(binding).bindingConnected(binding)
}

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

// Error handling
Expand Down
25 changes: 23 additions & 2 deletions src/tests/modules/core/event_options_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,36 @@ export default class EventOptionsTests extends LogControllerTestCase {
this.assertActions({ name: "log", eventType: "keydown" })
}

async "test edge case when updating action list with setAttribute preserves once history"() {
async "test edge case when updating action list with setAttribute loses once history if events are prepended to it"() {
await this.setAction(this.buttonElement, "c#log:once")

await this.triggerEvent(this.buttonElement, "click")
await this.triggerEvent(this.buttonElement, "click")

//modify with a setAttribute and c#log should not be called anyhow
//modify with a setAttribute and c#log should be called once again
await this.setAction(this.buttonElement, "c#log2 c#log:once d#log")
await this.triggerEvent(this.buttonElement, "click")
await this.triggerEvent(this.buttonElement, "click")

this.assertActions(
{ name: "log", identifier: "c" },
{ name: "log2", identifier: "c" },
{ name: "log", identifier: "d" },
{ name: "log", identifier: "c" },
{ name: "log2", identifier: "c" },
{ name: "log", identifier: "d" }
)
}

async "test edge case when updating action list with setAttribute keeps once history if action keeps the same index"() {
await this.setAction(this.buttonElement, "c#log:once")

await this.triggerEvent(this.buttonElement, "click")
await this.triggerEvent(this.buttonElement, "click")

//modify with a setAttribute and c#log should be called once again
await this.setAction(this.buttonElement, "c#log:once c#log2 d#log")
await this.triggerEvent(this.buttonElement, "click")

this.assertActions(
{ name: "log", identifier: "c" },
Expand Down
22 changes: 20 additions & 2 deletions src/tests/modules/core/memory_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default class MemoryTests extends ControllerTestCase() {

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

Expand All @@ -19,4 +19,22 @@ export default class MemoryTests extends ControllerTestCase() {
await this.fixtureElement.removeChild(this.controllerElement)
this.assert.equal(this.application.dispatcher.eventListeners.length, 0)
}

async "test removing a button clears dangling eventListeners"() {
this.assert.equal(this.application.dispatcher.eventListeners.length, 2)

const button = this.findElement("#button1")
await this.remove(button)

this.assert.equal(this.application.dispatcher.eventListeners.length, 1)
}

async "test removing a button clears dangling eventListeners with multiple actions"() {
this.assert.equal(this.application.dispatcher.eventListeners.length, 2)

const button = this.findElement("#button2")
await this.remove(button)

this.assert.equal(this.application.dispatcher.eventListeners.length, 1)
}
}