Skip to content

Fix: prevent resize listener accumulation in makeBackground#5824

Merged
walterbender merged 1 commit intosugarlabs:masterfrom
severe77:fix/resize-listener-accumulation
Mar 14, 2026
Merged

Fix: prevent resize listener accumulation in makeBackground#5824
walterbender merged 1 commit intosugarlabs:masterfrom
severe77:fix/resize-listener-accumulation

Conversation

@severe77
Copy link
Contributor

Summary

This PR fixes a resize listener accumulation issue in js/turtles.js
inside the Turtles.TurtlesView class.

Problem

In the makeBackground() method, a new window.resize event listener
was being registered each time the method was called.

Since the resize handler reference was not preserved across calls,
previously registered listeners were not removed before adding new ones,
leading to multiple active resize listeners.

Because makeBackground() may run multiple times during normal
canvas updates, this could result in multiple resize listeners
being attached.

Solution

  • Defined a named resizeHandler function inside makeBackground().

  • Stored the handler reference on the instance (this._resizeHandler).

  • Before registering a new listener, the existing one (if present)
    is removed using:

    window.removeEventListener("resize", this._resizeHandler);

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

Copy link
Contributor

@kartikktripathi kartikktripathi left a comment

Choose a reason for hiding this comment

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

This looks good to me. The resize listener accumulation issue is addressed properly by storing the handler reference and ensuring any existing listener is removed before registering a new one.
The fix is clean, scoped, and improves lifecycle stability without altering existing behaviour.

@severe77
Copy link
Contributor Author

@walterbender could you please take a look at this pr when you have some time?

@walterbender walterbender merged commit 1adcd93 into sugarlabs:master Mar 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants