arvo-event-handler
Version:
A complete set of orthogonal event handler and orchestration primitives for Arvo based applications, featuring declarative state machines (XState), imperative resumables for agentic workflows, contract-based routing, OpenTelemetry observability, and in-me
92 lines (91 loc) • 3.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEventHandlerTelemetryConfig = void 0;
exports.isError = isError;
exports.isNullOrUndefined = isNullOrUndefined;
exports.getValueOrDefault = getValueOrDefault;
exports.coalesce = coalesce;
exports.coalesceOrDefault = coalesceOrDefault;
var api_1 = require("@opentelemetry/api");
function isError(e) {
return e instanceof Error || (typeof e === 'object' && e !== null && 'message' in e);
}
/**
* Checks if the item is null or undefined.
*
* @param item - The value to check.
* @returns True if the item is null or undefined, false otherwise.
*/
function isNullOrUndefined(item) {
return item === null || item === undefined;
}
/**
* Returns the provided value if it's not null or undefined; otherwise, returns the default value.
*
* @template T - The type of the value and default value.
* @param value - The value to check.
* @param defaultValue - The default value to return if the provided value is null or undefined.
* @returns The provided value if it's not null or undefined; otherwise, the default value.
*/
function getValueOrDefault(value, defaultValue) {
return isNullOrUndefined(value) ? defaultValue : value;
}
/**
* Returns the first non-null and non-undefined value from the provided arguments.
* If all arguments are null or undefined, returns undefined.
*
* @template T - The type of the values.
* @param values - The values to coalesce.
* @returns The first non-null and non-undefined value, or undefined if all are null or undefined.
*/
function coalesce() {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
for (var _a = 0, values_1 = values; _a < values_1.length; _a++) {
var value = values_1[_a];
if (!isNullOrUndefined(value)) {
return value;
}
}
return undefined;
}
/**
* Returns the first non-null and non-undefined value from the provided array of values.
* If all values in the array are null or undefined, returns the specified default value.
*
* @template T - The type of the values and the default value.
* @param values - An array of values to coalesce.
* @param _default - The default value to return if all values in the array are null or undefined.
* @returns The first non-null and non-undefined value from the array, or the default value if all are null or undefined.
*
* @example
* const result = coalesceOrDefault([null, undefined, 'hello', 'world'], 'default');
* console.log(result); // Output: 'hello'
*
* @example
* const result = coalesceOrDefault([null, undefined], 'default');
* console.log(result); // Output: 'default'
*/
function coalesceOrDefault(values, _default) {
return getValueOrDefault(coalesce.apply(void 0, values), _default);
}
var createEventHandlerTelemetryConfig = function (name, options, contextConfig, event) { return ({
name: name,
disableSpanManagement: true,
spanOptions: options,
context: contextConfig.inheritFrom === 'EVENT'
? {
inheritFrom: 'TRACE_HEADERS',
traceHeaders: {
traceparent: event.traceparent,
tracestate: event.tracestate,
},
}
: {
inheritFrom: 'CONTEXT',
context: api_1.context.active(),
},
}); };
exports.createEventHandlerTelemetryConfig = createEventHandlerTelemetryConfig;