uci-base/src/processing.mjs

86 lines
2.5 KiB
JavaScript

// this._processing refers to this module/hash
const processor = async function (packet,socket) {
return await process[this.getSocket(socket).type].bind(this)(packet)
}
export { processor, commands }
const process = {
s: async function (packet) {
// add namepaces by unshifting them onto look_in
// console.log('in default socket processor',packet.cmd)
if (!packet.cmd) return {error: 'no command in packet', packet: packet }
let cmd_func = null
let namespaces = ['s',null,'_defaultCmds.s']
namespaces.some( namespace => {
cmd_func = this.getCmdFunc(namespace+'.'+packet.cmd)
if (cmd_func) return true
})
if (cmd_func) return cmd_func.bind(this)(packet)
return {error: 'no socket processing function supplied for command', packet: packet }
},
c: async function (packet) {
// console.log('in default consumer processor',packet.cmd)
if (packet.error) this._defaultCmds.c.error(packet)
if (packet.cmd) {
// move namespaces to class constructor and add unshift method for adding
let namespaces = ['c.'+packet.cmd,packet.cmd,'_defaultCmds.c.'+packet.cmd]
let cmd_func = false
namespaces.forEach( location => {
cmd_func = this.getCmdFunc(location)
if(cmd_func)return
})
if (cmd_func) return cmd_func.bind(this)(packet)
packet = {error:'no consumer processing function supplied for command',packet:packet}
this._defaultCmds.c.error(packet)
} else {
packet = {error:'no command in packet',packet:packet}
this._defaultCmds.c.error(packet)
}
}
}
/*
*
* Default packed command processing
*
*/
const commands ={
s:{
echo: async packet => {
return new Promise( async (resolve) => {
packet.processed = true
packet.cmd = 'reply'
packet.info = 'default socket echo'
return resolve(packet)
})
}
},
c:{
error: function (packet) {
console.log('==============Packet ERROR==========')
console.log(packet.error )
console.dir(packet.packet)
console.log('===========================')
},
reply: function(packet) {
console.log('==============Packet returned from socket==========')
console.dir(packet)
console.log('===========================')
this.amendConsumerProcessing({
reply: function (packet) {
console.log('==============Amended Default Packet Replay for Consumer=========')
console.dir(packet)
console.log('===========================')
}
})
}
}
}