From 10f67667d50d2930dfd53bfcce3ee309232c4616 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Thu, 18 Jan 2024 18:48:18 -0300 Subject: [PATCH] Refactor flowStore and flowsIOStore --- src/frontend/src/stores/flowStore.ts | 50 ++++++++ src/frontend/src/stores/flowsIOStore.ts | 144 +----------------------- 2 files changed, 51 insertions(+), 143 deletions(-) diff --git a/src/frontend/src/stores/flowStore.ts b/src/frontend/src/stores/flowStore.ts index 485ac01fd..08ea40348 100644 --- a/src/frontend/src/stores/flowStore.ts +++ b/src/frontend/src/stores/flowStore.ts @@ -343,6 +343,56 @@ const useFlowStore = create((set, get) => ({ }); }); }, + getInputTypes: () => { + let inputType: string[] = []; + const nodes = get().nodes; + nodes.forEach((node) => { + const nodeData: NodeDataType = node.data as NodeDataType; + if (isInputNode(nodeData)) { + // TODO remove count and ramdom key from type before pushing + inputType.push(nodeData.type); + } + }); + set({ inputTypes: inputType }); + return inputType; + }, + getOutputTypes: () => { + let outputType: string[] = []; + const nodes = get().nodes; + nodes.forEach((node) => { + const nodeData: NodeDataType = node.data as NodeDataType; + if (isOutputNode(nodeData)) { + outputType.push(nodeData.type); + } + }); + set({ outputTypes: outputType }); + return outputType; + }, + getInputIds: () => { + let inputIds: string[] = []; + const nodes = get().nodes; + nodes.forEach((node) => { + const nodeData: NodeDataType = node.data as NodeDataType; + if (isInputNode(nodeData)) { + inputIds.push(nodeData.id); + } + }); + set({ inputIds }); + return inputIds; + }, + getOutputIds: () => { + let outputIds: string[] = []; + const nodes = get().nodes; + + nodes.forEach((node) => { + const nodeData: NodeDataType = node.data as NodeDataType; + if (isOutputNode(nodeData)) { + outputIds.push(nodeData.id); + } + }); + set({ outputIds }); + return outputIds; + }, })); export default useFlowStore; diff --git a/src/frontend/src/stores/flowsIOStore.ts b/src/frontend/src/stores/flowsIOStore.ts index 1d7e0e0da..a977ba9ca 100644 --- a/src/frontend/src/stores/flowsIOStore.ts +++ b/src/frontend/src/stores/flowsIOStore.ts @@ -1,8 +1,4 @@ -import { cloneDeep } from "lodash"; import { create } from "zustand"; -import { FlowType, NodeDataType, NodeType } from "../types/flow"; -import { flowIOStoreType } from "../types/zustand/flowIOStore"; -import { isInputNode, isOutputNode } from "../utils/reactflowUtils"; import useAlertStore from "./alertStore"; import useFlowStore from "./flowStore"; import useFlowsManagerStore from "./flowsManagerStore"; @@ -13,145 +9,7 @@ const { reactFlowInstance, paste } = useFlowStore(); const { saveFlow } = useFlowsManagerStore(); const { setErrorData, setNoticeData } = useAlertStore(); -const useFlowIOStore = create((set, get) => ({ - flowPool: {}, - outputTypes: [], - inputTypes: [], - inputIds: [], - outputIds: [], - setFlowPool: (flowPool) => { - set({ flowPool }); - }, - setOutputTypes: (outputTypes) => { - set({ outputTypes }); - }, - setInputTypes: (inputTypes) => { - set({ inputTypes }); - }, - setInputIds: (inputIds) => { - set({ inputIds }); - }, - setOutputIds: (outputIds) => { - set({ outputIds }); - }, - - updateFlowPoolNodes: (nodes: NodeType[]) => { - //this function will update the removing the old ones - const nodeIdsSet = new Set(nodes.map((node) => node.id)); - set((state) => { - let newFlowPool = cloneDeep({ ...state.flowPool }); - Object.keys(newFlowPool).forEach((nodeId) => { - if (!nodeIdsSet.has(nodeId)) { - delete newFlowPool[nodeId]; - } - }); - return { flowPool: newFlowPool }; - }); - }, - addDataToFlowPool: (data: any, nodeId: string) => { - set((state) => { - let newFlowPool = cloneDeep({ ...state.flowPool }); - if (!newFlowPool[nodeId]) newFlowPool[nodeId] = [data]; - else { - newFlowPool[nodeId].push(data); - } - return { flowPool: newFlowPool }; - }); - }, - CleanFlowPool: () => { - set({ flowPool: {} }); - }, - checkInputandOutput: (flow?: FlowType) => { - let has_input = false; - let has_output = false; - if (!flow && !reactFlowInstance) { - return false; - } - const nodes = flow?.data?.nodes - ? flow.data.nodes - : reactFlowInstance!.getNodes(); - nodes.forEach((node) => { - const nodeData: NodeDataType = node.data as NodeDataType; - if (isInputNode(nodeData)) { - has_input = true; - } - if (isOutputNode(nodeData)) { - has_output = true; - } - }); - return has_input && has_output; - }, - getInputTypes: (flow?: FlowType) => { - let inputType: string[] = []; - if (!flow && !reactFlowInstance) { - return []; - } - const nodes = flow?.data?.nodes - ? flow.data.nodes - : reactFlowInstance!.getNodes(); - nodes.forEach((node) => { - const nodeData: NodeDataType = node.data as NodeDataType; - if (isInputNode(nodeData)) { - // TODO remove count and ramdom key from type before pushing - inputType.push(nodeData.type); - } - }); - set({ inputTypes: inputType }); - return inputType; - }, - getOutputTypes: (flow?: FlowType) => { - let outputType: string[] = []; - if (!flow && !reactFlowInstance) { - return []; - } - const nodes = flow?.data?.nodes - ? flow.data.nodes - : reactFlowInstance!.getNodes(); - nodes.forEach((node) => { - const nodeData: NodeDataType = node.data as NodeDataType; - if (isOutputNode(nodeData)) { - // TODO remove count and ramdom key from type before pushing - outputType.push(nodeData.type); - } - }); - set({ outputTypes: outputType }); - return outputType; - }, - getInputIds: (flow?: FlowType) => { - let inputIds: string[] = []; - if (!flow && !reactFlowInstance) { - return []; - } - const nodes = flow?.data?.nodes - ? flow.data.nodes - : reactFlowInstance!.getNodes(); - nodes.forEach((node) => { - const nodeData: NodeDataType = node.data as NodeDataType; - if (isInputNode(nodeData)) { - inputIds.push(nodeData.id); - } - }); - set({ inputIds }); - return inputIds; - }, - getOutputIds: (flow?: FlowType) => { - let outputIds: string[] = []; - if (!flow && !reactFlowInstance) { - return []; - } - const nodes = flow?.data?.nodes - ? flow.data.nodes - : reactFlowInstance!.getNodes(); - - nodes.forEach((node) => { - const nodeData: NodeDataType = node.data as NodeDataType; - if (isOutputNode(nodeData)) { - outputIds.push(nodeData.id); - } - }); - set({ outputIds }); - return outputIds; - }, +const useFlowIOStore = create((set, get) => ({ /* buildFlow: async (nodeId?: string) => { function handleBuildUpdate(data: any) { get().addDataToFlowPool(data.data[data.id], data.id);