From 64397d28305e7765599fd81fc884478d14682925 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 12 Jun 2023 13:05:02 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor(buildTrigger):=20change?= =?UTF-8?q?=20allNodesValid=20to=20validationResults=20array=20and=20add?= =?UTF-8?q?=20finished=20variable=20to=20improve=20readability=20?= =?UTF-8?q?=F0=9F=9A=80=20feat(buildTrigger):=20add=20waiting=20for=20stre?= =?UTF-8?q?am=20to=20finish=20and=20return=20validation=20results=20for=20?= =?UTF-8?q?each=20node=20The=20allNodesValid=20variable=20has=20been=20cha?= =?UTF-8?q?nged=20to=20a=20validationResults=20array=20to=20store=20the=20?= =?UTF-8?q?validation=20results=20for=20each=20node.=20A=20finished=20vari?= =?UTF-8?q?able=20has=20been=20added=20to=20improve=20readability=20and=20?= =?UTF-8?q?to=20wait=20for=20the=20stream=20to=20finish.=20The=20function?= =?UTF-8?q?=20now=20returns=20an=20array=20of=20validation=20results=20for?= =?UTF-8?q?=20each=20node.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chatComponent/buildTrigger/index.tsx | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/frontend/src/components/chatComponent/buildTrigger/index.tsx b/src/frontend/src/components/chatComponent/buildTrigger/index.tsx index 897a79c17..5d93d29b2 100644 --- a/src/frontend/src/components/chatComponent/buildTrigger/index.tsx +++ b/src/frontend/src/components/chatComponent/buildTrigger/index.tsx @@ -49,25 +49,39 @@ export default function BuildTrigger({ // Step 2: Use the session ID to establish an SSE connection using EventSource - let allNodesValid = true; + let validationResults = []; + let finished = false; apiUrl = `/build/stream/${flowId}`; const eventSource = new EventSource(apiUrl); eventSource.onmessage = (event) => { - const parsedData = JSON.parse(event.data); - if (parsedData.end_of_stream) { - eventSource.close(); + // 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(); + + return; + } + // Otherwise, process the data const isValid = processStreamResult(parsedData); - allNodesValid = allNodesValid && isValid; + validationResults.push(isValid); }; eventSource.onerror = (error) => { console.error("EventSource failed:", error); eventSource.close(); }; - return allNodesValid; + // Step 3: Wait for the stream to finish + while (!finished) { + await new Promise((resolve) => setTimeout(resolve, 100)); + finished = validationResults.length === flow.data.nodes.length; + } + // Step 4: Return true if all nodes are valid, false otherwise + return validationResults.every((result) => result); } function processStreamResult(parsedData) {