Files
Ivan Starkov 22633eb7c2 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
2024-12-10 09:37:33 +03:00

37 lines
1.0 KiB
TypeScript

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];
};