Skip to content

Commit

Permalink
feat(tasks): adds parallel transform
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Feb 17, 2021
1 parent e9b12d0 commit 689c555
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tasks/transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './context';
export * from './parallel';
export * from './select';
export * from './series';
40 changes: 40 additions & 0 deletions src/tasks/transform/parallel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Task, Context } from '../../definitions';
import { run } from '../consume/run';
import { log } from '../create/log';
import { Empty, NullaryFn } from 'type-core';
import { into } from 'pipettes';

export function parallel(...tasks: Array<Task | Empty>): Task.Async {
return async (ctx: Context): Promise<void> => {
into(ctx, log('debug', 'Run tasks in parallel'));

const cbs: NullaryFn[] = [];
function cancel(): void {
while (cbs.length) {
const cb = cbs.shift();
if (cb) cb();
}
}

ctx.cancellation.finally(() => cancel());

try {
await Promise.all(
tasks.map((task, i) => {
return task
? run(task, {
...ctx,
route: ctx.route.concat(i),
cancellation: new Promise((resolve) => {
cbs.push(resolve);
})
})
: Promise.resolve();
})
);
} catch (err) {
cancel();
throw err;
}
};
}

0 comments on commit 689c555

Please sign in to comment.