{"record":{"id":"4c3ae1a81fff3103","repo":"BabylonJS/Babylon.js","slug":"element-element-already-added-to-the-graph","errorCode":null,"errorMessage":"Element \"${element}\" already added to the graph!","messagePattern":"Element \"(.+?)\" already added to the graph!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/smartFilters/src/optimization/dependencyGraph.ts","lineNumber":26,"sourceCode":"    private _dependOn: Map<T, Set<T>>;\r\n    private _requiredBy: Map<T, Set<T>>;\r\n\r\n    /**\r\n     * Creates a new instance of a dependency graph.\r\n     */\r\n    constructor() {\r\n        this._list = new Set();\r\n        this._dependOn = new Map();\r\n        this._requiredBy = new Map();\r\n    }\r\n\r\n    /**\r\n     * Adds an element to the graph.\r\n     * @param element - The element to add to the graph.\r\n     */\r\n    public addElement(element: T) {\r\n        if (this._list.has(element)) {\r\n            throw new Error(`Element \"${element}\" already added to the graph!`);\r\n        }\r\n\r\n        this._list.add(element);\r\n    }\r\n\r\n    /**\r\n     * Adds a dependency between two elements.\r\n     * @param element - The element that depends on another element.\r\n     * @param dependency - The element that is required by the element passed as the first parameter.\r\n     */\r\n    public addDependency(element: T, dependency: T) {\r\n        if (!this._dependOn.has(element)) {\r\n            this._dependOn.set(element, new Set());\r\n        }\r\n\r\n        if (!this._requiredBy.has(dependency)) {\r\n            this._requiredBy.set(dependency, new Set());\r\n        }\r","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/smartFilters/src/optimization/dependencyGraph.ts#L8-L44","documentation":"DependencyGraph.addElement stores elements in a Set and refuses to add the same element twice, throwing this error on duplicates. The graph assumes each element is registered exactly once before dependencies between elements are declared.","triggerScenarios":"Calling addElement twice with the same (equal-by-reference or equal-value) element — e.g. registering each graph node in a loop that re-visits a node, or calling addDependency setup twice on the same node set.","commonSituations":"Iterating a graph that contains shared/reused nodes; adding a block to the optimization dependency graph both directly and through a group; accidental double-invocation of a graph-construction function.","solutions":["Guard adds with a check (or wrap in a helper that skips already-added elements).","Deduplicate your node list before building the graph.","Make graph construction idempotent by tracking which elements you've already added."],"exampleFix":"// before\nfor (const node of allNodesIncludingShared) graph.addElement(node);\n// after\nfor (const node of new Set(allNodesIncludingShared)) graph.addElement(node);","handlingStrategy":"validation","validationCode":"if (!addedElements.has(node)) {\n  addedElements.add(node);\n  graph.addElement(node);\n}","typeGuard":"null","tryCatchPattern":"try {\n  graph.addElement(el);\n} catch (e) {\n  if (!e.message.includes(\"already added to the graph\")) throw e;\n  // duplicate is expected; skip\n}","preventionTips":["Deduplicate node collections with new Set(nodes) before building the graph.","Make graph construction functions idempotent.","Remember Set membership is by reference for objects — reuse the same element instance."],"tags":["dependency-graph","duplicate-element","internal-state"],"backgroundTag":"duplicate-node-registration","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}