🔧 refactor(buildTrigger/index.tsx): remove unused processFlow function to improve code readability and maintainability
🔧 refactor(buildTrigger/index.tsx): remove unnecessary 'finished' variable and while loop to simplify code logic 🔧 refactor(buildTrigger/index.tsx): remove unnecessary return statement and resolve/reject promises directly to improve code readability 🔧 refactor(buildTrigger/index.tsx): remove unused 'isValid' variable and directly push validation results to 'validationResults' array 🔧 refactor(buildTrigger/index.tsx): remove unnecessary 'else' statements and simplify code logic 🔧 refactor(buildTrigger/index.tsx): remove unnecessary 'setProgress' function call and directly set progress value 🔧 refactor(buildTrigger/index.tsx): remove unnecessary 'setSuccessData' and 'setErrorData' function calls and directly set success and error data values 🔧 refactor(buildTrigger/index.tsx): remove unnecessary type assertion for 'setTabsState' function call 🔧 refactor(buildTrigger/index.tsx): remove unnecessary 'setIsBuilding' function call 🔧 refactor(buildTrigger/index.tsx): remove unnecessary 'eventSource.close()' function call in 'onerror' event handler 🔧 refactor(buildTrigger/index.tsx): remove unnecessary 'resolve' function call in 'onmessage' event handler
This commit is contained in:
parent
06bbfa2e04
commit
10d79fcbec
1 changed files with 46 additions and 53 deletions
|
|
@ -10,7 +10,7 @@ import { FlowType } from "../../../types/flow";
|
||||||
import { TabsContext } from "../../../contexts/tabsContext";
|
import { TabsContext } from "../../../contexts/tabsContext";
|
||||||
import { parsedDataType } from "../../../types/components";
|
import { parsedDataType } from "../../../types/components";
|
||||||
import { TabsState } from "../../../types/tabs";
|
import { TabsState } from "../../../types/tabs";
|
||||||
import { processFlow, validateNodes } from "../../../utils/reactflowUtils";
|
import { validateNodes } from "../../../utils/reactflowUtils";
|
||||||
import RadialProgressComponent from "../../RadialProgress";
|
import RadialProgressComponent from "../../RadialProgress";
|
||||||
import IconComponent from "../../genericIconComponent";
|
import IconComponent from "../../genericIconComponent";
|
||||||
|
|
||||||
|
|
@ -77,8 +77,8 @@ export default function BuildTrigger({
|
||||||
const { flowId } = response.data;
|
const { flowId } = response.data;
|
||||||
// Step 2: 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: boolean[] = [];
|
let validationResults: boolean[] = [];
|
||||||
let finished = false;
|
|
||||||
const apiUrl = `/api/v1/build/stream/${flowId}`;
|
const apiUrl = `/api/v1/build/stream/${flowId}`;
|
||||||
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
const eventSource = new EventSource(apiUrl);
|
const eventSource = new EventSource(apiUrl);
|
||||||
|
|
||||||
eventSource.onmessage = (event) => {
|
eventSource.onmessage = (event) => {
|
||||||
|
|
@ -89,11 +89,8 @@ export default function BuildTrigger({
|
||||||
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) {
|
||||||
// Close the connection and finish
|
|
||||||
finished = true;
|
|
||||||
eventSource.close();
|
eventSource.close();
|
||||||
|
resolve(validationResults.every((result) => result));
|
||||||
return;
|
|
||||||
} else if (parsedData.log) {
|
} else if (parsedData.log) {
|
||||||
// If the event is a log, log it
|
// If the event is a log, log it
|
||||||
setSuccessData({ title: parsedData.log });
|
setSuccessData({ title: parsedData.log });
|
||||||
|
|
@ -118,20 +115,16 @@ export default function BuildTrigger({
|
||||||
|
|
||||||
eventSource.onerror = (error: any) => {
|
eventSource.onerror = (error: any) => {
|
||||||
console.error("EventSource failed:", error);
|
console.error("EventSource failed:", error);
|
||||||
eventSource.close();
|
|
||||||
if (error.data) {
|
if (error.data) {
|
||||||
const parsedData = JSON.parse(error.data);
|
const parsedData = JSON.parse(error.data);
|
||||||
setErrorData({ title: parsedData.error });
|
setErrorData({ title: parsedData.error });
|
||||||
setIsBuilding(false);
|
setIsBuilding(false);
|
||||||
}
|
}
|
||||||
|
eventSource.close();
|
||||||
|
reject(new Error("Streaming failed"));
|
||||||
};
|
};
|
||||||
// Step 3: Wait for the stream to finish
|
});
|
||||||
while (!finished) {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
||||||
finished = validationResults.length === processFlow(flow.data!).nodes.length;
|
|
||||||
}
|
|
||||||
// Step 4: Return true if all nodes are valid, false otherwise
|
|
||||||
return validationResults.every((result) => result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function processStreamResult(parsedData: parsedDataType) {
|
function processStreamResult(parsedData: parsedDataType) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue