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

41 lines
1.5 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, 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) return(dispatchEvents(redisCnx, msg, chan, eventHandlers))
return(false)
})
}