🔨 refactor(buildTrigger): change allNodesValid to validationResults array and add finished variable to improve readability

🚀 feat(buildTrigger): add waiting for stream to finish and return validation results for each node
The allNodesValid variable has been changed to a validationResults array to store the validation results for each node. A finished variable has been added to improve readability and to wait for the stream to finish. The function now returns an array of validation results for each node.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-12 13:05:02 -03:00
commit 64397d2830

View file

@ -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) {