Always clean up event listener on binding disconnect by dfritsch · Pull Request #845 · hotwired/stimulus

@dfritsch

Updates the binding observer and dispatch logic to always clean up the
event listeners.

Without this change, removing an element _inside_ a controller can cause
the bindings on that element to be removed but the event listeners are
left dangling. Even if the controller is eventually also removed, these
events will never be cleaned up, so these elements will remain in memory
indefinitely.

Based on the tests, the one downside of this is that if you adjust an
event that was set to once and move it to a different order on the element,
that action now has a different index so will disconnect the old binding
and add a new binding. Previously this would find the old event listener
that wasn't properly cleaned up and re-use it, keeping the "once" logic.
Actions which are added on top of the existing action would keep the
same event, so this still partly works.

`clearEventListeners` would be passed as `true` in all cases with this
update, so the parameter is removed.