rover manager improvements and event bus

This commit is contained in:
legop3
2025-11-19 16:49:53 -05:00
parent 598c0404a2
commit d49ec34f0a
3 changed files with 112 additions and 5 deletions
+55
View File
@@ -0,0 +1,55 @@
const EventEmitter = require('events');
const logger = require('../globals/logger').child('eventBus');
const eventBus = new EventEmitter();
/**
* Publish a structured event onto the server-wide bus.
* @param {object} options
* @param {string} options.source - Origin module identifier.
* @param {string} options.type - Event type name.
* @param {any} [options.payload] - Optional event payload.
*/
function publishEvent({ source, type, payload = null }) {
if (!source) {
throw new Error('eventBus.publishEvent requires source');
}
if (!type) {
throw new Error('eventBus.publishEvent requires type');
}
const event = {
source,
type,
payload,
ts: Date.now(),
};
logger.debug('Publishing event', { source, type });
eventBus.emit(type, event);
eventBus.emit('*', event);
}
/**
* Subscribe to events of a given type.
* @param {string} type
* @param {(event: object) => void} handler
*/
function subscribe(type, handler) {
eventBus.on(type, handler);
return () => eventBus.off(type, handler);
}
/**
* Subscribe to all events on the bus.
* @param {(event: object) => void} handler
*/
function subscribeAll(handler) {
eventBus.on('*', handler);
return () => eventBus.off('*', handler);
}
module.exports = {
eventBus,
publishEvent,
subscribe,
subscribeAll,
};