UNPKG

arvo-event-handler

Version:

Type-safe event handler system with versioning, telemetry, and contract validation for distributed Arvo event-driven architectures, featuring routing and multi-handler support.

63 lines (62 loc) 3.92 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.createArvoResumable = void 0; var _1 = require("."); var utils_1 = require("../ArvoMachine/utils"); var uuid_1 = require("uuid"); /** * Factory function for creating ArvoResumable orchestrator instances * * Creates a new ArvoResumable orchestrator with type safety and sensible defaults. * ArvoResumable provides handler-based workflow orchestration with explicit context management, * contract validation, and distributed locking capabilities. * * @param param - Configuration object for the orchestrator * @param param.types - Optional type hints for better TypeScript inference * @param param.types.context - Partial type hint for the workflow context structure (not used at runtime) * @param param.contracts - Contract definitions for the orchestrator and its services * @param param.contracts.self - The orchestrator's own contract defining accepted events and emitted events * @param param.contracts.services - Record of service contracts this orchestrator can invoke, keyed by service name * @param param.memory - Generic memory interface for state persistence, locking, and retrieval operations * @param param.handler - Versioned orchestration logic handlers mapped by semantic version * @param param.executionunits - Resource allocation cost for this orchestrator's execution (default: 0) * @param param.requiresResourceLocking - Enable distributed locking for concurrent safety (default: auto-determined by service count) * @param param.systemErrorDomain - The domain override of the system error events. (default [event.domain, self.contract.domain, null]) * * @returns A new ArvoResumable orchestrator instance configured with the provided parameters * * @throws {Error} Service contracts have duplicate URIs - Multiple versions of the same contract are not allowed * @throws {Error} Circular dependency detected - Self contract is registered as a service, creating execution loops */ var createArvoResumable = function (param) { var _a; var _b, _c; var __areServiceContractsUnique = (0, utils_1.areServiceContractsUnique)(param.contracts.services); if (!__areServiceContractsUnique.result) { throw new Error("The service contracts must have unique URIs. Multiple versions of the same contract are not allow. The contracts '".concat(__areServiceContractsUnique.keys[0], "' and '").concat(__areServiceContractsUnique.keys[1], "' have the same URI '").concat(__areServiceContractsUnique.contractUri, "'")); } var __checkIfSelfIsAService = (0, utils_1.areServiceContractsUnique)(__assign(__assign({}, param.contracts.services), (_a = {}, _a[(0, uuid_1.v4)()] = param.contracts.self, _a))); if (!__checkIfSelfIsAService.result) { throw new Error("Circular dependency detected: Machine with URI '".concat(param.contracts.self.uri, "' is registered as service '").concat(__checkIfSelfIsAService.keys[1], "'. Self-referential services create execution loops and are prohibited.")); } return new _1.ArvoResumable({ contracts: param.contracts, memory: param.memory, handler: param.handler, executionunits: (_b = param.executionunits) !== null && _b !== void 0 ? _b : 0, requiresResourceLocking: (_c = param.requiresResourceLocking) !== null && _c !== void 0 ? _c : Object.keys(param.contracts.services).length > 1, systemErrorDomain: param.systemErrorDomain, }); }; exports.createArvoResumable = createArvoResumable;