(fix) O3-5555: Improve performance and readability of useLayoutType hook#1702
(fix) O3-5555: Improve performance and readability of useLayoutType hook#1702AshThe25 wants to merge 1 commit intoopenmrs:mainfrom
Conversation
|
|
In the course of doing this, I also realized that the resize handler may cause excessive updates even when the layout hasn’t changed. This ensures that the states are updated only when there is an actual layout change, thus reducing the number of re-renders. Also, the inclusion of the debounce prevents excessive updates during rapid resizes, thus improving performance. |
|
This is a nice improvement @AshThe25, reducing unnecessary re-renders and de-bouncing resize events should significantly improve performance I think it might also be helpful to add a test to verify layout state updates, to detect only when the layout actually changes and not on every resize event |
Hey thanks a lot this actually means a lot for lets connect sometime on linkdin new here and if made any mistake do lemme know will surely work on it and thanks for that feedback really means a lot |
|
Hey @AshThe25, just a heads up, PR #1707 by @dulajweligaththa08 addresses the same useLayoutType hook with very similar changes, including the debounce and redundant state update prevention. It would be worth coordinating with that PR to avoid duplicated effort or a merge conflict. |
ibacher
left a comment
There was a problem hiding this comment.
Thanks for this submission. In the absence of data supporting that this fixes a genuine problem (e.g., a test or something), this seems a little excessive. I also think the analysis here is misguided in that the attempted de-duplication really isn't effective. Please try to produce a test case that will show the value of this addition and remove the redundant redundancy check.
| setType((prev) => { | ||
| if (prev === newLayout) { | ||
| return prev; | ||
| } | ||
| return newLayout; | ||
| }); |
There was a problem hiding this comment.
While I get the idea of trying to deduplicate things, it's important to track what this value is. Fundamentally, it's a string and since 'large-desktop' === 'large-desktop', this kind of guard is purely redundant. Rather than improving things, this makes the code more complicated for no actual gain.
| clearTimeout(resizeTimeout.current); | ||
| } | ||
|
|
||
| resizeTimeout.current = setTimeout(updateLayout, 100); |
|
Hello @ibacher @Nandalily! I’m sorry for not getting back sooner; had an accident recently and had to take some time off. But I’m back now! You guys are completely correct about everything. I made some changes in the code in the most recent commit: Got rid of the prev === newLayout check. I didn’t need it because of React’s useState; when you use primitives for state, it doesn’t update anything when the value hasn’t changed. Got rid. |
|
@ibacher, I wanted to confirm that the last commit resolved your concerns. |
733e08b to
0427121
Compare
|
Hi @ibacher, addressed your feedback:
All 13 tests in |
|
@ibacher all your feedback from the Apr 1 review has been addressed in the latest commit:
SonarQube Quality Gate passed (0 new issues). Could you take another look when you get a chance? Thanks! |
…undant updates Browser fires resize up to ~60 times/s during a drag. Without debouncing, getLayout() re-reads the DOM on every event. Debouncing at 150ms collapses rapid bursts to a single read after the resize settles. - Add RESIZE_DEBOUNCE_MS = 150 constant - Debounce the resize handler using useRef to hold the timeout - Cancel pending timeout on unmount to prevent stale state updates - Restore accidentally removed /** @module @category UI */ JSDoc - Add tests with vi.useFakeTimers() covering debounce delay, burst collapsing, and cleanup on unmount
0427121 to
e4c0cff
Compare
|
|
@ibacher added a comment above |



Requirements
Summary
This PR improves the performance of the useLayoutType hook by preventing unnecessary state updates during window resize events.
Changes
Screenshots
N/A
Related Issue
https://issues.openmrs.org/browse/O3-5555
Note
This is a small improvement to prevent silent null returns and improve API safety. No existing JIRA issue was found for this case.
Other
N/A