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.

76 lines (75 loc) 3.59 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createArvoOrchestrator = void 0; var _1 = require("."); var MachineExecutionEngine_1 = require("../MachineExecutionEngine"); var MachineRegistry_1 = require("../MachineRegistry"); var errors_1 = require("../errors"); /** * Creates a new Arvo orchestrator instance with default components. * * Factory function that constructs an orchestrator with standard execution engine * and registry implementations. Validates that all machines share the same source * identifier and have unique versions. * * @param params - Configuration parameters for the orchestrator * @returns Configured ArvoOrchestrator instance ready for event handling * * @throws {Error} When no machines are provided * @throws {ConfigViolation} When machines have different source identifiers * @throws {ConfigViolation} When machines have duplicate versions * * @example * ```typescript * const orchestrator = createArvoOrchestrator({ * memory: new SimpleMachineMemory(), * executionunits: 1, * machines: [userOnboardingMachine, paymentMachine] * }); * * // Process events * const result = await orchestrator.execute(event); * ``` * * @see {@link setupArvoMachine} for creating machine definitions * @see {@link ArvoOrchestrator} for direct instantiation with custom components */ var createArvoOrchestrator = function (_a) { var executionunits = _a.executionunits, memory = _a.memory, machines = _a.machines, systemErrorDomain = _a.systemErrorDomain, spanOptions = _a.spanOptions, _locking = _a.requiresResourceLocking; if (!(machines === null || machines === void 0 ? void 0 : machines.length)) { throw new Error('At least one machine must be provided'); } var registry = new (MachineRegistry_1.MachineRegistry.bind.apply(MachineRegistry_1.MachineRegistry, __spreadArray([void 0], machines, false)))(); var requiresResourceLocking = _locking !== null && _locking !== void 0 ? _locking : machines.some(function (machine) { return machine.requiresResourceLocking; }); var representativeMachine = registry.machines[0]; var lastSeenVersions = []; for (var _i = 0, _b = registry.machines; _i < _b.length; _i++) { var machine = _b[_i]; if (representativeMachine.source !== machine.source) { throw new errors_1.ConfigViolation("All the machines in the orchestrator must have type '".concat(representativeMachine.source, "'")); } if (lastSeenVersions.includes(machine.version)) { throw new errors_1.ConfigViolation("An orchestrator must have unique machine versions. Machine ID:".concat(machine.id, " has duplicate version ").concat(machine.version, ".")); } lastSeenVersions.push(machine.version); } return new _1.ArvoOrchestrator({ executionunits: executionunits, memory: memory, registry: registry, executionEngine: new MachineExecutionEngine_1.MachineExecutionEngine(), requiresResourceLocking: requiresResourceLocking, systemErrorDomain: systemErrorDomain, spanOptions: spanOptions, }); }; exports.createArvoOrchestrator = createArvoOrchestrator;