Skip to content

Commit

Permalink
feat: allow combine task and recreate util to take a Task.Record retu…
Browse files Browse the repository at this point in the history
…rning function
  • Loading branch information
rafamel committed Apr 4, 2021
1 parent cc7576d commit 2b409da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/tasks/aggregate/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { run } from '../../utils/run';
import { recreate } from '../../utils/recreate';
import { context } from '../transform/context';
import { series } from './series';
import { NullaryFn } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';

Expand Down Expand Up @@ -35,7 +36,7 @@ export interface CombineOptions {
* @returns Task
*/
export function combine(
tasks: Task.Record,
tasks: Task.Record | NullaryFn<Task.Record>,
options?: CombineOptions
): Task.Async {
return async (ctx: Context): Promise<void> => {
Expand All @@ -45,8 +46,7 @@ export function combine(
);

return into(
tasks,
recreate.bind(null, (task, route) => {
recreate(tasks, (task, route) => {
return context(
(ctx) => ({ ...ctx, route: ctx.route.concat(route) }),
task
Expand Down
46 changes: 42 additions & 4 deletions src/utils/recreate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
import { Task } from '../definitions';
import { Empty } from 'type-core';
import { context } from '../tasks/transform/context';
import { announce } from '../tasks/transform/announce';
import { Empty, NullaryFn, TypeGuard } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';

export interface RecreateOptions {
/**
* Fixes current routes for tasks according to their
* current paths, so they won't be dinamically reassigned.
*/
fix?: boolean;
/**
* Prints routes before execution for all tasks.
*/
announce?: boolean;
}

export interface RecreateMap {
(task: Task, route: string[]): Task | Empty;
}

/**
* Maps all tasks in a `Task.Record`.
*/
export function recreate(
map: (task: Task, route: string[]) => Task | Empty,
tasks: Task.Record
tasks: Task.Record | NullaryFn<Task.Record>,
options?: RecreateOptions | RecreateMap
): Task.Record {
return recreateHelper([], tasks, map);
const record = TypeGuard.isFunction(tasks) ? tasks() : tasks;

if (TypeGuard.isEmpty(options)) {
return record;
} else if (TypeGuard.isFunction(options)) {
return recreateHelper([], record, options);
} else {
const opts: RecreateOptions = shallow(
{ fix: false, announce: false },
options || undefined
);
return recreateHelper([], record, (task, route) => {
return into(
task,
(task) => (opts.announce ? announce(task) : task),
(task) => (opts.fix ? context({ route }, task) : task)
);
});
}
}

function recreateHelper(
Expand Down

0 comments on commit 2b409da

Please sign in to comment.