Skip to content

Conversation

@vyagh
Copy link
Contributor

@vyagh vyagh commented Jan 9, 2026

Replace addEventListener with onclick assignment to prevent duplicate event listeners from accumulating each time the play button is clicked.

Problem

The stop button listener was added inside the play button's onclick handler. Since the play button can be clicked repeatedly, addEventListener keeps adding new listeners without removing previous ones, causing:

  • Memory leak from accumulating listeners
  • Multiple handlers executing on single stop click
  • Performance degradation over time

Solution

Use .onclick assignment which automatically replaces the previous handler instead of accumulating listeners.

Changes

  • js/toolbar.js: Changed addEventListener to .onclick
  • js/__tests__/toolbar.test.js: Updated test to match new implementation

Testing

Replace addEventListener with onclick assignment for stop button handler
to prevent duplicate event listeners from accumulating each time the play
button is clicked.

The previous implementation added a new click listener to the stop button
every time the play button was clicked, causing:
- Memory leak from accumulating listeners
- Multiple handlers firing on single click
- Performance degradation over time

This fix follows the pattern used elsewhere in toolbar.js (e.g., renderStopIcon)
and matches recent memory leak fixes in widgets (sugarlabs#5030, sugarlabs#5029, sugarlabs#4950, sugarlabs#4913).

Changed Files:
- js/toolbar.js: Replace addEventListener with onclick
- js/__tests__/toolbar.test.js: Update test to match new implementation
Copilot AI review requested due to automatic review settings January 9, 2026 12:11
@github-actions
Copy link
Contributor

github-actions bot commented Jan 9, 2026

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a memory leak in the toolbar play button by replacing addEventListener with .onclick assignment. The memory leak occurred because the stop button's event listener was being added inside the play button's click handler, causing duplicate listeners to accumulate with each play button click.

  • Changed stopIcon.addEventListener("click", ...) to stopIcon.onclick = ... in the play button handler
  • Updated test assertions to verify onclick assignment instead of addEventListener calls
  • Added explanatory comment in tests about the memory leak prevention

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
js/toolbar.js Replaced addEventListener with .onclick assignment to prevent accumulating event listeners on the stop button
js/tests/toolbar.test.js Updated test to verify onclick assignment and added comment explaining the memory leak fix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vyagh
Copy link
Contributor Author

vyagh commented Jan 9, 2026

forgot to turn off copilot, appologies

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.

Memory leak in play button from accumulating event listeners

1 participant