typescript-event-emitter
Version:
Versatile and feature-rich TypeScript library for event management, providing a solid foundation for building event-driven applications in TypeScript.
92 lines (91 loc) • 4.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.findEventInfo = exports.getPrioritizedValue = exports.isObjectEmpty = exports.insertSorted = exports.parseEvent = void 0;
const Constants_1 = require("./Constants");
/**
* Splits the given event string into namespace and event name parts.
* @param {string} event - The event string to parse.
* @param {string} separator - The separator used to split the event string.
* @returns A tuple containing the namespace and event name.
*/
const parseEvent = (event, separator) => {
const parts = event.split(separator);
const namespace = parts.length > 1 ? parts[0] : '';
const eventName = parts[parts.length - 1];
return [namespace, eventName];
};
exports.parseEvent = parseEvent;
/**
* Inserts a listener object into a sorted array based on priority.
* @param listeners - The array of listener objects to insert into.
* @param listenerObject - The listener object to be inserted, containing the listener function and its priority.
*/
const insertSorted = (listeners, listenerObject) => {
const index = listeners.findIndex(l => listenerObject.priority > l.priority);
if (index === -1) {
listeners.push(listenerObject);
}
else {
listeners.splice(index, 0, listenerObject);
}
};
exports.insertSorted = insertSorted;
/**
* Checks if the provided object is empty (has no own enumerable properties).
* @param obj - The object to check for emptiness.
* @returns `true` if the object is empty; otherwise, `false`.
*/
const isObjectEmpty = (obj) => {
return Object.keys(obj).length === 0;
};
exports.isObjectEmpty = isObjectEmpty;
/**
* Gets the prioritized value, favoring a new value over a default value.
* If a new value is provided and not empty, it is returned; otherwise, the default value is used.
* @param defaultValue - The default value.
* @param newValue - The potentially new value.
* @returns The prioritized value.
*/
const getPrioritizedValue = (defaultVal, newVal) => {
if (newVal)
return newVal;
return defaultVal;
};
exports.getPrioritizedValue = getPrioritizedValue;
/**
* Finds information about a specific event, including its separator and event name,
* within the provided event namespaces.
*
* @param event - The name of the event for which information is to be retrieved.
* @param eventNamespaces - A record containing namespaces and their associated events and listeners.
* @returns separator from event information.
*/
const findEventInfo = (event, eventNamespaces) => {
const findSeparatorInListeners = (listeners, condition) => {
const foundListener = listeners.find(condition);
return (foundListener === null || foundListener === void 0 ? void 0 : foundListener.eventInfo.separator) || null;
};
const findSeparatorForEvent = (namespace, eventName, isWildcard) => {
var _a, _b;
const listeners = (_b = (_a = eventNamespaces[namespace]) === null || _a === void 0 ? void 0 : _a[eventName]) === null || _b === void 0 ? void 0 : _b.listeners;
return listeners
? isWildcard
? findSeparatorInListeners(listeners, () => event.startsWith(namespace))
: findSeparatorInListeners(listeners, l => l.eventInfo.event === event) ||
(namespace === Constants_1.defaultWildCard
? findSeparatorInListeners(listeners, l => event.endsWith(`${l.eventInfo.separator}${eventName}`))
: null)
: null;
};
for (const [namespace, events] of Object.entries(eventNamespaces)) {
for (const [eventName] of Object.entries(events)) {
const isWildcard = eventName === Constants_1.defaultWildCard;
const separator = findSeparatorForEvent(namespace, eventName, isWildcard);
if (separator) {
return separator;
}
}
}
return Constants_1.defaultSeparator;
};
exports.findEventInfo = findEventInfo;
;