UNPKG

@flowlab/all

Version:

A cool library focusing on handling various flows

106 lines (105 loc) 4.41 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkflowDefinition = void 0; var config_1 = require("../types/config"); var errors_1 = require("../errors"); var WorkflowDefinition = /** @class */ (function () { function WorkflowDefinition(id, name, description) { if (!id) throw new errors_1.ConfigurationError('Workflow definition ID is required.'); this.data = { id: id, name: name, description: description, steps: {}, }; } // --- Chainable Methods for Building --- WorkflowDefinition.prototype.addStep = function (config) { this._addStepInternal(__assign(__assign({}, config), { type: config_1.StepType.TASK })); return this; }; WorkflowDefinition.prototype.addCondition = function (config) { this._addStepInternal(__assign(__assign({}, config), { type: config_1.StepType.CONDITION })); return this; }; WorkflowDefinition.prototype.addParallel = function (config) { // TODO: Add validation for parallel steps structure this._addStepInternal(__assign(__assign({}, config), { type: config_1.StepType.PARALLEL })); return this; }; WorkflowDefinition.prototype.addSubWorkflow = function (config) { this._addStepInternal(__assign(__assign({}, config), { type: config_1.StepType.SUB_WORKFLOW })); return this; }; // TODO: Add methods for EventTrigger, EventListener etc. WorkflowDefinition.prototype.setStartStep = function (stepId) { if (!this.data.steps[stepId]) { throw new errors_1.ConfigurationError("Cannot set start step: Step with ID '".concat(stepId, "' does not exist in definition '").concat(this.data.id, "'.")); } this.data.startStepId = stepId; return this; }; // --- Internal Logic --- WorkflowDefinition.prototype._addStepInternal = function (config) { if (!config.id) throw new errors_1.ConfigurationError('Step ID is required.'); if (this.data.steps[config.id]) throw new errors_1.ConfigurationError("Step with ID '".concat(config.id, "' already exists in definition '").concat(this.data.id, "'.")); // TODO: Add more validation based on step type (e.g., nextStepId presence/absence) this.data.steps[config.id] = config; }; Object.defineProperty(WorkflowDefinition.prototype, "id", { // --- Accessors --- get: function () { return this.data.id; }, enumerable: false, configurable: true }); Object.defineProperty(WorkflowDefinition.prototype, "name", { get: function () { return this.data.name; }, enumerable: false, configurable: true }); Object.defineProperty(WorkflowDefinition.prototype, "description", { get: function () { return this.data.description; }, enumerable: false, configurable: true }); Object.defineProperty(WorkflowDefinition.prototype, "startStepId", { get: function () { return this.data.startStepId; }, enumerable: false, configurable: true }); WorkflowDefinition.prototype.getStep = function (stepId) { return this.data.steps[stepId]; }; WorkflowDefinition.prototype.getSteps = function () { return this.data.steps; }; // Get the raw data for persistence or inspection WorkflowDefinition.prototype.getData = function () { return this.data; }; // TODO: Add a validate() method similar to Root WorkflowDefinition.prototype.validate = function () { if (!this.data.startStepId) { console.error("[Validation Error] Workflow '".concat(this.id, "': Start step not set.")); return false; } // TODO: Check for unreachable steps, cycles (if not allowed), valid nextStepId refs etc. return true; }; return WorkflowDefinition; }()); exports.WorkflowDefinition = WorkflowDefinition;