fix: debounce clock timer and lite mode updates to prevent simulator …#848
fix: debounce clock timer and lite mode updates to prevent simulator …#848kunal-10-cloud wants to merge 4 commits into
Conversation
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
WalkthroughThis change adds a debounce utility and applies it in the simulator: a new Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/simulator/src/simulationArea.ts`:
- Around line 61-73: The changeClockTime implementation uses `this` and
`this._debouncedChangeClockTime`, which breaks when `changeClockTime` is
imported/destructured and called unbound; update the function body to reference
the module-level `simulationArea` object directly (use
`simulationArea._debouncedChangeClockTime`, `simulationArea.timePeriod`, and
`simulationArea.ClockInterval`) and remove `this` uses so the logic that
clears/sets `simulationArea.ClockInterval` and calls the debounced function
works whether called as a method or as the exported/destructured
`changeClockTime`.
|
Hey @kunal-10-cloud there are lot of merge conflict can you resolve it |

Fixes #847
Describe the changes you have made in this PR -
This PR fixes simulator crashes caused by rapid clock timer adjustments and Lite Mode toggles.
Screenshots of the UI changes (If any) -
Code Understanding and AI Usage
Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?
If you used AI assistance:
Explain your implementation approach:
--What problem does your code solve?
The code addresses a critical stability issue where the simulator would crash or freeze during rapid user interactions. Specifically, adjusting the clock time or toggling "Lite Mode" triggered immediate, heavy, synchronous operations. When these events fired in quick succession, they created a cascading execution pile-up that overwhelmed the browser's main thread.
--What alternative approaches did you consider?
I considered using a simple throttle instead of a debounce, but throttling would still allow multiple expensive resetup calls to execute while the user is actively clicking, which is unnecessary. I also considered adding a global "loading" overlay to block UI during updates, but that would significantly degrade the user experience and feel sluggish.
--Why did you choose this specific implementation?
I chose a combination of debouncing and re-entrancy guards.
Debouncing is the most efficient way to handle UI inputs because it waits for the user to "settle" on a value before executing the heavy lifting.
The re-entrancy guard in setup.js provides a second layer of defense. It ensures that if a resize or setup event is somehow triggered while another is already running, it exits immediately rather than creating a recursive loop. This "double-locking" approach makes the simulator virtually immune to this specific crash vector.
--What are the key functions/components and what do they do?
I implemented a reusable utility in debounce.ts to rate-limit expensive updates, which is now used by changeClockTime in simulationArea.ts(300ms) and changeLightMode in engine.js(150ms) to ensure interval resets and mode toggles occur only after user interaction settles. Additionally, I added an isResettingUp state guard in setup.js to block re-entrant calls to the resetup function, while debounce.test.ts provides a dedicated test suite to verify the utility's timing and reliability.
Checklist before requesting a review
Note: Please check Allow edits from maintainers if you would like us to assist in the PR.
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Chores
✏️ Tip: You can customize this high-level summary in your review settings.