feat: implement runSequentially utility for sequential function execution
(cherry picked from commit 53c0706a3aeadf99c24724d1563a96e340c789fb)
This commit is contained in:
parent
86f0f4949c
commit
a1a44c1020
@ -1,5 +1,5 @@
|
|||||||
import { getScript } from '@/data/script'
|
import { getScript } from '@/data/script'
|
||||||
import { createToast } from '@/utils'
|
import { createToast, runSequentially } from '@/utils'
|
||||||
import { createDocumentResource } from 'frappe-ui'
|
import { createDocumentResource } from 'frappe-ui'
|
||||||
|
|
||||||
const documentsCache = {}
|
const documentsCache = {}
|
||||||
@ -52,43 +52,40 @@ export function useDocument(doctype, docname) {
|
|||||||
const controllers = getControllers()
|
const controllers = getControllers()
|
||||||
if (!controllers.length) return
|
if (!controllers.length) return
|
||||||
|
|
||||||
await Promise.all(
|
const tasks = controllers.map((c) => async () => await c.refresh())
|
||||||
controllers.map(async (c) => {
|
await runSequentially(tasks)
|
||||||
await c.refresh()
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function triggerOnChange(fieldname, row) {
|
async function triggerOnChange(fieldname, row) {
|
||||||
const controllers = getControllers(row)
|
const controllers = getControllers(row)
|
||||||
if (!controllers.length) return
|
if (!controllers.length) return
|
||||||
|
|
||||||
await Promise.all(
|
const tasks = controllers.map((c) => async () => {
|
||||||
controllers.map(async (c) => {
|
if (row) {
|
||||||
if (row) {
|
c.currentRowIdx = row.idx
|
||||||
c.currentRowIdx = row.idx
|
c.value = row[fieldname]
|
||||||
c.value = row[fieldname]
|
c.oldValue = getOldValue(fieldname, row)
|
||||||
c.oldValue = getOldValue(fieldname, row)
|
} else {
|
||||||
} else {
|
c.value = documentsCache[doctype][docname].doc[fieldname]
|
||||||
c.value = documentsCache[doctype][docname].doc[fieldname]
|
c.oldValue = getOldValue(fieldname)
|
||||||
c.oldValue = getOldValue(fieldname)
|
}
|
||||||
}
|
await c[fieldname]?.()
|
||||||
await c[fieldname]?.()
|
})
|
||||||
}),
|
|
||||||
)
|
await runSequentially(tasks)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function triggerOnRowAdd(row) {
|
async function triggerOnRowAdd(row) {
|
||||||
const controllers = getControllers(row)
|
const controllers = getControllers(row)
|
||||||
if (!controllers.length) return
|
if (!controllers.length) return
|
||||||
|
|
||||||
await Promise.all(
|
const tasks = controllers.map((c) => async () => {
|
||||||
controllers.map(async (c) => {
|
c.currentRowIdx = row.idx
|
||||||
c.currentRowIdx = row.idx
|
c.value = row
|
||||||
c.value = row
|
await c[row.parentfield + '_add']?.()
|
||||||
await c[row.parentfield + '_add']?.()
|
})
|
||||||
}),
|
|
||||||
)
|
await runSequentially(tasks)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function triggerOnRowRemove(selectedRows, rows) {
|
async function triggerOnRowRemove(selectedRows, rows) {
|
||||||
@ -96,21 +93,21 @@ export function useDocument(doctype, docname) {
|
|||||||
const controllers = getControllers(rows[0])
|
const controllers = getControllers(rows[0])
|
||||||
if (!controllers.length) return
|
if (!controllers.length) return
|
||||||
|
|
||||||
await Promise.all(
|
const tasks = controllers.map((c) => async () => {
|
||||||
controllers.map(async (c) => {
|
if (selectedRows.size === 1) {
|
||||||
if (selectedRows.size === 1) {
|
const selectedRow = Array.from(selectedRows)[0]
|
||||||
const selectedRow = Array.from(selectedRows)[0]
|
c.currentRowIdx = rows.find((r) => r.name === selectedRow).idx
|
||||||
c.currentRowIdx = rows.find((r) => r.name === selectedRow).idx
|
} else {
|
||||||
} else {
|
delete c.currentRowIdx
|
||||||
delete c.currentRowIdx
|
}
|
||||||
}
|
|
||||||
|
|
||||||
c.selectedRows = Array.from(selectedRows)
|
c.selectedRows = Array.from(selectedRows)
|
||||||
c.rows = rows
|
c.rows = rows
|
||||||
|
|
||||||
await c[rows[0].parentfield + '_remove']?.()
|
await c[rows[0].parentfield + '_remove']?.()
|
||||||
}),
|
})
|
||||||
)
|
|
||||||
|
await runSequentially(tasks)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getOldValue(fieldname, row) {
|
function getOldValue(fieldname, row) {
|
||||||
|
|||||||
@ -349,3 +349,9 @@ export function getRandom(len = 4) {
|
|||||||
|
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function runSequentially(functions) {
|
||||||
|
return functions.reduce((promise, fn) => {
|
||||||
|
return promise.then(() => fn())
|
||||||
|
}, Promise.resolve())
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user