@prostojs/wf
Version:
Generic workflow framework
85 lines (84 loc) • 4.35 kB
text/typescript
import { a as TFlowPaused, c as TStepHandler, d as TWorkflowControl, f as TWorkflowItem, h as TWorkflowStepSchemaObj, i as TFlowOutput, l as TStepOutput, m as TWorkflowStepConditionFn, n as TFlowFailed, o as TFlowSpyData, p as TWorkflowSchema, r as TFlowFinished, s as TFlowState, t as StepRetriableError, u as TSubWorkflowSchemaObj } from "./types-Ca8uVF2-.cjs";
import { FtringsPool } from "@prostojs/ftring";
//#region src/step.d.ts
interface StepOptions<I> {
globals?: Record<string, unknown>;
input?: I;
}
declare class Step<T, I, IR> {
readonly id: string;
protected handler: string | TStepHandler<T, I, IR>;
protected opts: StepOptions<I>;
constructor(id: string, handler: string | TStepHandler<T, I, IR>, opts?: StepOptions<I>);
protected _handler?: TStepHandler<T, I, IR>;
getGlobals(ctx: T, input: I): Record<string, unknown>;
handle(ctx: T, input: I): TStepOutput<IR> | StepRetriableError<IR> | Promise<TStepOutput<IR> | StepRetriableError<IR>>;
}
declare function createStep<T = any, I = any, IR = any>(id: string, opts: {
input?: I;
handler: string | TStepHandler<T, I, IR>;
}): Step<T, I, IR>;
//#endregion
//#region src/spy.d.ts
type TWorkflowSpy<T, _I, IR> = (event: string, eventOutput: string | undefined | {
fn: string | TWorkflowStepConditionFn<T>;
result: boolean;
}, flowOutput: TFlowSpyData<T, IR>, ms?: number) => void;
//#endregion
//#region src/workflow.d.ts
interface TFlowMutable<T, IR> {
state: TFlowState<T>;
finished: boolean;
stepId: string;
inputRequired?: IR;
interrupt?: boolean;
break?: boolean;
error?: Error;
expires?: number;
errorList?: unknown;
}
declare class Workflow<T, IR> {
protected mappedSteps: Record<string, Step<T, any, IR>>;
protected schemas: Record<string, TWorkflowSchema<T>>;
protected schemaPrefix: Record<string, string>;
protected spies: TWorkflowSpy<T, any, IR>[];
protected fnPool: FtringsPool<Promise<boolean>, unknown>;
constructor(steps: Step<T, any, IR>[]);
protected resolveStep(stepId: string): Step<T, any, IR>;
attachSpy<I = any>(fn: TWorkflowSpy<T, I, IR>): () => void;
detachSpy<I = any>(fn: TWorkflowSpy<T, I, IR>): void;
addStep<I>(step: Step<T, I, IR>): void;
protected emit(spy: TWorkflowSpy<T, any, IR> | undefined, event: string, eventOutput: string | undefined | {
fn: string | TWorkflowStepConditionFn<T>;
result: boolean;
}, flowOutput: TFlowSpyData<T, IR>, ms?: number): void;
protected validateSchema(schemaId: string, item: TWorkflowItem<T>, prefix?: string): void;
register(id: string, schema: TWorkflowSchema<T>, prefix?: string): void;
start<I>(schemaId: string, initialContext: T, input?: I, spy?: TWorkflowSpy<T, I, IR>): Promise<TFlowOutput<T, I, IR>>;
protected callConditionFn(spy: TWorkflowSpy<T, any, IR> | undefined, event: string, fn: string | TWorkflowStepConditionFn<T>, result: TFlowMutable<T, IR>): Promise<boolean>;
protected loopInto<I>(event: string, opts: {
schemaId: string;
schema: TWorkflowSchema<T>;
context: T;
input?: I;
indexes?: number[];
level?: number;
spy?: TWorkflowSpy<T, I, IR>;
}): Promise<TFlowMutable<T, IR>>;
protected prefixStepId(id: string, prefix?: string): string;
protected getItemStepId(item: TWorkflowItem<T>, prefix?: string): string | undefined;
protected normalizeWorkflowItem(item: TWorkflowItem<T>, prefix?: string): {
stepId?: string;
input?: unknown;
steps?: TWorkflowSchema<T>;
conditionFn?: string | TWorkflowStepConditionFn<T>;
continueFn?: string | TWorkflowStepConditionFn<T>;
breakFn?: string | TWorkflowStepConditionFn<T>;
whileFn?: string | TWorkflowStepConditionFn<T>;
};
protected convertToOutput(result: TFlowMutable<T, IR>, spy?: TWorkflowSpy<T, any, IR>): TFlowOutput<T, unknown, IR>;
protected resumeLoop<I>(state: TFlowState<T>, input: I, spy?: TWorkflowSpy<T, I, IR>): Promise<TFlowMutable<T, IR>>;
resume<I>(state: TFlowState<T>, input: I, spy?: TWorkflowSpy<T, I, IR>): Promise<TFlowOutput<T, unknown, IR>>;
}
//#endregion
export { Step, StepOptions, StepRetriableError, TFlowFailed, TFlowFinished, TFlowOutput, TFlowPaused, TFlowSpyData, TFlowState, TStepHandler, TStepOutput, TSubWorkflowSchemaObj, TWorkflowControl, TWorkflowItem, TWorkflowSchema, TWorkflowSpy, TWorkflowStepConditionFn, TWorkflowStepSchemaObj, Workflow, createStep };