From f28c215e06de7e10b3356d177c13fe124ebb43ca Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Thu, 8 Apr 2021 12:52:18 +0200 Subject: [PATCH] feat(tasks): create pipes passed functions and checks for cancellation --- src/definitions/tasks.ts | 3 +- src/tasks/creation/create.ts | 176 +++++++++++++++++++++++++++++++++-- 2 files changed, 170 insertions(+), 9 deletions(-) diff --git a/src/definitions/tasks.ts b/src/definitions/tasks.ts index 8c20e78..eefa92f 100644 --- a/src/definitions/tasks.ts +++ b/src/definitions/tasks.ts @@ -1,4 +1,4 @@ -import { Empty, Members, UnaryFn } from 'type-core'; +import { Members } from 'type-core'; import { Writable, Readable } from 'stream'; /** @@ -13,7 +13,6 @@ export declare namespace Task { export interface Record { [key: string]: Task | Record; } - export type Create = UnaryFn>; } /** diff --git a/src/tasks/creation/create.ts b/src/tasks/creation/create.ts index 36bfb3a..6300813 100644 --- a/src/tasks/creation/create.ts +++ b/src/tasks/creation/create.ts @@ -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>>): Task.Async { return async (ctx: Context): Promise => { - 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>): 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; +}