-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Reduce amount of draw on resize (#4544)
## Description 1. What is this PR about (link the issue and add a short description) ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
- Loading branch information
Showing
2 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { shallowEqual } from "shallow-equal"; | ||
import warnOnce from "warn-once"; | ||
|
||
const trackers: Record<string, (args: Record<string, unknown>) => void> = {}; | ||
|
||
/** | ||
* Debug-only: Tracks changes between consecutive calls. | ||
* Usage: `track('label')({ a, b, c })` logs changed keys on subsequent calls. | ||
*/ | ||
export const trackChanges = (label: string) => { | ||
if (process.env.NODE_ENV !== "development") { | ||
warnOnce(true, "track should not be used in production"); | ||
return () => {}; | ||
} | ||
|
||
if (!trackers[label]) { | ||
let previousArgs: Record<string, unknown> | undefined; | ||
|
||
trackers[label] = (currentArgs: Record<string, unknown>) => { | ||
if (!previousArgs) { | ||
previousArgs = currentArgs; | ||
return; | ||
} | ||
|
||
if (!shallowEqual(previousArgs, currentArgs)) { | ||
const changedKeys = Object.keys(currentArgs).filter( | ||
(key) => previousArgs![key] !== currentArgs[key] | ||
); | ||
console.info(label, changedKeys); | ||
previousArgs = currentArgs; | ||
} | ||
}; | ||
} | ||
|
||
return trackers[label]; | ||
}; |