add bindFuncs method for binding a whole module of functions to the base instance (this) or a given call site

update deps
master
David Kebler 2018-02-17 18:23:38 -08:00
parent e6774301c2
commit 2e07dab28d
3 changed files with 24 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@uci/base",
"version": "0.1.0",
"version": "0.1.1",
"description": "Mutli Level/Transport Message/Event Classes",
"main": "src/base",
"scripts": {
@ -28,15 +28,16 @@
"homepage": "https://github.com/uCOMmandIt/message#readme",
"@std/esm": "cjs",
"devDependencies": {
"@std/esm": "^0.18.0",
"@std/esm": "^0.22.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"codecov": "^3.0.0",
"istanbul": "^0.4.5",
"mocha": "^4.0.1",
"mocha": "^5.0.1",
"nodemon": "^1.14.12"
},
"dependencies": {
"@uci/socket": "^0.1.0"
"@uci/socket": "^0.1.1",
"@uci/utils": "^0.1.1"
}
}

View File

@ -1,6 +1,7 @@
// import { Socket, Consumer } from '@uci/socket'
import UCISocket from '../../uci-socket/src'
import UCISocket from '@uci/socket'
// import UCISocket from '../../uci-socket/src'
import EventEmitter from 'events'
import { bindFuncs } from '@uci/utils/src/function'
import { processor, commands, namespaces } from './processing.mjs'
@ -27,6 +28,8 @@ export default class Base extends EventEmitter {
Object.assign(this.socket[socket.name],socket) // copy socket info props to new socket
this.socket[socket.name]._packetProcess = this._packetProcess.bind(this,socket.name)
})
this.bindFuncs = bindFuncs
// console.log('base opts', opts)
} // end constructor
@ -41,17 +44,20 @@ export default class Base extends EventEmitter {
sockets.push(this.socket[name].connect())
}
}
await Promise.all(sockets)
// TODO maybe throw if one fails
return Promise.all(sockets).then(() => {return 'ready'}).catch((err) => {return err})
} // init
async end(name) {} // TODO end all or named sockets
async send (name,packet) {
if (typeof name !== 'string') {
packet = name
let sends = []
for(let name of Object.keys(this.socket)){
if (this.socket[name].type ==='c') {
// console.log(name)
// console.log(name, this.socket[name])
sends.push(this.socket[name].send.bind(this.socket[name]))
}
}
@ -94,6 +100,8 @@ export default class Base extends EventEmitter {
return found
}
// TODO confirm Object.assign will be ok as it is not a deep copy
amendConsumerProcessing(funcs,trans) {
if (trans) {
if (!this._defaultCmds.c[trans]) this._defaultCmds.c[trans] ={}
@ -120,8 +128,10 @@ export default class Base extends EventEmitter {
}
}
beforeHook (type,funcs){}
afterHook(type,funcs){}
// takes and returns a packet
beforeWriteHook (type,funcs){} // TODO before packet send
afterReceiveHook(type,funcs){} // TODO after receiv
afterProcessHook(type,funcs){} // TODO
// here you can add namespaced functions for packet commands
consumersProcessor(func) {
@ -154,6 +164,8 @@ export default class Base extends EventEmitter {
this._packetProcess = func
}
/*
*
* Private Methods

View File

@ -59,7 +59,7 @@ const commands ={
},
c:{
error: function (packet) {
console.log('==============Packet ERROR==========')
console.log('==============Packet ERROR [consumer]==========')
console.log(packet.error )
console.dir(packet.packet)
console.log('===========================')