Fix touch listeners stacking on canvas when horizontal scrolling is toggled - #7994
Open
Abhishek-Sonje wants to merge 3 commits into
Open
Fix touch listeners stacking on canvas when horizontal scrolling is toggled#7994Abhishek-Sonje wants to merge 3 commits into
Abhishek-Sonje wants to merge 3 commits into
Conversation
…up event handlers
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a bug in Activity._setupBlocksContainerEvents() where re-running the setup (triggered by toggling horizontal scrolling) could stack multiple canvas touch listeners, causing duplicated gesture handling and a memory leak.
Changes:
- Convert
touchstart,touchmove, andtouchendcanvas handlers to stored instance references so prior handlers can be removed before re-adding. - Route touch handlers through
Activity’s managedaddEventListener/removeEventListenerso they’re tracked and cleaned up viacleanupEventListeners(). - Keep the existing
wheelhandler’s remove-before-add pattern intact while aligning touch handlers with it.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
_setupBlocksContainerEvents()injs/activity.jsattachestouchstart,touchmove,touchend, andwheellisteners tomyCanvas. This function reruns every time the user toggles "Enable/Disable horizontal scrolling" in the Advanced mode toolbar (viasetScroller, seetoolbar-ui.js:1373-1378), not just once at startup.The
wheellistener already guards against this, it stores a handler reference and removes the old one before adding a new one on each rerun. Thetouchstart,touchmove, andtouchendlisteners didn't have that guard. They were attached with rawmyCanvas.addEventListener(...)calls with no stored reference and no removal, so every toggle added a fresh set of touch handlers on top of the old ones. The old ones never got removed and kept firing alongside the new ones, each one holding the wholeActivityinstance alive through its closure.This PR applies the same store-reference-and-remove-before-add pattern the
wheelhandler already uses to the three touch handlers, and routes all of them through the class's managedthis.addEventListener/this.removeEventListenerso they also get swept up automatically bycleanupEventListeners().Related Issue
This PR fixes #7993
PR Category
Changes Made
touchstarthandler in a named function (__touchStartHandler), stored onthis._touchStartHandler, with a remove-before-add guard, matching the existingwheelhandler pattern.touchmove(__touchMoveHandler/this._touchMoveHandler) andtouchend(__touchEndHandler/this._touchEndHandler).myCanvas.addEventListener(...)to the class's managedthis.addEventListener(...), so they're tracked inthis._listenersand cleaned up bycleanupEventListeners()like the rest of the managed listeners.Testing Performed
console.count("_setupBlocksContainerEvents")at the top of the function and confirmed it climbs on each horizontal-scroll toggle, both before and after this fix (confirming the function itself is still expected to rerun, that part isn't the bug).getEventListeners(document.getElementById("myCanvas"))in Chrome DevTools after several toggles and confirmedtouchstart/touchmove/touchendentries grew by one per toggle whilewheelstayed at one.touchstart,touchmove,touchend,wheel) stay at exactly one listener entry regardless of how many times horizontal scrolling is toggled.node --check js/activity.jspasses.Checklist
npm run lintandnpx prettier --check .with no errors.Additional Notes for Reviewers
The fix mirrors the exact pattern already present in this same function for the
wheellistener (see the lines right below the touch handlers), so it's intentionally not introducing a new convention, just applying the existing one consistently to the three handlers that were missing it. Happy to add a regression test around_setupBlocksContainerEventsif there's an existing pattern in the test suite for asserting listener counts on canvas elements, pointers welcome.