frontend: Add parallel for-each-device helper with CLI output
This commit is contained in:
parent
05a7ee26da
commit
44af21c885
1 changed files with 28 additions and 0 deletions
28
src/frontend/devices.ts
Normal file
28
src/frontend/devices.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import chalk from "chalk"
|
||||
|
||||
export async function forEachDevice<Device>(
|
||||
devices: Device[],
|
||||
parallel: boolean,
|
||||
callback: (device: Device) => Promise<void>,
|
||||
deviceKey: (device: Device) => string = d => d as any as string,
|
||||
) {
|
||||
let jobs = []
|
||||
let isMultiDevice = devices.length > 1
|
||||
for (let device of devices) {
|
||||
if (isMultiDevice) {
|
||||
console.log(`
|
||||
|
||||
${chalk.bold(chalk.blueBright(deviceKey(device)))}
|
||||
`)
|
||||
}
|
||||
|
||||
let job = callback(device)
|
||||
if (parallel) {
|
||||
jobs.push(job)
|
||||
} else {
|
||||
await job
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(jobs)
|
||||
}
|
Loading…
Reference in a new issue