|
1 |
| -import type { EdgeTypes } from '../edges'; |
| 1 | +import type { ReactFlowInstance } from '@xyflow/react'; |
| 2 | +import type { DiagramEditorEdge, EdgeTypes } from '../edges'; |
2 | 3 | import type { DiagramEditorNode, NodeTypes } from '../nodes';
|
| 4 | +import { exhaustiveCheck } from './exhaustive-check'; |
3 | 5 |
|
4 | 6 | const ALLOWED_OUTPUT_EDGES: Record<NodeTypes, Set<EdgeTypes>> = {
|
5 | 7 | buffer: new Set<EdgeTypes>(['bufferKey', 'bufferSeq']),
|
@@ -67,3 +69,147 @@ export function getValidEdgeTypes(
|
67 | 69 | : new Set([]);
|
68 | 70 | return Array.from(setIntersection(allowedOutputEdges, allowedInputEdges));
|
69 | 71 | }
|
| 72 | + |
| 73 | +enum CardinalityType { |
| 74 | + Single, |
| 75 | + Pair, |
| 76 | + Many, |
| 77 | +} |
| 78 | + |
| 79 | +function getOutputCardinality(type: NodeTypes): CardinalityType { |
| 80 | + switch (type) { |
| 81 | + case 'fork_clone': |
| 82 | + case 'unzip': |
| 83 | + case 'buffer': |
| 84 | + case 'section': |
| 85 | + case 'split': { |
| 86 | + return CardinalityType.Many; |
| 87 | + } |
| 88 | + case 'fork_result': { |
| 89 | + return CardinalityType.Pair; |
| 90 | + } |
| 91 | + case 'node': |
| 92 | + case 'buffer_access': |
| 93 | + case 'join': |
| 94 | + case 'serialized_join': |
| 95 | + case 'listen': |
| 96 | + case 'scope': |
| 97 | + case 'stream_out': |
| 98 | + case 'transform': |
| 99 | + case 'start': |
| 100 | + case 'terminate': |
| 101 | + case 'sectionBuffer': |
| 102 | + case 'sectionInput': |
| 103 | + case 'sectionOutput': { |
| 104 | + return CardinalityType.Single; |
| 105 | + } |
| 106 | + default: { |
| 107 | + exhaustiveCheck(type); |
| 108 | + throw new Error('unknown op type'); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export type EdgeValidationResult = |
| 114 | + | { valid: true; validEdgeTypes: EdgeTypes[] } |
| 115 | + | { valid: false; error: string }; |
| 116 | + |
| 117 | +function createValidationError(error: string): EdgeValidationResult { |
| 118 | + return { valid: false, error }; |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Perform a quick check if an edge is valid. |
| 123 | + * This only checks if the edge type is valid, does not check for conflicting edges, data correctness etc. |
| 124 | + * Complexity is O(1). |
| 125 | + */ |
| 126 | +export function checkValidEdgeQuick( |
| 127 | + edge: DiagramEditorEdge, |
| 128 | + reactFlow: ReactFlowInstance<DiagramEditorNode, DiagramEditorEdge>, |
| 129 | +): EdgeValidationResult { |
| 130 | + const sourceNode = reactFlow.getNode(edge.source); |
| 131 | + const targetNode = reactFlow.getNode(edge.target); |
| 132 | + |
| 133 | + if (!sourceNode || !targetNode) { |
| 134 | + return createValidationError('cannot find source or target node'); |
| 135 | + } |
| 136 | + |
| 137 | + const validEdgeTypes = getValidEdgeTypes(sourceNode, targetNode); |
| 138 | + if (!validEdgeTypes.includes(edge.type)) { |
| 139 | + return createValidationError('invalid edge type'); |
| 140 | + } |
| 141 | + |
| 142 | + return { valid: true, validEdgeTypes }; |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Perform a simple check of the validity of edges. |
| 147 | + * A more complete check than `checkValidEdgeQuick`, but still does not do complicated checks like type compatibility. |
| 148 | + * Complexity is O(numOfEdges), so it is not recommended to call this very frequently. |
| 149 | + */ |
| 150 | +export function checkValidEdgeSimple( |
| 151 | + edge: DiagramEditorEdge, |
| 152 | + reactFlow: ReactFlowInstance<DiagramEditorNode, DiagramEditorEdge>, |
| 153 | +): EdgeValidationResult { |
| 154 | + const quickCheck = checkValidEdgeQuick(edge, reactFlow); |
| 155 | + if (!quickCheck.valid) { |
| 156 | + return quickCheck; |
| 157 | + } |
| 158 | + |
| 159 | + const sourceNode = reactFlow.getNode(edge.source); |
| 160 | + if (!sourceNode) { |
| 161 | + return createValidationError('cannot find source or target node'); |
| 162 | + } |
| 163 | + |
| 164 | + // Check if the source supports emitting multiple outputs. |
| 165 | + // NOTE: All nodes supports "Many" inputs so we don't need to check that. |
| 166 | + const outputCardinality = getOutputCardinality(sourceNode.type); |
| 167 | + switch (outputCardinality) { |
| 168 | + case CardinalityType.Single: { |
| 169 | + if (reactFlow.getEdges().some((edge) => edge.source === sourceNode.id)) { |
| 170 | + return createValidationError('source node already has an edge'); |
| 171 | + } |
| 172 | + break; |
| 173 | + } |
| 174 | + case CardinalityType.Pair: { |
| 175 | + let count = 0; |
| 176 | + for (const edge of reactFlow.getEdges()) { |
| 177 | + if (edge.source === sourceNode.id) { |
| 178 | + count++; |
| 179 | + } |
| 180 | + if (count > 1) { |
| 181 | + return createValidationError('source node already has two edges'); |
| 182 | + } |
| 183 | + } |
| 184 | + break; |
| 185 | + } |
| 186 | + case CardinalityType.Many: { |
| 187 | + break; |
| 188 | + } |
| 189 | + default: { |
| 190 | + exhaustiveCheck(outputCardinality); |
| 191 | + throw new Error('unknown output cardinality'); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return { valid: true, validEdgeTypes: quickCheck.validEdgeTypes }; |
| 196 | +} |
| 197 | + |
| 198 | +/** |
| 199 | + * Perform a full check of the validity of edges. |
| 200 | + * This can be slow so it is not recommended to call this frequently. |
| 201 | + */ |
| 202 | +export async function checkValidEdgeFull( |
| 203 | + edge: DiagramEditorEdge, |
| 204 | + reactFlow: ReactFlowInstance<DiagramEditorNode, DiagramEditorEdge>, |
| 205 | +): Promise<EdgeValidationResult> { |
| 206 | + const simpleCheck = checkValidEdgeSimple(edge, reactFlow); |
| 207 | + if (!simpleCheck.valid) { |
| 208 | + return simpleCheck; |
| 209 | + } |
| 210 | + |
| 211 | + // TODO: check message type compatibility. Writing the same logic as `bevy_impulse` is hard, it might |
| 212 | + // be better to introduce a validation endpoint. |
| 213 | + |
| 214 | + return { valid: true, validEdgeTypes: simpleCheck.validEdgeTypes }; |
| 215 | +} |
0 commit comments