Files
P42_godDaemons/bus/publishActionReply.js

65 lines
1.5 KiB
JavaScript

export function busReplyRoute(daemonBlock, meshName) {
if(!daemonBlock?.senderId) return(null)
const onArena = meshName === 'arena'
const actionsReply = onArena
? (daemonBlock.bus?.arena?.actionsReply ?? daemonBlock.ActionsReply)
: daemonBlock.ActionsReply
if(!actionsReply) return(null)
return({
senderId: daemonBlock.senderId,
actionsReply,
})
}
export function publishActionReply(redisCnx, options) {
const {
action,
reqid,
sender,
reply,
replyChannel,
} = options
reply.action = action
reply.sender = redisCnx.senderId
reply.cnxId = redisCnx.cnxId
if(reqid) reply.reqid = reqid
const chan = replyChannel.replace(/\[UID\]/g, sender)
.replace(/\[CUID\]/g, redisCnx.cnxId)
redisCnx.redisPublish(chan, reply)
}
export function replyToAction(redisCnx, options) {
const {
action,
reqid,
sender,
success,
payload,
err,
replyChannel,
} = options
const routeReplyChannel = replyChannel ?? redisCnx.actionsReply
if(!routeReplyChannel) {
console.error(`[${redisCnx.redisId}] Cannot resolve action reply route`)
return
}
const reply = { success }
if(err != null) reply.err = err
if(payload !== undefined) reply.payload = payload
publishActionReply(redisCnx, {
action,
reqid,
sender,
replyChannel: routeReplyChannel,
reply,
})
}