@moostjs/event-wf
Version:
193 lines (188 loc) • 6.62 kB
JavaScript
;
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
const __wooksjs_event_wf = __toESM(require("@wooksjs/event-wf"));
const moost = __toESM(require("moost"));
const __wooksjs_event_core = __toESM(require("@wooksjs/event-core"));
const __prostojs_wf = __toESM(require("@prostojs/wf"));
//#region packages/event-wf/src/meta-types.ts
function getWfMate() {
return (0, moost.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 (0, moost.Resolve)(() => (0, __wooksjs_event_wf.useWfState)(), "Workflow-State");
case "resume": return (0, moost.Resolve)(() => (0, __wooksjs_event_wf.useWfState)().resume, "Workflow-Resume");
case "indexes": return (0, moost.Resolve)(() => (0, __wooksjs_event_wf.useWfState)().indexes, "Workflow-Indexes");
case "schemaId": return (0, moost.Resolve)(() => (0, __wooksjs_event_wf.useWfState)().schemaId, "Workflow-SchemaId");
case "stepId": return (0, moost.Resolve)(() => (0, __wooksjs_event_wf.useWfState)().stepId(), "Workflow-StepId");
case "context": return (0, moost.Resolve)(() => (0, __wooksjs_event_wf.useWfState)().ctx(), "Workflow-Context");
case "input": return (0, moost.Resolve)(() => (0, __wooksjs_event_wf.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 (0, moost.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$1) {
this.moost = moost$1;
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 = (0, __wooksjs_event_core.useEventId)().getId();
(0, moost.getMoostInfact)().unregisterScope(scopeId);
});
}
resume(state, input) {
return this.wfApp.resume(state, input, () => {}, () => {
const scopeId = (0, __wooksjs_event_core.useEventId)().getId();
(0, moost.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 = (0, moost.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 () => {
(0, moost.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 __wooksjs_event_wf.WooksWf) this.wfApp = opts;
else if (opts) this.wfApp = (0, __wooksjs_event_wf.createWfApp)(opts);
else this.wfApp = (0, __wooksjs_event_wf.createWfApp)();
}
};
//#endregion
exports.MoostWf = MoostWf
exports.Step = Step
Object.defineProperty(exports, 'StepRetriableError', {
enumerable: true,
get: function () {
return __prostojs_wf.StepRetriableError;
}
});
exports.Workflow = Workflow
exports.WorkflowParam = WorkflowParam
exports.WorkflowSchema = WorkflowSchema
Object.defineProperty(exports, 'useWFContext', {
enumerable: true,
get: function () {
return __wooksjs_event_wf.useWFContext;
}
});
Object.defineProperty(exports, 'useWfState', {
enumerable: true,
get: function () {
return __wooksjs_event_wf.useWfState;
}
});