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.
71 lines (70 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.areServiceContractsUnique = exports.detectParallelStates = void 0;
/**
* Detects if an XState machine configuration contains any parallel states.
* Uses a stack-based approach for efficient traversal of the state hierarchy.
*
* @param config - XState machine configuration
* @returns True if the machine contains at least one parallel state, false otherwise
*
* @example
* const machine = {
* states: {
* processing: {
* type: 'parallel',
* states: {
* upload: { ... },
* scan: { ... }
* }
* }
* }
* }
* const hasParallel = detectParallelStates(machine) // Returns true
*/
var detectParallelStates = function (config) {
if (!(config === null || config === void 0 ? void 0 : config.states)) {
return false;
}
var stack = [config];
while (stack.length) {
var currentConfig = stack.pop();
if (!(currentConfig === null || currentConfig === void 0 ? void 0 : currentConfig.states))
continue;
if (currentConfig.type === 'parallel')
return true;
for (var _i = 0, _a = Object.values(currentConfig.states); _i < _a.length; _i++) {
var state = _a[_i];
stack.push(state);
}
}
return false;
};
exports.detectParallelStates = detectParallelStates;
/**
* Validates that all service contracts in a collection have unique URIs.
*
* Iterates through the provided contracts and checks if any URI appears more than once.
* Multiple versions of the same contract (with the same URI) are not allowed.
*
* @param contracts - A record mapping contract keys to their respective ArvoContract objects
* @returns An object with a boolean result indicating if all contracts are unique, and the error keys if not
*/
var areServiceContractsUnique = function (contracts) {
var uriToKeyMap = {};
for (var _i = 0, _a = Object.entries(contracts); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], contract = _b[1];
if (uriToKeyMap[contract.uri]) {
return {
result: false,
keys: [key, uriToKeyMap[contract.uri]],
contractUri: contract.uri,
};
}
uriToKeyMap[contract.uri] = key;
}
return {
result: true,
};
};
exports.areServiceContractsUnique = areServiceContractsUnique;