Refactor: Make changes in buildVertices Promises and build
This commit is contained in:
parent
5ffb60e90f
commit
06ed5e371c
2 changed files with 51 additions and 73 deletions
|
|
@ -130,7 +130,7 @@ export type Component = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type VerticesOrderTypeAPI = {
|
export type VerticesOrderTypeAPI = {
|
||||||
ids: Array<string>;
|
ids: Array<Array<string>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type VertexBuildTypeAPI = {
|
export type VertexBuildTypeAPI = {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { AxiosError } from "axios";
|
import { AxiosError } from "axios";
|
||||||
import { getVerticesOrder, postBuildVertex } from "../controllers/API";
|
import { getVerticesOrder, postBuildVertex } from "../controllers/API";
|
||||||
import { FlowType } from "../types/flow";
|
import { FlowType } from "../types/flow";
|
||||||
|
import { VertexBuildTypeAPI } from "../types/api";
|
||||||
|
|
||||||
type BuildVerticesParams = {
|
type BuildVerticesParams = {
|
||||||
flow: FlowType; // Assuming FlowType is the type for your flow
|
flow: FlowType; // Assuming FlowType is the type for your flow
|
||||||
|
|
@ -19,77 +20,54 @@ export async function buildVertices({
|
||||||
onBuildComplete,
|
onBuildComplete,
|
||||||
onBuildError,
|
onBuildError,
|
||||||
}: BuildVerticesParams) {
|
}: BuildVerticesParams) {
|
||||||
try {
|
let orderResponse = await getVerticesOrder(flow.id);
|
||||||
// Step 1: Get vertices order
|
let verticesOrder: Array<Array<string>> = orderResponse.data.ids;
|
||||||
console.log(JSON.parse(JSON.stringify(flow)));
|
let vertices : Array<Array<string>> = [];
|
||||||
let orderResponse = await getVerticesOrder(flow.id);
|
if (nodeId) {
|
||||||
console.log(orderResponse);
|
for (let i = 0; i < verticesOrder.length; i += 1) {
|
||||||
let verticesOrder: Array<Array<string>> = orderResponse.data.ids;
|
const innerArray = verticesOrder[i];
|
||||||
console.log("order", verticesOrder);
|
const idIndex = innerArray.indexOf(nodeId);
|
||||||
|
|
||||||
// Determine the range of vertices to build
|
if (idIndex !== -1) {
|
||||||
let vertexIndex: number | null = null;
|
// If the targetId is found in the inner array, cut the array before the id
|
||||||
if (nodeId) {
|
vertices.push(innerArray.slice(0, idIndex + 1));
|
||||||
vertexIndex = verticesOrder.findIndex((ids) => ids.includes(nodeId));
|
break; // Stop searching after finding the first occurrence
|
||||||
}
|
}
|
||||||
let buildRange =
|
// If the targetId is not found, include the entire inner array
|
||||||
vertexIndex !== null
|
vertices.push(innerArray);
|
||||||
? verticesOrder.slice(0, vertexIndex + 1)
|
|
||||||
: verticesOrder;
|
|
||||||
console.log(buildRange);
|
|
||||||
|
|
||||||
const buildResults: boolean[] = [];
|
|
||||||
await buildRange.reduce(async (previousPromise, idArray) => {
|
|
||||||
await previousPromise;
|
|
||||||
return Promise.all(
|
|
||||||
idArray.map(async (vertexId) => {
|
|
||||||
try {
|
|
||||||
const buildResponse = await postBuildVertex(flow, vertexId);
|
|
||||||
const buildData = buildResponse.data;
|
|
||||||
if (onProgressUpdate) {
|
|
||||||
const progress =
|
|
||||||
verticesOrder
|
|
||||||
.map((ids) => ids.indexOf(vertexId))
|
|
||||||
.filter((index) => index !== -1)[0] /
|
|
||||||
verticesOrder.flat().length;
|
|
||||||
onProgressUpdate(progress);
|
|
||||||
}
|
|
||||||
if (onBuildUpdate) {
|
|
||||||
let data = {};
|
|
||||||
if (!buildData.valid) {
|
|
||||||
if (onBuildError) {
|
|
||||||
onBuildError("Error Building Component", [buildData.params]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
data[buildData.id] = buildData;
|
|
||||||
|
|
||||||
onBuildUpdate({ data, id: buildData.id });
|
|
||||||
}
|
|
||||||
buildResults.push(buildData.valid);
|
|
||||||
} catch (error) {
|
|
||||||
if (onBuildError) {
|
|
||||||
console.log(error);
|
|
||||||
onBuildError("Error Building Component", [
|
|
||||||
(error as AxiosError<any>).response?.data?.detail ??
|
|
||||||
"Unknown Error",
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}, Promise.resolve());
|
|
||||||
|
|
||||||
// Callback for when all vertices have been built
|
|
||||||
if (onBuildComplete) {
|
|
||||||
const allNodesValid = buildResults.every((result) => result);
|
|
||||||
onBuildComplete(allNodesValid);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Callback for handling errors
|
|
||||||
if (onBuildError) {
|
|
||||||
onBuildError("Error Building Component", [
|
|
||||||
(error as AxiosError<any>).response?.data?.detail ?? "Unknown Error",
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const buildResults: Array<boolean> = [];
|
||||||
|
for (let i = 0; i < vertices.length; i += 1) {
|
||||||
|
await Promise.all(
|
||||||
|
vertices[i].map(async (id) => {
|
||||||
|
try {
|
||||||
|
const buildRes = await postBuildVertex(flow, id);
|
||||||
|
const buildData: VertexBuildTypeAPI = buildRes.data;
|
||||||
|
if (onBuildUpdate) {
|
||||||
|
let data = {};
|
||||||
|
if (!buildData.valid) {
|
||||||
|
if (onBuildError) {
|
||||||
|
onBuildError("Error Building Component", [buildData.params])
|
||||||
|
}
|
||||||
|
data[buildData.id] = buildData;
|
||||||
|
|
||||||
|
onBuildUpdate({ data, id: buildData.id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buildResults.push(buildData.valid);
|
||||||
|
if (onBuildComplete) {
|
||||||
|
const allNodesValid = buildResults.every((result) => result);
|
||||||
|
onBuildComplete(allNodesValid);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (onBuildError) {
|
||||||
|
console.log(error)
|
||||||
|
onBuildError("Error Building Component", [(error as AxiosError<any>).response?.data?.detail??"Unknown Error"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue