-
-
Notifications
You must be signed in to change notification settings - Fork 875
fix: improve deep cloning for graph and resampling to handle non-cloneable properties #1365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,48 @@ | |||||||||
| import type { River } from "./river-generator"; | ||||||||||
| import type { Point } from "./voronoi"; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Deeply clones an object, omitting properties that cannot be cloned with structuredClone. | ||||||||||
| * D3 quadtrees store accessor functions that fail structuredClone, so they're excluded. | ||||||||||
| */ | ||||||||||
| const safeDeepClone = <T>(obj: T): T => { | ||||||||||
| if (obj === null || typeof obj !== "object") { | ||||||||||
| return obj; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Handle typed arrays - slice() creates a copy with a new underlying buffer | ||||||||||
| if (ArrayBuffer.isView(obj) && !(obj instanceof DataView)) { | ||||||||||
| return (obj as any).slice() as T; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Handle arrays | ||||||||||
| if (Array.isArray(obj)) { | ||||||||||
| return obj.map((item) => safeDeepClone(item)) as T; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Handle objects - skip quadtree (has functions) and other non-clonable properties | ||||||||||
| const cloned: Record<string, unknown> = {}; | ||||||||||
| for (const key in obj) { | ||||||||||
| if (!Object.prototype.hasOwnProperty.call(obj, key)) continue; | ||||||||||
|
|
||||||||||
| const value = (obj as Record<string, unknown>)[key]; | ||||||||||
|
|
||||||||||
| // Skip quadtree properties (D3 quadtrees have _x and _y functions) | ||||||||||
| if (value && typeof value === "object" && "_x" in (value as object)) { | ||||||||||
|
Comment on lines
+42
to
+43
|
||||||||||
| // Skip quadtree properties (D3 quadtrees have _x and _y functions) | |
| if (value && typeof value === "object" && "_x" in (value as object)) { | |
| // Skip known quadtree properties (D3 quadtrees have _x and _y functions) | |
| if (key === "q" && value && typeof value === "object" && "_x" in (value as object)) { |
Copilot
AI
Mar 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
safeDeepClone creates plain objects via {} and recursively copies enumerable own props, which does not preserve prototypes (e.g., Quadtree instances, classes) and may change behavior if any cloned values rely on their prototype methods. If the intent is only to omit pack.cells.q, consider explicitly stripping that field (or using structuredClone with a targeted fallback) so other object types keep their identity/shape as much as possible.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ export interface PackedGraph { | |
| p: [number, number][]; // cell polygon points | ||
| b: boolean[]; // cell is on border | ||
| h: TypedArray; // cell heights | ||
| q: Quadtree<[number, number, number]>; // cell quadtree index | ||
| q?: Quadtree<[number, number, number]>; // cell quadtree index (optional, not cloned) | ||
|
||
| /** Terrain type */ | ||
| t: TypedArray; // cell terrain types | ||
| r: TypedArray; // river id passing through cell | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using JSON.parse(JSON.stringify(currentGraph)) as a deep clone is fragile and can be very expensive for large graphs. It also drops/rewrites non-JSON-safe values (typed arrays, undefined, Infinity/NaN, etc.), and will still throw if any circular references exist. Consider cloning only the specific fields needed for preview generation (e.g., points/boundary/dimensions), or using a dedicated clone that explicitly omits the non-cloneable quadtree rather than serializing the entire graph.