From f7afaac0ef10d228d6ecf5ed5e47b4ea7a9bab0e Mon Sep 17 00:00:00 2001 From: Xie Yuheng Date: Sun, 30 Jul 2023 18:52:08 +0800 Subject: [PATCH] `connect` should not reconnect --- src/lang/graph/cleanUpWires.ts | 13 ++++++++----- src/lang/graph/connect.ts | 8 ++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/lang/graph/cleanUpWires.ts b/src/lang/graph/cleanUpWires.ts index 3fde1175..3ed3b820 100644 --- a/src/lang/graph/cleanUpWires.ts +++ b/src/lang/graph/cleanUpWires.ts @@ -1,18 +1,21 @@ import { Net } from "./Net" import { connect } from "./connect" -import { removeEdge } from "./removeEdge" +import { disconnect } from "./disconnect" import { removeNode } from "./removeNode" export function cleanUpWires(net: Net): void { for (const wire of net.wires) { if (wire.start.connection && wire.end.connection) { - removeEdge(net, wire.start.connection.edge) - removeEdge(net, wire.end.connection.edge) - removeNode(net, wire.start.node) removeNode(net, wire.end.node) - connect(net, wire.start.connection.port, wire.end.connection.port) + const start = wire.start.connection.port + const end = wire.end.connection.port + + disconnect(net, wire.start.connection.edge) + disconnect(net, wire.end.connection.edge) + + connect(net, start, end) } } diff --git a/src/lang/graph/connect.ts b/src/lang/graph/connect.ts index be99e64c..e60124d4 100644 --- a/src/lang/graph/connect.ts +++ b/src/lang/graph/connect.ts @@ -4,6 +4,14 @@ import { lookupRuleByPorts } from "../mod/lookupRuleByPorts" export function connect(net: Net, start: Port, end: Port): void { const rule = lookupRuleByPorts(net.mod, start, end) + if (start.connection !== undefined) { + throw new Error(`[connect] The start port is already connected`) + } + + if (end.connection !== undefined) { + throw new Error(`[connect] The end port is already connected`) + } + if (rule !== undefined) { const edge = { start, end, rule } start.connection = { edge, port: end }