-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,74 @@ | ||
import async from 'async' | ||
|
||
type ExecFn<T, R> = (task: T) => Promise<R> | ||
|
||
type ExecResult<T, R> = { | ||
task: T | ||
result?: R | ||
error?: Error | ||
} | ||
|
||
function reflect<T, R>(fn: ExecFn<T, R>) { | ||
return async (task: T): Promise<ExecResult<T, R>> => { | ||
type ParrallelResult<T, R> = { | ||
successes: ExecResult<T, R>[] | ||
errors: ExecResult<T, R>[] | ||
} | ||
|
||
type ParrallelActions<T, R> = { | ||
stop: () => Promise<void> | ||
finished: () => Promise<ParrallelResult<T, R>> | ||
} | ||
|
||
export function parallel<T, R>( | ||
tasks: T[], | ||
limit: number, | ||
fn: ExecFn<T, R>, | ||
processKill = false | ||
): ParrallelActions<T, R> { | ||
let errors: ExecResult<T, R>[] = [] | ||
let successes: ExecResult<T, R>[] = [] | ||
|
||
const queue = async.queue(async (task: T) => { | ||
try { | ||
const result = await fn(task) | ||
return { | ||
successes = successes.concat({ | ||
task, | ||
result, | ||
} | ||
}) | ||
} catch (error) { | ||
return { | ||
errors = errors.concat({ | ||
task, | ||
error, | ||
} | ||
}) | ||
} | ||
}, limit) | ||
|
||
queue.push(tasks) | ||
|
||
async function stop() { | ||
await queue.kill() | ||
await queue.drain() | ||
} | ||
|
||
if (processKill) { | ||
process.on('SIGINT', function () { | ||
console.log('SIGINT, stopping queue') | ||
stop().then(() => { | ||
console.log('queue stopped, killing process') | ||
process.exit() | ||
}) | ||
}) | ||
} | ||
|
||
async function finished(): Promise<ParrallelResult<T, R>> { | ||
await queue.drain() | ||
return { | ||
errors, | ||
successes, | ||
} | ||
} | ||
} | ||
|
||
export async function parallel<T, R>( | ||
tasks: T[], | ||
limit: number, | ||
fn: ExecFn<T, R> | ||
) { | ||
const results = await async.mapLimit(tasks, limit, reflect(fn)) | ||
const errors = results.reduce( | ||
(acc, curr) => (curr.error ? acc.concat(curr) : acc), | ||
[] | ||
) | ||
const successes = results.reduce( | ||
(acc, curr) => (curr.result ? acc.concat(curr) : acc), | ||
[] | ||
) | ||
return { | ||
errors, | ||
successes, | ||
stop, | ||
finished, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters