fix: avoid double initialization of node context menu items#143
Merged
Conversation
`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
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a runtime structuredClone error when creating nodes with contextMenuItems by ensuring node context menu items are initialized exactly once (in GraphViewNode.addContextMenu), restoring Storybook and consumer functionality.
Changes:
- Stop initializing/cloning
nodeSchema.contextMenuItemsinsideGraph.createNode. - Pass raw schema
contextMenuItemsdirectly toview.addNodeContextMenu, avoiding double-initialization and cloning functions.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Fixes the runtime error thrown when adding a node to a
Graph:This breaks every story in the published Storybook (and any consumer that builds nodes with
contextMenuItemsin the schema, e.g. the React example).Root cause
Graph.createNodewas initializing context menu items and then handing them toview.addNodeContextMenu, which eventually callsGraphViewNode.addContextMenu— which initializes them again:onSelectcallbacks.structuredCloneon items that now containonSelectfunction references → throws.The regression was exposed by #136, which replaced the custom
deepCopyFunctionwithstructuredClone. The previous custom deep-copy iterated withfor...inand silently preserved function references, masking this latent double-initialization bug.The other call site (
GraphViewNode.updateNodeType) already passes raw schema items directly, confirmingaddContextMenuis the correct (single) place to initialize.Fix
In
Graph.createNode, passnodeSchema.contextMenuItemsdirectly toview.addNodeContextMenu. Initialization now happens exactly once insideGraphViewNode.addContextMenu.No public API changes.
Fixes #141
Test plan
npm run lintpassesnpm run storybookopens without thestructuredCloneerrorDirected GraphandVisual Programming Graphstories → context menu appears, items (e.g. "Delete Node", "Add ... Edge") are functional