Skip to content

Commit

Permalink
extract findCurrentPortOrFail from compose
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 2, 2023
1 parent 6753aea commit 28ccf4b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 56 deletions.
96 changes: 44 additions & 52 deletions src/lang/compose/compose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node } from "../graph"
import { Node, Port, PortConnection } from "../graph"
import { findPortInNodes } from "../graph/findPortInActiveEdge"
import { Mod } from "../mod"
import { lookupDefinitionOrFail } from "../mod/lookupDefinitionOrFail"
Expand Down Expand Up @@ -43,71 +43,63 @@ export function compose(
}

case "PortPush": {
const { current } = options || {}
const currentPort = findCurrentPortOrFail(
word.nodeName,
word.portName,
options,
)

if (current === undefined) {
throw new Error(`[PortPush.compose] expect current activeEdge`)
}

const found = findPortInNodes(word.nodeName, word.portName, [
current.start,
current.end,
])

if (found === undefined) {
throw new Error(
`[PortPush.compose] can not find port: ${word.portName} in active edge`,
)
}

if (found.connection === undefined) {
throw new Error(
`[PortPush.compose] I expect the found port to have connection`,
)
}

disconnect(net, found.connection.edge)

net.ports.push(found)
disconnect(net, currentPort.connection.edge)

net.ports.push(currentPort)
return
}

case "PortReconnect": {
const { current } = options || {}
const currentPort = findCurrentPortOrFail(
word.nodeName,
word.portName,
options,
)

if (current === undefined) {
throw new Error(`[PortReconnect.compose] expect current activeEdge`)
}

const found = findPortInNodes(word.nodeName, word.portName, [
current.start,
current.end,
])

if (found === undefined) {
throw new Error(
`[PortReconnect.compose] can not find port: ${word.portName} in active edge`,
)
}

if (found.connection === undefined) {
throw new Error(
`[PortReconnect.compose] I expect the found port to have connection`,
)
}

disconnect(net, found.connection.edge)
disconnect(net, currentPort.connection.edge)

const topPort = net.ports.pop()

if (topPort === undefined) {
throw new Error(`[PortReconnect.compose] expect top port`)
}

connect(net, topPort, found)

connect(net, topPort, currentPort)
return
}
}
}

function findCurrentPortOrFail(
nodeName: string,
portName: string,
options?: ComposeOptions,
): Port & { connection: PortConnection } {
const who = "findCurrentPortOrFail"

const { current } = options || {}

if (current === undefined) {
throw new Error(`[${who}] I expect current start and end nodes`)
}

const found = findPortInNodes(nodeName, portName, [
current.start,
current.end,
])

if (found === undefined) {
throw new Error(`[${who}] I can not find port: ${portName} in nodes`)
}

if (found.connection === undefined) {
throw new Error(`[${who}] I expect the found port to have connection`)
}

return found as Port & { connection: PortConnection }
}
10 changes: 6 additions & 4 deletions src/lang/graph/Port.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Edge, Node } from "../graph"
import { Type } from "../type"

export type PortConnection = {
edge: Edge
port: Port
}

export type Port = {
node: Node
name: string
t: Type
isPrincipal: boolean
connection?: {
edge: Edge
port: Port
}
connection?: PortConnection
}

0 comments on commit 28ccf4b

Please sign in to comment.