Refactoring all nodes functionality into their own files, create a modular framework for the visual programming. #21

Merged
9828273 merged 15 commits from refactor/node-encapsulation into dev 2025-11-20 15:00:02 +00:00
2 changed files with 9 additions and 46 deletions
Showing only changes of commit bd7620a182 - Show all commits

View File

@@ -1,6 +1,6 @@
import StartNode, { StartConnects, StartReduce } from "./nodes/StartNode";
import EndNode, { EndConnects, EndReduce } from "./nodes/EndNode";
import PhaseNode, { PhaseConnects, PhaseReduce, PhaseReduce2 } from "./nodes/PhaseNode";
import PhaseNode, { PhaseConnects, PhaseReduce } from "./nodes/PhaseNode";
import NormNode, { NormConnects, NormReduce } from "./nodes/NormNode";
import { EndNodeDefaults } from "./nodes/EndNode.default";
import { StartNodeDefaults } from "./nodes/StartNode.default";
@@ -42,7 +42,7 @@ export const NodeDefaults = {
export const NodeReduces = {
start: StartReduce,
end: EndReduce,
phase: PhaseReduce2,
phase: PhaseReduce,
norm: NormReduce,
goal: GoalReduce,
trigger: TriggerReduce,

View File

@@ -6,7 +6,7 @@ import {
} from '@xyflow/react';
import { Toolbar } from '../components/NodeComponents';
import styles from '../../VisProg.module.css';
import { NodeDefaults, NodeReduces, NodesInPhase, NodeTypes } from '../NodeRegistry';
import { NodeReduces, NodesInPhase, NodeTypes } from '../NodeRegistry';
import useFlowStore from '../VisProgStores';
import { TextField } from '../../../../components/TextField';
@@ -62,6 +62,7 @@ export default function PhaseNode(props: NodeProps<Node>) {
);
};
/**
* Reduces each phase, including its children down into its relevant data.
* @param props: The Node Properties of this node.
@@ -69,66 +70,28 @@ export default function PhaseNode(props: NodeProps<Node>) {
export function PhaseReduce(node: Node, nodes: Node[]) {
const thisnode = node as PhaseNode;
const data = thisnode.data as PhaseNodeData;
const reducableChildren = Object.entries(NodeDefaults)
.filter(([, data]) => data.hasReduce)
.map(([type]) => (
type
));
let childrenData: unknown = ""
if (data.children != undefined) {
childrenData = data.children.map((childId) => {
// Reduce each of this phases' children.
const child = nodes.find((node) => node.id == childId);
// Make sure that we reduce only valid children nodes.
if (child == undefined || child.type == undefined || !reducableChildren.includes(child.type)) return ''
const reducer = NodeReduces[child.type as keyof typeof NodeReduces]
if (!reducer) {
console.warn(`No reducer found for node type ${child.type}`);
return null;
}
return reducer(child, nodes);
})}
return {
id: thisnode.id,
name: data.label as string,
children: childrenData,
}
}
/**
* Reduces each phase, including its children down into its relevant data.
* @param props: The Node Properties of this node.
*/
export function PhaseReduce2(node: Node, nodes: Node[]) {
const thisnode = node as PhaseNode;
const data = thisnode.data as PhaseNodeData;
// node typings that are not in phase
let nodesNotInPhase: string[] = Object.entries(NodesInPhase)
const nodesNotInPhase: string[] = Object.entries(NodesInPhase)
.filter(([, f]) => !f())
.map(([t]) => t);
// node typings that then are in phase
let nodesInPhase: string[] = Object.entries(NodeTypes)
const nodesInPhase: string[] = Object.entries(NodeTypes)
.filter(([t]) => !nodesNotInPhase.includes(t))
.map(([t]) => t);
// children nodes
let childrenNodes = nodes.filter((node) => data.children.includes(node.id));
const childrenNodes = nodes.filter((node) => data.children.includes(node.id));
// Build the result object
let result: Record<string, unknown> = {
const result: Record<string, unknown> = {
id: thisnode.id,
label: data.label,
};
nodesInPhase.forEach((type) => {
let typedChildren = childrenNodes.filter((child) => child.type == type);
const typedChildren = childrenNodes.filter((child) => child.type == type);
const reducer = NodeReduces[type as keyof typeof NodeReduces];
if (!reducer) {
console.warn(`No reducer found for node type ${type}`);