From 53c0706a3aeadf99c24724d1563a96e340c789fb Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Fri, 9 May 2025 17:06:41 +0530 Subject: [PATCH] feat: implement runSequentially utility for sequential function execution --- frontend/src/data/document.js | 75 +++++++++++++++++------------------ frontend/src/utils/index.js | 6 +++ 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/frontend/src/data/document.js b/frontend/src/data/document.js index 1d21774a..18bff8ed 100644 --- a/frontend/src/data/document.js +++ b/frontend/src/data/document.js @@ -1,5 +1,5 @@ import { getScript } from '@/data/script' -import { createToast } from '@/utils' +import { createToast, runSequentially } from '@/utils' import { createDocumentResource } from 'frappe-ui' const documentsCache = {} @@ -52,43 +52,40 @@ export function useDocument(doctype, docname) { const controllers = getControllers() if (!controllers.length) return - await Promise.all( - controllers.map(async (c) => { - await c.refresh() - }), - ) + const tasks = controllers.map((c) => async () => await c.refresh()) + await runSequentially(tasks) } async function triggerOnChange(fieldname, row) { const controllers = getControllers(row) if (!controllers.length) return - await Promise.all( - controllers.map(async (c) => { - if (row) { - c.currentRowIdx = row.idx - c.value = row[fieldname] - c.oldValue = getOldValue(fieldname, row) - } else { - c.value = documentsCache[doctype][docname].doc[fieldname] - c.oldValue = getOldValue(fieldname) - } - await c[fieldname]?.() - }), - ) + const tasks = controllers.map((c) => async () => { + if (row) { + c.currentRowIdx = row.idx + c.value = row[fieldname] + c.oldValue = getOldValue(fieldname, row) + } else { + c.value = documentsCache[doctype][docname].doc[fieldname] + c.oldValue = getOldValue(fieldname) + } + await c[fieldname]?.() + }) + + await runSequentially(tasks) } async function triggerOnRowAdd(row) { const controllers = getControllers(row) if (!controllers.length) return - await Promise.all( - controllers.map(async (c) => { - c.currentRowIdx = row.idx - c.value = row - await c[row.parentfield + '_add']?.() - }), - ) + const tasks = controllers.map((c) => async () => { + c.currentRowIdx = row.idx + c.value = row + await c[row.parentfield + '_add']?.() + }) + + await runSequentially(tasks) } async function triggerOnRowRemove(selectedRows, rows) { @@ -96,21 +93,21 @@ export function useDocument(doctype, docname) { const controllers = getControllers(rows[0]) if (!controllers.length) return - await Promise.all( - controllers.map(async (c) => { - if (selectedRows.size === 1) { - const selectedRow = Array.from(selectedRows)[0] - c.currentRowIdx = rows.find((r) => r.name === selectedRow).idx - } else { - delete c.currentRowIdx - } + const tasks = controllers.map((c) => async () => { + if (selectedRows.size === 1) { + const selectedRow = Array.from(selectedRows)[0] + c.currentRowIdx = rows.find((r) => r.name === selectedRow).idx + } else { + delete c.currentRowIdx + } - c.selectedRows = Array.from(selectedRows) - c.rows = rows + c.selectedRows = Array.from(selectedRows) + c.rows = rows - await c[rows[0].parentfield + '_remove']?.() - }), - ) + await c[rows[0].parentfield + '_remove']?.() + }) + + await runSequentially(tasks) } function getOldValue(fieldname, row) { diff --git a/frontend/src/utils/index.js b/frontend/src/utils/index.js index 47c3e590..22d2d48c 100644 --- a/frontend/src/utils/index.js +++ b/frontend/src/utils/index.js @@ -349,3 +349,9 @@ export function getRandom(len = 4) { return text } + +export function runSequentially(functions) { + return functions.reduce((promise, fn) => { + return promise.then(() => fn()) + }, Promise.resolve()) +}