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,63 +23,72 @@ 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) => {
try { await previousPromise;
const buildResponse = await postBuildVertex(flow, vertexId); return Promise.all(
const buildData = buildResponse.data; idArray.map(async (vertexId) => {
if (onProgressUpdate) { try {
const progress = const buildResponse = await postBuildVertex(flow, vertexId);
verticesOrder.indexOf(vertexId) / verticesOrder.length; const buildData = buildResponse.data;
onProgressUpdate(progress); if (onProgressUpdate) {
} const progress =
if (onBuildUpdate) { verticesOrder
let data = {}; .map((ids) => ids.indexOf(vertexId))
if (!buildData.valid) { .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) { if (onBuildError) {
onBuildError("Error Building Component", [buildData.params]); console.log(error);
onBuildError(
"Error Building Component",
[(error as AxiosError<any>).response?.data?.detail ?? "Unknown Error"]
);
} }
} }
data[buildData.id] = buildData; })
);
onBuildUpdate({ data, id: buildData.id }); }, Promise.resolve());
}
buildResults.push(buildData.valid);
} catch (error: any) {
if (onBuildError) {
console.log(error);
onBuildError("Error Building Component", [
(error as AxiosError<any>).response?.data?.detail ??
"Unknown Error",
]);
}
}
}
// 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"]
]); );
}
} }
} }
} }