36 lines
1014 B
JavaScript
36 lines
1014 B
JavaScript
|
|
export function dispatchEvents(redisCnx, msg, chan, eventHandlers) {
|
|
const eventType = msg.eventType
|
|
const sender = msg.sender ?? null
|
|
const cnxId = msg.cnxId ?? null
|
|
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, sender, cnxId)
|
|
} 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)
|
|
}
|