Files
P42_godDaemons/bus/dispatchEvents.js
T
2026-06-20 18:50:26 +00:00

34 lines
925 B
JavaScript

export function dispatchEvents(redisCnx, msg, chan, eventHandlers) {
const eventType = msg.eventType
if(!eventType || typeof(eventType) !== 'string') return(false)
let handled = false
for(const [channelPattern, byType] of Object.entries(eventHandlers ?? {})) {
if(!redisCnx.matchesChan(chan, channelPattern)) continue
const handlers = byType[eventType]
if(!handlers?.length) continue
for(const handle of handlers) {
try {
handle.call(redisCnx, msg, chan)
} catch(err) {
console.error(
`[${redisCnx.redisId}] Event ${eventType} on ${chan} failed:`,
err
)
}
}
handled = true
}
if(!handled && redisCnx.debug) {
console.log(`[${redisCnx.redisId}] Unhandled event ${eventType} on ${chan}`)
}
return(handled)
}