Skip to content

Commit d1fd189

Browse files
committed
fix: avoid reruns from array identity in autoPaint worker effect
Use memoized stable payloads derived from content keys so reference-only changes to filaments/filtered do not retrigger worker computation. Keeps existing 250ms debounce behavior.
1 parent a87b00a commit d1fd189

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

src/hooks/useAutoPaintWorker.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ export function useAutoPaintWorker(
7474
return filtered.map((s) => `${s.hex}:${(s.count as number | undefined) ?? 0}`).join(';');
7575
}, [filtered]);
7676

77+
// Keep stable references when only array identity changes but content does not.
78+
const stableFilaments = useMemo(() => filaments, [filamentsKey]);
79+
80+
const stableImageSwatches = useMemo(
81+
() =>
82+
filtered.map((s) => ({
83+
hex: s.hex,
84+
count: s.count as number | undefined,
85+
})),
86+
[filteredKey]
87+
);
88+
7789
// Create the worker lazily on first need.
7890
const getWorker = useCallback(() => {
7991
if (!workerRef.current) {
@@ -137,11 +149,8 @@ export function useAutoPaintWorker(
137149

138150
const request: AutoPaintWorkerRequest = {
139151
id,
140-
filaments,
141-
imageSwatches: filtered.map((s) => ({
142-
hex: s.hex,
143-
count: s.count as number | undefined,
144-
})),
152+
filaments: stableFilaments,
153+
imageSwatches: stableImageSwatches,
145154
layerHeight,
146155
firstLayerHeight: slicerFirstLayerHeight,
147156
maxHeight: autoPaintMaxHeight,
@@ -179,8 +188,8 @@ export function useAutoPaintWorker(
179188
regionWeightingMode,
180189
imageDimensions,
181190
getWorker,
182-
filaments, // Still need the actual arrays for the worker
183-
filtered,
191+
stableFilaments,
192+
stableImageSwatches,
184193
]);
185194

186195
return { autoPaintResult, isComputing };

0 commit comments

Comments
 (0)