UNPKG

@moostjs/event-wf

Version:
147 lines (143 loc) 4.79 kB
import { WooksWf, createWfApp, useWFContext, useWfState } from "@wooksjs/event-wf"; import { Resolve, defineMoostEventHandler, getMoostInfact, getMoostMate, setControllerContext } from "moost"; import { useEventId } from "@wooksjs/event-core"; import { StepRetriableError } from "@prostojs/wf"; //#region packages/event-wf/src/meta-types.ts function getWfMate() { return getMoostMate(); } //#endregion //#region packages/event-wf/src/decorators/wf.decorator.ts function Step(path) { return getWfMate().decorate("handlers", { path, type: "WF_STEP" }, true); } function Workflow(path) { return getWfMate().decorate("handlers", { path, type: "WF_FLOW" }, true); } function WorkflowSchema(schema) { return getWfMate().decorate("wfSchema", schema); } const WorkflowParam = (name) => { switch (name) { case "state": return Resolve(() => useWfState(), "Workflow-State"); case "resume": return Resolve(() => useWfState().resume, "Workflow-Resume"); case "indexes": return Resolve(() => useWfState().indexes, "Workflow-Indexes"); case "schemaId": return Resolve(() => useWfState().schemaId, "Workflow-SchemaId"); case "stepId": return Resolve(() => useWfState().stepId(), "Workflow-StepId"); case "context": return Resolve(() => useWfState().ctx(), "Workflow-Context"); case "input": return Resolve(() => useWfState().input(), "Workflow-Input"); } }; //#endregion //#region packages/event-wf/src/event-wf.ts function _define_property(obj, key, value) { if (key in obj) Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true }); else obj[key] = value; return obj; } const LOGGER_TITLE = "moost-wf"; const CONTEXT_TYPE = "WF"; var MoostWf = class { async onNotFound() { return defineMoostEventHandler({ loggerTitle: LOGGER_TITLE, getIterceptorHandler: () => this.moost?.getGlobalInterceptorHandler(), getControllerInstance: () => this.moost, callControllerMethod: () => new Error("WF Handler not found"), logErrors: this.debug, targetPath: "", handlerType: "__SYSTEM__" })(); } onInit(moost) { this.moost = moost; this.toInit.forEach((fn) => { fn(); }); } getWfApp() { return this.wfApp; } attachSpy(fn) { return this.wfApp.attachSpy(fn); } detachSpy(fn) { this.wfApp.detachSpy(fn); } start(schemaId, initialContext, input) { return this.wfApp.start(schemaId, initialContext, input, () => {}, () => { const scopeId = useEventId().getId(); getMoostInfact().unregisterScope(scopeId); }); } resume(state, input) { return this.wfApp.resume(state, input, () => {}, () => { const scopeId = useEventId().getId(); getMoostInfact().unregisterScope(scopeId); }); } bindHandler(opts) { let fn; for (const handler of opts.handlers) { if (!["WF_STEP", "WF_FLOW"].includes(handler.type)) continue; const schemaId = handler.path; const path = typeof schemaId === "string" ? schemaId : typeof opts.method === "string" ? opts.method : ""; const targetPath = `${`${opts.prefix || ""}/${path}`.replace(/\/\/+/g, "/")}${path.endsWith("//") ? "/" : ""}`; fn = defineMoostEventHandler({ contextType: CONTEXT_TYPE, loggerTitle: LOGGER_TITLE, getIterceptorHandler: opts.getIterceptorHandler, getControllerInstance: opts.getInstance, controllerMethod: opts.method, resolveArgs: opts.resolveArgs, manualUnscope: true, targetPath, handlerType: handler.type }); if (handler.type === "WF_STEP") { this.wfApp.step(targetPath, { handler: fn }); opts.logHandler(`${"\x1B[36m"}(${handler.type})${"\x1B[32m"}${targetPath}`); } else { const mate = getWfMate(); let wfSchema = mate.read(opts.fakeInstance, opts.method)?.wfSchema; if (!wfSchema) wfSchema = mate.read(opts.fakeInstance)?.wfSchema; const _fn = async () => { setControllerContext(this.moost, "bindHandler", targetPath); return fn(); }; this.toInit.push(() => { this.wfApp.flow(targetPath, wfSchema || [], opts.prefix === "/" ? "" : opts.prefix, _fn); opts.logHandler(`${"\x1B[36m"}(${handler.type})${"\x1B[32m"}${targetPath}`); }); } } } constructor(opts, debug) { _define_property(this, "opts", void 0); _define_property(this, "debug", void 0); _define_property(this, "name", void 0); _define_property(this, "wfApp", void 0); _define_property(this, "moost", void 0); _define_property(this, "toInit", void 0); this.opts = opts; this.debug = debug; this.name = "workflow"; this.toInit = []; if (opts && opts instanceof WooksWf) this.wfApp = opts; else if (opts) this.wfApp = createWfApp(opts); else this.wfApp = createWfApp(); } }; //#endregion export { MoostWf, Step, StepRetriableError, Workflow, WorkflowParam, WorkflowSchema, useWFContext, useWfState };