General Actions to handlers Refacto
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user