repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

starfx / store
Eric Bower · 04 Mar 24

run.ts

 1import { Callable, Operation, Result, Scope, Task } from "../deps.ts";
 2import { parallel, safe } from "../fx/mod.ts";
 3
 4export function createRun(scope: Scope) {
 5  function run<T>(op: Callable<T>[]): Task<Result<T>[]>;
 6  function run<T>(op: Callable<T>): Task<Result<T>>;
 7  function run<T>(
 8    op: Callable<T> | Callable<T>[],
 9  ): Task<Result<T> | Result<T>[]> {
10    if (Array.isArray(op)) {
11      return scope.run(function* (): Operation<Result<T>[]> {
12        const group = yield* parallel(op);
13        const result = yield* group;
14        return result;
15      });
16    }
17    return scope.run(() => safe(op));
18  }
19
20  return run;
21}