44 lines
1.6 KiB
JavaScript
44 lines
1.6 KiB
JavaScript
import { dispatchActions } from './dispatchActions.js'
|
|
import { dispatchEvents } from './dispatchEvents.js'
|
|
|
|
export function assembleHandlers(modules) {
|
|
const actions = {}
|
|
const tree = {}
|
|
const afterLogin = []
|
|
|
|
for(const mod of modules) {
|
|
if(mod.actions) Object.assign(actions, mod.actions)
|
|
if(typeof(mod.construct) === 'function') afterLogin.push(mod.construct)
|
|
if(!mod.eventHandlers) continue
|
|
for(const [channelPattern, byType] of Object.entries(mod.eventHandlers)) {
|
|
if(!tree[channelPattern]) tree[channelPattern] = {}
|
|
for(const [eventType, handler] of Object.entries(byType)) {
|
|
if(!tree[channelPattern][eventType]) tree[channelPattern][eventType] = []
|
|
const list = Array.isArray(handler) ? handler : [handler]
|
|
tree[channelPattern][eventType].push(...list)
|
|
}
|
|
}
|
|
}
|
|
|
|
return({
|
|
actionHandlers: actions,
|
|
eventHandlers: tree,
|
|
afterLogin,
|
|
})
|
|
}
|
|
|
|
export function createDispatchMessage({ eventHandlers, eventRules, actionRules }) {
|
|
return(async function dispatchMessage(redisCnx, msg, chan) {
|
|
if(msg.action && msg.eventType) {
|
|
console.warn(`[${redisCnx.redisId}] Message has both action and eventType on ${chan}`)
|
|
return(false)
|
|
}
|
|
if(msg.action) return(dispatchActions(redisCnx, msg, chan, actionRules(redisCnx)))
|
|
if(msg.eventType) {
|
|
const handlers = eventRules ? eventRules(redisCnx) : eventHandlers
|
|
return(dispatchEvents(redisCnx, msg, chan, handlers))
|
|
}
|
|
return(false)
|
|
})
|
|
}
|