Skip to content

Commit 22633eb

Browse files
authored
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
1 parent cd4041e commit 22633eb

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

apps/builder/app/canvas/instance-selected.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from "~/canvas/collapsed";
3131
import { getBrowserStyle } from "./features/webstudio-component/get-browser-style";
3232
import type { InstanceSelector } from "~/shared/tree-utils";
33+
import { shallowEqual } from "shallow-equal";
3334

3435
const isHtmlTag = (tag: string): tag is HtmlTags =>
3536
htmlTags.includes(tag as HtmlTags);
@@ -205,7 +206,14 @@ const subscribeSelectedInstance = (
205206
selectedInstanceSelector
206207
);
207208

208-
$selectedInstanceIntanceToTag.set(instanceToTag);
209+
if (
210+
!shallowEqual(
211+
[...($selectedInstanceIntanceToTag.get()?.entries() ?? [])].flat(),
212+
[...(instanceToTag?.entries() ?? [])].flat()
213+
)
214+
) {
215+
$selectedInstanceIntanceToTag.set(instanceToTag);
216+
}
209217

210218
const unitSizes = calculateUnitSizes(element);
211219
$selectedInstanceUnitSizes.set(unitSizes);
@@ -229,7 +237,12 @@ const subscribeSelectedInstance = (
229237
activeStates.add(state);
230238
}
231239
}
232-
$selectedInstanceStates.set(activeStates);
240+
241+
if (
242+
!shallowEqual(activeStates.keys(), $selectedInstanceStates.get().keys())
243+
) {
244+
$selectedInstanceStates.set(activeStates);
245+
}
233246
};
234247

235248
let updateStoreTimeouHandle: undefined | ReturnType<typeof setTimeout>;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { shallowEqual } from "shallow-equal";
2+
import warnOnce from "warn-once";
3+
4+
const trackers: Record<string, (args: Record<string, unknown>) => void> = {};
5+
6+
/**
7+
* Debug-only: Tracks changes between consecutive calls.
8+
* Usage: `track('label')({ a, b, c })` logs changed keys on subsequent calls.
9+
*/
10+
export const trackChanges = (label: string) => {
11+
if (process.env.NODE_ENV !== "development") {
12+
warnOnce(true, "track should not be used in production");
13+
return () => {};
14+
}
15+
16+
if (!trackers[label]) {
17+
let previousArgs: Record<string, unknown> | undefined;
18+
19+
trackers[label] = (currentArgs: Record<string, unknown>) => {
20+
if (!previousArgs) {
21+
previousArgs = currentArgs;
22+
return;
23+
}
24+
25+
if (!shallowEqual(previousArgs, currentArgs)) {
26+
const changedKeys = Object.keys(currentArgs).filter(
27+
(key) => previousArgs![key] !== currentArgs[key]
28+
);
29+
console.info(label, changedKeys);
30+
previousArgs = currentArgs;
31+
}
32+
};
33+
}
34+
35+
return trackers[label];
36+
};

0 commit comments

Comments
 (0)