diff --git a/packages/core/graph/src/Graph.js b/packages/core/graph/src/Graph.js index ecdd48dc55a..8fb1a3f4024 100644 --- a/packages/core/graph/src/Graph.js +++ b/packages/core/graph/src/Graph.js @@ -142,7 +142,7 @@ export default class Graph { } for (let {type, from} of this.adjacencyList.getInboundEdgesByType(nodeId)) { - this.removeEdge( + this._removeEdge( from, nodeId, type, @@ -153,7 +153,7 @@ export default class Graph { } for (let {type, to} of this.adjacencyList.getOutboundEdgesByType(nodeId)) { - this.removeEdge(nodeId, to, type); + this._removeEdge(nodeId, to, type); } let wasRemoved = this.nodes.delete(nodeId); @@ -166,16 +166,31 @@ export default class Graph { } for (let to of this.getNodeIdsConnectedFrom(nodeId, type)) { - this.removeEdge(nodeId, to, type); + this._removeEdge(nodeId, to, type); } } - // Removes edge and node the edge is to if the node is orphaned removeEdge( from: NodeId, to: NodeId, type: TEdgeType | NullEdgeType = 1, removeOrphans: boolean = true, + ) { + if (!this.adjacencyList.hasEdge(from, to, type)) { + throw new Error( + `Edge from ${fromNodeId(from)} to ${fromNodeId(to)} not found!`, + ); + } + + this._removeEdge(from, to, type, removeOrphans); + } + + // Removes edge and node the edge is to if the node is orphaned + _removeEdge( + from: NodeId, + to: NodeId, + type: TEdgeType | NullEdgeType = 1, + removeOrphans: boolean = true, ) { if (!this.adjacencyList.hasEdge(from, to, type)) { return; @@ -249,7 +264,7 @@ export default class Graph { } for (let child of childrenToRemove) { - this.removeEdge(fromNodeId, child, type); + this._removeEdge(fromNodeId, child, type); } } diff --git a/packages/core/graph/test/Graph.test.js b/packages/core/graph/test/Graph.test.js index c709fb2474a..6cf42d30ff1 100644 --- a/packages/core/graph/test/Graph.test.js +++ b/packages/core/graph/test/Graph.test.js @@ -89,6 +89,16 @@ describe('Graph', () => { assert(!graph.isOrphanedNode(nodeC)); }); + it("removeEdge should throw if the edge doesn't exist", () => { + let graph = new Graph(); + let nodeA = graph.addNode('a'); + let nodeB = graph.addNode('b'); + + assert.throws(() => { + graph.removeEdge(nodeA, nodeB); + }, /Edge from 0 to 1 not found!/); + }); + it('removeEdge should prune the graph at that edge', () => { // a // / \