Skip to content

Fix canplay listener and interval accumulation in doUseCamera()#5837

Merged
walterbender merged 2 commits intosugarlabs:masterfrom
severe77:fix/camera-canplay-interval-accumulation
Mar 8, 2026
Merged

Fix canplay listener and interval accumulation in doUseCamera()#5837
walterbender merged 2 commits intosugarlabs:masterfrom
severe77:fix/camera-canplay-interval-accumulation

Conversation

@severe77
Copy link
Contributor

Summary

This PR fixes accumulation of "canplay" event listeners and setInterval
loops in doUseCamera() inside js/utils/utils.js.

Problem

Each call to doUseCamera() attached a new anonymous "canplay" listener
and started a new interval when isVideo was true.

Since the listener reference was not preserved, previously attached
listeners could not be removed. As a result:

  • Multiple "canplay" handlers accumulated
  • Multiple setInterval(draw, 100) loops could run simultaneously
  • Repeated camera activations caused unnecessary memory and CPU usage

Solution

This change:

  • Stores the "canplay" handler in CameraManager.canPlayHandler
  • Removes any existing handler before attaching a new one
  • Tracks the active interval in CameraManager.intervalId
  • Clears any existing interval before starting a new one

The original logic and behavior are preserved. No refactoring or structural changes were introduced.

Notes

This PR focuses only on preventing listener and interval accumulation.

@github-actions
Copy link
Contributor

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

Copy link
Contributor

@vanshika2720 vanshika2720 left a comment

Choose a reason for hiding this comment

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

@severe77 Properly stores and removes the canplay listener and clears existing intervals, preventing accumulation without changing original behavior.

optional suggestion (not blocking): after clearInterval, you could set CameraManager.intervalId = null to make the state explicit.

@github-actions
Copy link
Contributor

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

@severe77
Copy link
Contributor Author

good eye 👁️
thanks for the suggestion, @vanshika2720

@severe77
Copy link
Contributor Author

@walterbender the PR is ready for review. Please have a look when you get a chance.

@walterbender walterbender merged commit 12b7612 into sugarlabs:master Mar 8, 2026
7 checks passed
parthdagia05 pushed a commit to parthdagia05/musicblocks that referenced this pull request Mar 15, 2026
…rlabs#5837)

* Fix canplay listener and interval accumulation in doUseCamera()

* Reset CameraManager.intervalId to null after clearInterval
parthdagia05 pushed a commit to parthdagia05/musicblocks that referenced this pull request Mar 15, 2026
…rlabs#5837)

* Fix canplay listener and interval accumulation in doUseCamera()

* Reset CameraManager.intervalId to null after clearInterval
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