Skip to content

Commit

Permalink
feat(tasks): create pipes passed functions and checks for cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 8, 2021
1 parent f3cf37b commit f28c215
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/definitions/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Empty, Members, UnaryFn } from 'type-core';
import { Members } from 'type-core';
import { Writable, Readable } from 'stream';

/**
Expand All @@ -13,7 +13,6 @@ export declare namespace Task {
export interface Record {
[key: string]: Task | Record;
}
export type Create = UnaryFn<Context, Task | Empty | Promise<Task | Empty>>;
}

/**
Expand Down
176 changes: 169 additions & 7 deletions src/tasks/creation/create.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,177 @@
import { Task, Context } from '../../definitions';
import { Empty, MaybePromise, UnaryFn } from 'type-core';
import { Context, Task } from '../../definitions';
import { isCancelled } from '../../utils/is-cancelled';
import { run } from '../../utils/run';

/**
* Takes an optionally *Task* returning function
* and turns the function itself and the returned
* *Task* into a single sequential *Task*.
* A task creation pipe of unary functions.
* All functions will compose the task itself,
* with a check for cancellation before running each.
* The first function takes `Context` as an argument.
* The last function can optionally return a `Task`,
* which will also be run.
* @returns Task
*/
export function create(fn: Task.Create): Task.Async {
export const create = pipe as Create;

function pipe(...fns: Array<UnaryFn<any, MaybePromise<any>>>): Task.Async {
return async (ctx: Context): Promise<void> => {
const task = await fn(ctx);
if (task) await run(task, ctx);
let value: any = ctx;
for (const fn of fns) {
if (await isCancelled(ctx)) break;
value = await fn(value);
}
if (value) await run(value, ctx);
};
}

export interface Create {
(f1: UnaryFn<Context, MaybePromise<Task | Empty>>): Task.Async;
<T1>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6, T7>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<T7>>,
f8: UnaryFn<T7, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6, T7, T8>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<T7>>,
f8: UnaryFn<T7, MaybePromise<T8>>,
f9: UnaryFn<T8, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<T7>>,
f8: UnaryFn<T7, MaybePromise<T8>>,
f9: UnaryFn<T8, MaybePromise<T9>>,
f10: UnaryFn<T9, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<T7>>,
f8: UnaryFn<T7, MaybePromise<T8>>,
f9: UnaryFn<T8, MaybePromise<T9>>,
f10: UnaryFn<T9, MaybePromise<T10>>,
f11: UnaryFn<T10, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<T7>>,
f8: UnaryFn<T7, MaybePromise<T8>>,
f9: UnaryFn<T8, MaybePromise<T9>>,
f10: UnaryFn<T9, MaybePromise<T10>>,
f11: UnaryFn<T10, MaybePromise<T11>>,
f12: UnaryFn<T11, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<T7>>,
f8: UnaryFn<T7, MaybePromise<T8>>,
f9: UnaryFn<T8, MaybePromise<T9>>,
f10: UnaryFn<T9, MaybePromise<T10>>,
f11: UnaryFn<T10, MaybePromise<T11>>,
f12: UnaryFn<T11, MaybePromise<T12>>,
f13: UnaryFn<T12, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<T7>>,
f8: UnaryFn<T7, MaybePromise<T8>>,
f9: UnaryFn<T8, MaybePromise<T9>>,
f10: UnaryFn<T9, MaybePromise<T10>>,
f11: UnaryFn<T10, MaybePromise<T11>>,
f12: UnaryFn<T11, MaybePromise<T12>>,
f13: UnaryFn<T12, MaybePromise<T13>>,
f14: UnaryFn<T13, MaybePromise<Task | Empty>>
): Task.Async;
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(
f1: UnaryFn<Context, MaybePromise<T1>>,
f2: UnaryFn<T1, MaybePromise<T2>>,
f3: UnaryFn<T2, MaybePromise<T3>>,
f4: UnaryFn<T3, MaybePromise<T4>>,
f5: UnaryFn<T4, MaybePromise<T5>>,
f6: UnaryFn<T5, MaybePromise<T6>>,
f7: UnaryFn<T6, MaybePromise<T7>>,
f8: UnaryFn<T7, MaybePromise<T8>>,
f9: UnaryFn<T8, MaybePromise<T9>>,
f10: UnaryFn<T9, MaybePromise<T10>>,
f11: UnaryFn<T10, MaybePromise<T11>>,
f12: UnaryFn<T11, MaybePromise<T12>>,
f13: UnaryFn<T12, MaybePromise<T13>>,
f14: UnaryFn<T13, MaybePromise<T14>>,
f15: UnaryFn<T14, MaybePromise<Task | Empty>>
): Task.Async;
}

0 comments on commit f28c215

Please sign in to comment.