🔨 refactor(buildTrigger): remove unused state variable 'valid' and simplify streamNodeData function

The 'valid' state variable was not being used, so it was removed. The 'streamNodeData' function was simplified by removing the 'onStreamComplete' callback and instead using a while loop to wait for the stream to finish. The function now returns a boolean indicating whether all nodes in the flow are valid or not.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-15 15:30:51 -03:00
commit f21bc80359

View file

@ -25,7 +25,6 @@ export default function BuildTrigger({
const { updateSSEData } = useSSE(); const { updateSSEData } = useSSE();
const { reactFlowInstance } = useContext(typesContext); const { reactFlowInstance } = useContext(typesContext);
const { setErrorData } = useContext(alertContext); const { setErrorData } = useContext(alertContext);
const [valid, setValid] = useState(true);
async function handleBuild(flow: FlowType) { async function handleBuild(flow: FlowType) {
try { try {
@ -44,10 +43,7 @@ export default function BuildTrigger({
const startTime = Date.now(); const startTime = Date.now();
setIsBuilding(true); setIsBuilding(true);
try { const allNodesValid = await streamNodeData(flow);
const allNodesValid = await streamNodeData(flow, (isAllNodesValid) => {
setIsBuilt(isAllNodesValid);
});
await enforceMinimumLoadingTime(startTime, minimumLoadingTime); await enforceMinimumLoadingTime(startTime, minimumLoadingTime);
setIsBuilt(allNodesValid); setIsBuilt(allNodesValid);
} catch (error) { } catch (error) {
@ -55,22 +51,19 @@ export default function BuildTrigger({
} finally { } finally {
setIsBuilding(false); setIsBuilding(false);
} }
} catch (error) {
console.error("Error:", error);
} finally {
setIsBuilding(false);
}
} }
async function streamNodeData(flow: FlowType, onStreamComplete) { async function streamNodeData(flow: FlowType) {
// Make a POST request to send the flow data and receive a unique session ID // Step 1: Make a POST request to send the flow data and receive a unique session ID
const response = await postBuildInit(flow); const response = await postBuildInit(flow);
const { flowId } = response.data; const { flowId } = response.data;
// Use the session ID to establish an SSE connection using EventSource
// Step 2: Use the session ID to establish an SSE connection using EventSource
let validationResults = [];
let finished = false;
const apiUrl = `/api/v1/build/stream/${flowId}`; const apiUrl = `/api/v1/build/stream/${flowId}`;
const eventSource = new EventSource(apiUrl); const eventSource = new EventSource(apiUrl);
try {
eventSource.onmessage = (event) => { eventSource.onmessage = (event) => {
// If the event is parseable, return // If the event is parseable, return
if (!event.data) { if (!event.data) {
@ -80,22 +73,25 @@ export default function BuildTrigger({
// if the event is the end of the stream, close the connection // if the event is the end of the stream, close the connection
if (parsedData.end_of_stream) { if (parsedData.end_of_stream) {
eventSource.close(); eventSource.close();
onStreamComplete(valid);
return; return;
} }
// Otherwise, process the data // Otherwise, process the data
processStreamResult(parsedData); const isValid = processStreamResult(parsedData);
validationResults.push(isValid);
}; };
eventSource.onerror = (error) => { eventSource.onerror = (error) => {
console.error("EventSource failed:", error); console.error("EventSource failed:", error);
eventSource.close(); eventSource.close();
onStreamComplete(valid);
}; };
} catch (e) { // Step 3: Wait for the stream to finish
console.log(e); while (!finished) {
eventSource.close(); 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) { function processStreamResult(parsedData) {
@ -106,9 +102,7 @@ export default function BuildTrigger({
} catch (err) { } catch (err) {
console.log("Error parsing stream data: ", err); console.log("Error parsing stream data: ", err);
} }
if (!parsedData.valid) { return parsedData.valid;
setValid(false);
}
} }
async function enforceMinimumLoadingTime( async function enforceMinimumLoadingTime(