🔨 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,17 +43,9 @@ 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) => { await enforceMinimumLoadingTime(startTime, minimumLoadingTime);
setIsBuilt(isAllNodesValid); setIsBuilt(allNodesValid);
});
await enforceMinimumLoadingTime(startTime, minimumLoadingTime);
setIsBuilt(allNodesValid);
} catch (error) {
console.error("Error:", error);
} finally {
setIsBuilding(false);
}
} catch (error) { } catch (error) {
console.error("Error:", error); console.error("Error:", error);
} finally { } finally {
@ -62,40 +53,45 @@ export default function BuildTrigger({
} }
} }
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) { return;
return; }
} const parsedData = JSON.parse(event.data);
const parsedData = JSON.parse(event.data); // 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();
onStreamComplete(valid);
return;
}
// Otherwise, process the data
processStreamResult(parsedData);
};
eventSource.onerror = (error) => {
console.error("EventSource failed:", error);
eventSource.close(); eventSource.close();
onStreamComplete(valid);
}; return;
} catch (e) { }
console.log(e); // Otherwise, process the data
const isValid = processStreamResult(parsedData);
validationResults.push(isValid);
};
eventSource.onerror = (error) => {
console.error("EventSource failed:", error);
eventSource.close(); eventSource.close();
};
// 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) { 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(