🔨 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:
parent
baeadf018d
commit
64397d2830
1 changed files with 20 additions and 6 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue