Skip to content

Commit bc73def

Browse files
committed
refactor
Signed-off-by: Teo Koon Peng <[email protected]>
1 parent d31a114 commit bc73def

14 files changed

+70
-50
lines changed

diagram-editor/frontend/edges/buffer-edge.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
import { type EdgeProps, StepEdge } from '@xyflow/react';
22
import { memo } from 'react';
33
import type { Edge } from '../types/react-flow';
4-
import type { SectionBufferSlotData } from './section-edge';
5-
6-
export type BufferKeySlotData = {
7-
type: 'bufferKey';
8-
key: string;
9-
};
10-
11-
export type BufferSeqSlotData = {
12-
type: 'bufferSeq';
13-
seq: number;
14-
};
4+
import type {
5+
BufferKeyInputSlotData,
6+
BufferSeqInputSlotData,
7+
SectionBufferInputSlotData,
8+
} from './input-slots';
159

1610
export type BufferOutputData = Record<string, never>;
1711

1812
export type BufferEdge = Edge<
1913
BufferOutputData,
20-
BufferKeySlotData | BufferSeqSlotData | SectionBufferSlotData,
14+
BufferKeyInputSlotData | BufferSeqInputSlotData | SectionBufferInputSlotData,
2115
'buffer'
2216
>;
2317

diagram-editor/frontend/edges/create-edge.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { MarkerType } from '@xyflow/react';
22
import { v4 as uuidv4 } from 'uuid';
33
import type { DiagramEditorEdge, EdgeOutputData } from '.';
4-
import type { BufferKeySlotData, BufferSeqSlotData } from './buffer-edge';
54
import type { DefaultEdge } from './default-edge';
6-
import type { SectionBufferSlotData } from './section-edge';
5+
import type {
6+
BufferKeyInputSlotData,
7+
BufferSeqInputSlotData,
8+
SectionBufferInputSlotData,
9+
} from './input-slots';
710

