refactor: replace deepCopyFunction with structuredClone#136
Merged
willeastcott merged 2 commits intomainfrom Mar 9, 2026
Merged
refactor: replace deepCopyFunction with structuredClone#136willeastcott merged 2 commits intomainfrom
willeastcott merged 2 commits intomainfrom
Conversation
49e2e68 to
82a60b8
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors context-menu item cloning by replacing the custom deepCopyFunction utility with the platform structuredClone, and removes the now-unused utility file.
Changes:
- Replaced
deepCopyFunction(...)withstructuredClone(...)for edge/node context menu item cloning. - Removed
src/util.tsand the corresponding README entry.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/util.ts |
Removes the custom deep copy helper that was previously used for menu item cloning. |
src/index.ts |
Switches edge/node context menu item cloning to structuredClone and removes the old import. |
README.md |
Removes util.ts from the source layout list. |
Comments suppressed due to low confidence (2)
src/index.ts:323
- Using
structuredClonehere can throw at runtime whenedgeSchema.contextMenuItemscontains non-cloneable values (notably callback functions likeonSelect, which PCUI Menu items commonly use). The previousdeepCopyFunctionpreserved function references, so this is a breaking behavior change. Consider switching to a cloning approach that preserves functions (e.g., shallow-clone items/arrays) or explicitly constrain/validatecontextMenuItemsto structured-cloneable data before cloning (and document the restriction).
const contextMenuItems = structuredClone(edgeSchema.contextMenuItems).map((item: any) => {
if (item.action === GRAPH_ACTIONS.DELETE_EDGE) {
item.onSelect = () => {
this._dispatchEvent(GRAPH_ACTIONS.DELETE_EDGE, { edgeId: edgeId, edge: this._graphData.get(`data.edges.${edgeId}`) });
};
src/index.ts:433
_initializeNodeContextMenuItemsaddsonSelectfunctions to each item, and it’s invoked both fromGraph.createNodeand again insideGraphViewNode.addContextMenu. WithstructuredClone, the second invocation will attempt to clone items that now include functions and will throw aDataCloneError, breaking node context menus. Fix by ensuring menu items are only initialized/cloned once (single call site), or by replacingstructuredClonewith a clone that tolerates function properties for these menu item objects.
const contextMenuItems = structuredClone(items).map((item: any) => {
if (item.action === GRAPH_ACTIONS.ADD_EDGE) {
item.onSelect = () => this._createUnconnectedEdgeForNode(node, item.edgeType);
}
if (item.action === GRAPH_ACTIONS.DELETE_NODE) {
item.onSelect = () => {
this._dispatchEvent(GRAPH_ACTIONS.DELETE_NODE, this._deleteNode(node.id));
};
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
structuredClone is supported natively in all modern browsers and Node 17+. This removes the custom deep-copy utility and its source file. Made-with: Cursor
cde6fcb to
5d7958c
Compare
4 tasks
willeastcott
added a commit
that referenced
this pull request
Apr 29, 2026
`Graph.createNode` was calling `_initializeNodeContextMenuItems` and then passing the result to `view.addNodeContextMenu`, which (via `GraphViewNode.addContextMenu`) calls `_initializeNodeContextMenuItems` again. The second invocation runs `structuredClone` on items that now contain `onSelect` function references attached during the first invocation, causing: Failed to execute 'structuredClone' on 'Window': () => this._createUnconnectedEdgeForNode(node, item.edgeType) could not be cloned. This regression was exposed by #136 (replacing `deepCopyFunction` with `structuredClone`); the previous custom deep-copy silently preserved function references on objects, masking the latent bug. Pass the raw schema items straight to `view.addNodeContextMenu` so initialization happens exactly once inside `GraphViewNode.addContextMenu`, matching the existing flow used by `updateNodeType`. Fixed #141 Made-with: Cursor
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
deepCopyFunctioninutil.tswith the built-instructuredClonesrc/util.ts(it only containeddeepCopyFunction)structuredCloneis supported in all modern browsers and Node 17+.Test plan
npm run buildcompletes successfullydeepCopyFunction)