Refactor: Use Promise.all to proccess vertices assynchronously

This commit is contained in:
igorrCarvalho 2024-01-18 20:30:10 -03:00
commit f9f6057efc

View file

@ -23,25 +23,33 @@ export async function buildVertices({
// Step 1: Get vertices order // Step 1: Get vertices order
console.log(JSON.parse(JSON.stringify(flow))); console.log(JSON.parse(JSON.stringify(flow)));
let orderResponse = await getVerticesOrder(flow.id); let orderResponse = await getVerticesOrder(flow.id);
let verticesOrder = orderResponse.data.ids.flatMap((id) => id); console.log(orderResponse);
let verticesOrder: Array<Array<string>> = orderResponse.data.ids;
console.log('order', verticesOrder);
// Determine the range of vertices to build // Determine the range of vertices to build
let vertexIndex: number | null = null; let vertexIndex: number | null = null;
if (nodeId) { if (nodeId) {
vertexIndex = verticesOrder.indexOf(nodeId); vertexIndex = verticesOrder.findIndex((ids) => ids.includes(nodeId));
} }
let buildRange = let buildRange =
vertexIndex !== null vertexIndex !== null ? verticesOrder.slice(0, vertexIndex + 1) : verticesOrder;
? verticesOrder.slice(0, vertexIndex + 1) console.log(buildRange);
: verticesOrder;
const buildResults: boolean[] = []; const buildResults: boolean[] = [];
for (let vertexId of buildRange) { await buildRange.reduce(async (previousPromise, idArray) => {
await previousPromise;
return Promise.all(
idArray.map(async (vertexId) => {
try { try {
const buildResponse = await postBuildVertex(flow, vertexId); const buildResponse = await postBuildVertex(flow, vertexId);
const buildData = buildResponse.data; const buildData = buildResponse.data;
if (onProgressUpdate) { if (onProgressUpdate) {
const progress = const progress =
verticesOrder.indexOf(vertexId) / verticesOrder.length; verticesOrder
.map((ids) => ids.indexOf(vertexId))
.filter((index) => index !== -1)[0] /
verticesOrder.flat().length;
onProgressUpdate(progress); onProgressUpdate(progress);
} }
if (onBuildUpdate) { if (onBuildUpdate) {
@ -56,30 +64,31 @@ export async function buildVertices({
onBuildUpdate({ data, id: buildData.id }); onBuildUpdate({ data, id: buildData.id });
} }
buildResults.push(buildData.valid); buildResults.push(buildData.valid);
} catch (error: any) { } catch (error) {
if (onBuildError) { if (onBuildError) {
console.log(error); console.log(error);
onBuildError("Error Building Component", [ onBuildError(
(error as AxiosError<any>).response?.data?.detail ?? "Error Building Component",
"Unknown Error", [(error as AxiosError<any>).response?.data?.detail ?? "Unknown Error"]
]); );
}
} }
} }
})
);
}, Promise.resolve());
// Callback for when all vertices have been built // Callback for when all vertices have been built
if (onBuildComplete) { if (onBuildComplete) {
const allNodesValid = buildResults.every((result) => result); const allNodesValid = buildResults.every((result) => result);
onBuildComplete(allNodesValid); onBuildComplete(allNodesValid);
} }
} catch (error: any) { } catch (error) {
// Callback for handling errors // Callback for handling errors
if (onBuildError) { if (onBuildError) {
if (onBuildError) { onBuildError(
onBuildError("Error Building Component", [ "Error Building Component",
(error as AxiosError<any>).response?.data?.detail ?? "Unknown Error", [(error as AxiosError<any>).response?.data?.detail ?? "Unknown Error"]
]); );
}
} }
} }
} }