811
export function createBaseEdge(
912
source: string,
@@ -109,7 +112,10 @@ export function createSplitRemainingEdge(
109112
export function createBufferEdge(
110113
source: string,
111114
target: string,
112-
data: BufferKeySlotData | BufferSeqSlotData | SectionBufferSlotData,
115+
data:
116+
| BufferKeyInputSlotData
117+
| BufferSeqInputSlotData
118+
| SectionBufferInputSlotData,
113119
): DiagramEditorEdge<'buffer'> {
114120
return {
115121
...createBaseEdge(source, target),
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { Edge } from '../types/react-flow';
2+
import type { DefaultInputSlotData, SectionInputSlotData } from './input-slots';
3+
4+
/**
5+
* A specialization of `Edge` that enforces the edge input slot data is valid for data edges.
6+
*/
7+
export type DataEdge<
8+
O extends Record<string, unknown>,
9+
S extends string,
10+
> = Edge<O, DefaultInputSlotData | SectionInputSlotData, S>;

diagram-editor/frontend/edges/default-edge.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type EdgeProps, StepEdge } from '@xyflow/react';
22
import { memo } from 'react';
3-
import type { DataEdge } from '.';
3+
import type { DataEdge } from './data-edge';
44

55
export type DefaultEdgeOutputData = Record<string, never>;
66

diagram-editor/frontend/edges/fork-result-err-edge.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type EdgeProps, StepEdge } from '@xyflow/react';
22
import { memo } from 'react';
3-
import type { DataEdge } from '.';
3+
import type { DataEdge } from './data-edge';
44

55
export type ForkResultErrOutputData = Record<string, never>;
66
export type ForkResultErrEdge = DataEdge<

diagram-editor/frontend/edges/fork-result-ok-edge.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type EdgeProps, StepEdge } from '@xyflow/react';
22
import { memo } from 'react';
3-
import type { DataEdge } from '.';
3+
import type { DataEdge } from './data-edge';
44

55
export type ForkResultOkOutputData = Record<string, never>;
66
export type ForkResultOkEdge = DataEdge<ForkResultOkOutputData, 'forkResultOk'>;

diagram-editor/frontend/edges/index.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import type { Edge } from '../types/react-flow';
21
import { type BufferEdge, BufferEdgeComp } from './buffer-edge';
2+
import type { DataEdge } from './data-edge';
33
import { type DefaultEdge, DefaultEdgeComp } from './default-edge';
44
import ForkResultErrEdgeComp, {
55
type ForkResultErrEdge,
66
} from './fork-result-err-edge';
77
import ForkResultOkEdgeComp, {
88
type ForkResultOkEdge,
99
} from './fork-result-ok-edge';
10-
import {
11-
type SectionEdge,
12-
type SectionInputSlotData,
13-
SectionOutputEdgeComp,
14-
} from './section-edge';
10+
import { type SectionEdge, SectionOutputEdgeComp } from './section-edge';
1511
import SplitKeyEdgeComp, { type SplitKeyEdge } from './split-key-edge';
1612
import SplitRemainingEdgeComp, {
1713
type SplitRemainingEdge,
@@ -22,21 +18,15 @@ import UnzipEdgeComp, { type UnzipEdge } from './unzip-edge';
2218

2319
export type { BufferEdge } from './buffer-edge';
2420
export * from './create-edge';
21+
export type * from './input-slots';
2522
export type { SectionEdge } from './section-edge';
2623
export type { SplitKeyEdge } from './split-key-edge';
2724
export type { SplitRemainingEdge } from './split-remaining-edge';
2825
export type { SplitSeqEdge } from './split-seq-edge';
2926
export type { StreamOutEdge } from './stream-out-edge';
3027
export type { UnzipEdge } from './unzip-edge';
3128

32-
export type DefaultInputSlotData = { type: 'default' };
33-
34-
export type DataEdge<
35-
O extends Record<string, unknown>,
36-
S extends string,
37-
> = Edge<O, DefaultInputSlotData | SectionInputSlotData, S>;
38-
39-
export type EdgeMapping = {
29+
type EdgeMapping = {
4030
default: DefaultEdge;
4131
unzip: UnzipEdge;
4232
forkResultOk: ForkResultOkEdge;
@@ -56,6 +46,9 @@ export type EdgeData<K extends EdgeTypes = EdgeTypes> = EdgeMapping[K]['data'];
5646
export type EdgeOutputData<K extends EdgeTypes = EdgeTypes> =
5747
EdgeData<K>['output'];
5848

49+
export type EdgeInputData<K extends EdgeTypes = EdgeTypes> =
50+
EdgeData<K>['input'];
51+
5952
export const EDGE_TYPES = {
6053
default: DefaultEdgeComp,
6154
unzip: UnzipEdgeComp,
@@ -77,7 +70,7 @@ export enum EdgeCategory {
7770
Stream,
7871
}
7972

80-
export const EDGE_CATEGORIES: Record<EdgeTypes, EdgeCategory> = {
73+
export const EDGE_CATEGORIES = {
8174
buffer: EdgeCategory.Buffer,
8275
forkResultOk: EdgeCategory.Data,
8376
forkResultErr: EdgeCategory.Data,
@@ -88,7 +81,13 @@ export const EDGE_CATEGORIES: Record<EdgeTypes, EdgeCategory> = {
8881
streamOut: EdgeCategory.Stream,
8982
unzip: EdgeCategory.Data,
9083
section: EdgeCategory.Data,
91-
};
84+
} satisfies Record<EdgeTypes, EdgeCategory>;
85+
86+
export type DataEdgeTypes = {
87+
[K in EdgeTypes]: (typeof EDGE_CATEGORIES)[K] extends EdgeCategory.Data
88+
? K
89+
: never;
90+
}[EdgeTypes];
9291

9392
export function isDataEdge<T extends EdgeTypes>(
9493
edge: DiagramEditorEdge<T>,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export type DefaultInputSlotData = { type: 'default' };
2+
3+
export type SectionInputSlotData = {
4+
type: 'sectionInput';
5+
inputId: string;
6+
};
7+
8+
export type BufferKeyInputSlotData = {
9+
type: 'bufferKey';
10+
key: string;
11+
};
12+
13+
export type BufferSeqInputSlotData = {
14+
type: 'bufferSeq';
15+
seq: number;
16+
};
17+
18+
export type SectionBufferInputSlotData = {
19+
type: 'sectionBuffer';
20+
inputId: string;
21+
};
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type EdgeProps, StepEdge } from '@xyflow/react';
22
import { memo } from 'react';
3-
import type { DataEdge } from '.';
3+
import type { DataEdge } from './data-edge';
44

55
export type SectionOutputData = {
66
output: string;
@@ -13,13 +13,3 @@ export type SectionOutputEdgeProps = Exclude<EdgeProps<SectionEdge>, 'label'>;
1313
export const SectionOutputEdgeComp = memo((props: SectionOutputEdgeProps) => {
1414
return <StepEdge {...props} label={props.data.output.output} />;
1515
});
16-
17-
export type SectionInputSlotData = {
18-
type: 'sectionInput';
19-
inputId: string;
20-
};
21-
22-
export type SectionBufferSlotData = {
23-
type: 'sectionBuffer';
24-
inputId: string;
25-
};

diagram-editor/frontend/edges/split-key-edge.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type EdgeProps, StepEdge } from '@xyflow/react';
22
import { memo } from 'react';
3-
import type { DataEdge } from '.';
3+
import type { DataEdge } from './data-edge';
44

55
export type SplitKeyOutputData = {
66
key: string;

0 commit comments

Comments
 (0)