Revert unnecessary change.
This commit is contained in:
parent
aecd4f9e4d
commit
ce105d8f4e
|
@ -339,7 +339,7 @@ export class GoogleSheetsIntegration implements DatasourcePlus {
|
|||
const tables: Record<string, Table> = {}
|
||||
let errors: Record<string, string> = {}
|
||||
|
||||
await utils.parallelForEach(
|
||||
await utils.parallelForeach(
|
||||
sheets,
|
||||
async sheet => {
|
||||
try {
|
||||
|
@ -367,7 +367,7 @@ export class GoogleSheetsIntegration implements DatasourcePlus {
|
|||
throw err
|
||||
}
|
||||
},
|
||||
{ maxConcurrency: 2 }
|
||||
10
|
||||
)
|
||||
|
||||
for (const sheet of sheets) {
|
||||
|
|
|
@ -7,34 +7,43 @@ export function unreachable(
|
|||
throw new Error(message)
|
||||
}
|
||||
|
||||
interface PromiseWithId<T> {
|
||||
promise: Promise<T>
|
||||
id: number
|
||||
}
|
||||
|
||||
export async function parallelForEach<T>(
|
||||
export async function parallelForeach<T>(
|
||||
items: T[],
|
||||
task: (item: T) => Promise<void>,
|
||||
opts?: { maxConcurrency?: number }
|
||||
maxConcurrency: number
|
||||
): Promise<void> {
|
||||
const { maxConcurrency = 10 } = opts || {}
|
||||
let next = 0
|
||||
let inProgress: PromiseWithId<number>[] = []
|
||||
while (next < items.length) {
|
||||
if (inProgress.length === maxConcurrency) {
|
||||
const finished = await Promise.race(inProgress.map(t => t.promise))
|
||||
inProgress = inProgress.filter(task => task.id !== finished)
|
||||
}
|
||||
const promises: Promise<void>[] = []
|
||||
let index = 0
|
||||
|
||||
const promise = async (next: number) => {
|
||||
await task(items[next])
|
||||
return next
|
||||
const processItem = async (item: T) => {
|
||||
try {
|
||||
await task(item)
|
||||
} finally {
|
||||
processNext()
|
||||
}
|
||||
|
||||
inProgress.push({ promise: promise(next), id: next })
|
||||
next++
|
||||
}
|
||||
await Promise.all(inProgress.map(t => t.promise))
|
||||
|
||||
const processNext = () => {
|
||||
if (index >= items.length) {
|
||||
// No more items to process
|
||||
return
|
||||
}
|
||||
|
||||
const item = items[index]
|
||||
index++
|
||||
|
||||
const promise = processItem(item)
|
||||
promises.push(promise)
|
||||
|
||||
if (promises.length >= maxConcurrency) {
|
||||
Promise.race(promises).then(processNext)
|
||||
} else {
|
||||
processNext()
|
||||
}
|
||||
}
|
||||
processNext()
|
||||
|
||||
await Promise.all(promises)
|
||||
}
|
||||
|
||||
export function filterValueToLabel() {
|
||||
|
|
Loading…
Reference in New Issue