diff --git a/src/tasks/creation/create.ts b/src/tasks/creation/create.ts index 0970b4c..d13a86a 100644 --- a/src/tasks/creation/create.ts +++ b/src/tasks/creation/create.ts @@ -1,177 +1,18 @@ import { Empty, MaybePromise, UnaryFn } from 'type-core'; -import { Context, Task } from '../../definitions'; -import { isCancelled } from '../../utils/is-cancelled'; +import { Task, Context } from '../../definitions'; import { run } from '../../utils/run'; /** - * 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. + * Takes an optionally *Task* returning function + * and turns the function itself and the returned + * *Task* into a single sequential *Task*. * @returns Task */ -export const create = pipe as Create; - -function pipe(...fns: Array>>): Task.Async { +export function create( + fn: UnaryFn> +): Task.Async { return async (ctx: Context): Promise => { - let value: any = ctx; - for (const fn of fns) { - if (await isCancelled(ctx)) break; - value = await fn(value); - } - if (value) await run(ctx, value); + const task = await fn(ctx); + if (task) await run(ctx, task); }; } - -export interface Create { - (f1: UnaryFn>): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn>, - f8: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn>, - f8: UnaryFn>, - f9: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn>, - f8: UnaryFn>, - f9: UnaryFn>, - f10: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn>, - f8: UnaryFn>, - f9: UnaryFn>, - f10: UnaryFn>, - f11: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn>, - f8: UnaryFn>, - f9: UnaryFn>, - f10: UnaryFn>, - f11: UnaryFn>, - f12: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn>, - f8: UnaryFn>, - f9: UnaryFn>, - f10: UnaryFn>, - f11: UnaryFn>, - f12: UnaryFn>, - f13: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn>, - f8: UnaryFn>, - f9: UnaryFn>, - f10: UnaryFn>, - f11: UnaryFn>, - f12: UnaryFn>, - f13: UnaryFn>, - f14: UnaryFn> - ): Task.Async; - ( - f1: UnaryFn>, - f2: UnaryFn>, - f3: UnaryFn>, - f4: UnaryFn>, - f5: UnaryFn>, - f6: UnaryFn>, - f7: UnaryFn>, - f8: UnaryFn>, - f9: UnaryFn>, - f10: UnaryFn>, - f11: UnaryFn>, - f12: UnaryFn>, - f13: UnaryFn>, - f14: UnaryFn>, - f15: UnaryFn> - ): Task.Async; -}