chore(modules-sdk): parallel migrations (#13898)
This commit is contained in:
committed by
GitHub
parent
fffc1be1e7
commit
13d7d15be5
31
packages/core/utils/src/common/execute-with-concurrency.ts
Normal file
31
packages/core/utils/src/common/execute-with-concurrency.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Execute functions with a concurrency limit
|
||||
* @param functions Array of functions to execute in parallel
|
||||
* @param concurrency Maximum number of concurrent executions
|
||||
*/
|
||||
export async function executeWithConcurrency<T>(
|
||||
functions: (() => Promise<T>)[],
|
||||
concurrency: number
|
||||
): Promise<PromiseSettledResult<Awaited<T>>[]> {
|
||||
const results: PromiseSettledResult<Awaited<T>>[] = new Array(
|
||||
functions.length
|
||||
)
|
||||
let currentIndex = 0
|
||||
|
||||
const executeNext = async (): Promise<void> => {
|
||||
while (currentIndex < functions.length) {
|
||||
const index = currentIndex++
|
||||
const result = await Promise.allSettled([functions[index]()])
|
||||
results[index] = result[0]
|
||||
}
|
||||
}
|
||||
|
||||
const workers: Promise<void>[] = []
|
||||
for (let i = 0; i < concurrency; i++) {
|
||||
workers.push(executeNext())
|
||||
}
|
||||
|
||||
await Promise.all(workers)
|
||||
|
||||
return results
|
||||
}
|
||||
@@ -19,6 +19,7 @@ export * from "./define-file-config"
|
||||
export * from "./dynamic-import"
|
||||
export * from "./env-editor"
|
||||
export * from "./errors"
|
||||
export * from "./execute-with-concurrency"
|
||||
export * from "./file-system"
|
||||
export * from "./filter-object-by-keys"
|
||||
export * from "./filter-operator-map"
|
||||
|
||||
Reference in New Issue
Block a user