From 10d79fcbecaad1f18f843ddb8ecc27596d04da14 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 5 Oct 2023 14:49:58 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20refactor(buildTrigger/index.tsx)?= =?UTF-8?q?:=20remove=20unused=20processFlow=20function=20to=20improve=20c?= =?UTF-8?q?ode=20readability=20and=20maintainability=20=F0=9F=94=A7=20refa?= =?UTF-8?q?ctor(buildTrigger/index.tsx):=20remove=20unnecessary=20'finishe?= =?UTF-8?q?d'=20variable=20and=20while=20loop=20to=20simplify=20code=20log?= =?UTF-8?q?ic=20=F0=9F=94=A7=20refactor(buildTrigger/index.tsx):=20remove?= =?UTF-8?q?=20unnecessary=20return=20statement=20and=20resolve/reject=20pr?= =?UTF-8?q?omises=20directly=20to=20improve=20code=20readability=20?= =?UTF-8?q?=F0=9F=94=A7=20refactor(buildTrigger/index.tsx):=20remove=20unu?= =?UTF-8?q?sed=20'isValid'=20variable=20and=20directly=20push=20validation?= =?UTF-8?q?=20results=20to=20'validationResults'=20array=20=F0=9F=94=A7=20?= =?UTF-8?q?refactor(buildTrigger/index.tsx):=20remove=20unnecessary=20'els?= =?UTF-8?q?e'=20statements=20and=20simplify=20code=20logic=20=F0=9F=94=A7?= =?UTF-8?q?=20refactor(buildTrigger/index.tsx):=20remove=20unnecessary=20'?= =?UTF-8?q?setProgress'=20function=20call=20and=20directly=20set=20progres?= =?UTF-8?q?s=20value=20=F0=9F=94=A7=20refactor(buildTrigger/index.tsx):=20?= =?UTF-8?q?remove=20unnecessary=20'setSuccessData'=20and=20'setErrorData'?= =?UTF-8?q?=20function=20calls=20and=20directly=20set=20success=20and=20er?= =?UTF-8?q?ror=20data=20values=20=F0=9F=94=A7=20refactor(buildTrigger/inde?= =?UTF-8?q?x.tsx):=20remove=20unnecessary=20type=20assertion=20for=20'setT?= =?UTF-8?q?absState'=20function=20call=20=F0=9F=94=A7=20refactor(buildTrig?= =?UTF-8?q?ger/index.tsx):=20remove=20unnecessary=20'setIsBuilding'=20func?= =?UTF-8?q?tion=20call=20=F0=9F=94=A7=20refactor(buildTrigger/index.tsx):?= =?UTF-8?q?=20remove=20unnecessary=20'eventSource.close()'=20function=20ca?= =?UTF-8?q?ll=20in=20'onerror'=20event=20handler=20=F0=9F=94=A7=20refactor?= =?UTF-8?q?(buildTrigger/index.tsx):=20remove=20unnecessary=20'resolve'=20?= =?UTF-8?q?function=20call=20in=20'onmessage'=20event=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chatComponent/buildTrigger/index.tsx | 99 +++++++++---------- 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/src/frontend/src/components/chatComponent/buildTrigger/index.tsx b/src/frontend/src/components/chatComponent/buildTrigger/index.tsx index 43016ddeb..ad12baa31 100644 --- a/src/frontend/src/components/chatComponent/buildTrigger/index.tsx +++ b/src/frontend/src/components/chatComponent/buildTrigger/index.tsx @@ -10,7 +10,7 @@ import { FlowType } from "../../../types/flow"; import { TabsContext } from "../../../contexts/tabsContext"; import { parsedDataType } from "../../../types/components"; import { TabsState } from "../../../types/tabs"; -import { processFlow, validateNodes } from "../../../utils/reactflowUtils"; +import { validateNodes } from "../../../utils/reactflowUtils"; import RadialProgressComponent from "../../RadialProgress"; import IconComponent from "../../genericIconComponent"; @@ -77,61 +77,54 @@ export default function BuildTrigger({ const { flowId } = response.data; // Step 2: Use the session ID to establish an SSE connection using EventSource let validationResults: boolean[] = []; - let finished = false; const apiUrl = `/api/v1/build/stream/${flowId}`; - const eventSource = new EventSource(apiUrl); + return new Promise((resolve, reject) => { + const eventSource = new EventSource(apiUrl); - eventSource.onmessage = (event) => { - // If the event is parseable, return - if (!event.data) { - return; - } - const parsedData = JSON.parse(event.data); - // if the event is the end of the stream, close the connection - if (parsedData.end_of_stream) { - // Close the connection and finish - finished = true; + eventSource.onmessage = (event) => { + // If the event is parseable, return + if (!event.data) { + return; + } + const parsedData = JSON.parse(event.data); + // if the event is the end of the stream, close the connection + if (parsedData.end_of_stream) { + eventSource.close(); + resolve(validationResults.every((result) => result)); + } else if (parsedData.log) { + // If the event is a log, log it + setSuccessData({ title: parsedData.log }); + } else if (parsedData.input_keys !== undefined) { + //@ts-ignore + setTabsState((old: TabsState) => { + return { + ...old, + [flowId]: { + ...old[flowId], + formKeysData: parsedData, + }, + }; + }); + } else { + // Otherwise, process the data + const isValid = processStreamResult(parsedData); + setProgress(parsedData.progress); + validationResults.push(isValid); + } + }; + + eventSource.onerror = (error: any) => { + console.error("EventSource failed:", error); + + if (error.data) { + const parsedData = JSON.parse(error.data); + setErrorData({ title: parsedData.error }); + setIsBuilding(false); + } eventSource.close(); - - return; - } else if (parsedData.log) { - // If the event is a log, log it - setSuccessData({ title: parsedData.log }); - } else if (parsedData.input_keys !== undefined) { - //@ts-ignore - setTabsState((old: TabsState) => { - return { - ...old, - [flowId]: { - ...old[flowId], - formKeysData: parsedData, - }, - }; - }); - } else { - // Otherwise, process the data - const isValid = processStreamResult(parsedData); - setProgress(parsedData.progress); - validationResults.push(isValid); - } - }; - - eventSource.onerror = (error: any) => { - console.error("EventSource failed:", error); - eventSource.close(); - if (error.data) { - const parsedData = JSON.parse(error.data); - setErrorData({ title: parsedData.error }); - setIsBuilding(false); - } - }; - // Step 3: Wait for the stream to finish - while (!finished) { - await new Promise((resolve) => setTimeout(resolve, 100)); - finished = validationResults.length === processFlow(flow.data!).nodes.length; - } - // Step 4: Return true if all nodes are valid, false otherwise - return validationResults.every((result) => result); + reject(new Error("Streaming failed")); + }; + }); } function processStreamResult(parsedData: parsedDataType) {