0.1.19 add ack to default socket commands, add socketsListen and consumersListen and private _eventListen methods

master
David Kebler 2019-04-20 16:57:44 -07:00
parent 3ab37ae694
commit 6da51d453f
3 changed files with 33 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@uci/base",
"version": "0.1.18",
"version": "0.1.19",
"description": "Multi type and transport JSON packet communication base class. Used in UCI extended classes",
"main": "src/base",
"scripts": {

View File

@ -210,7 +210,7 @@ class Base extends EventEmitter {
sends.push(this.socket[name].send.bind(this.socket[name]))
}
}
if (sends.length === 1) return sends[0](packet)
if (sends.length === 1) return await sends[0](packet)
return Promise.all(
sends.map(send => {
return send(packet)
@ -277,6 +277,14 @@ class Base extends EventEmitter {
return found
}
socketsListen(event,fn) {
this._eventListen('s',event,fn)
}
consumersListen(event,fn) {
this._eventListen('c',event,fn)
}
// TODO confirm Object.assign will be ok as it is not a deep copy
amendConsumerProcessing(funcs, trans) {
if (trans) {
@ -362,6 +370,19 @@ class Base extends EventEmitter {
else return init()
}
_eventListen(type,event,fn) {
for (let name of Object.keys(this.socket)) {
if (this.socket[name].type === type) {
if (fn==='stop') this.socket[name].removeAllListeners(event)
else {
console.log('adding listener',name,type,event,fn )
console.log(this.socket[name].name)
this.socket[name].on(event, fn)
}
}
}
}
_transport(name) {
return this.socket[name].transport
} //getter for socket transport
@ -417,6 +438,8 @@ class Base extends EventEmitter {
*/
async _packetProcess(socket_name, packet) {
// TODO Try all added packet processors then defualt before sending back
// console.log(socket_name,packet)
let processor =
packet._processor || this._processors[socket_name] || '_default'

View File

@ -63,12 +63,14 @@ const namespaces = {
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)
})
packet.processed = true
packet.info = 'default socket echo'
return packet
},
ack: async packet => {
packet.cmd = 'reply'
packet.ack = true
return packet
}
},
c:{