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", "name": "@uci/base",
"version": "0.1.0", "version": "0.1.1",
"description": "Mutli Level/Transport Message/Event Classes", "description": "Mutli Level/Transport Message/Event Classes",
"main": "src/base", "main": "src/base",
"scripts": { "scripts": {
@ -28,15 +28,16 @@
"homepage": "https://github.com/uCOMmandIt/message#readme", "homepage": "https://github.com/uCOMmandIt/message#readme",
"@std/esm": "cjs", "@std/esm": "cjs",
"devDependencies": { "devDependencies": {
"@std/esm": "^0.18.0", "@std/esm": "^0.22.0",
"chai": "^4.1.2", "chai": "^4.1.2",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",
"codecov": "^3.0.0", "codecov": "^3.0.0",
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"mocha": "^4.0.1", "mocha": "^5.0.1",
"nodemon": "^1.14.12" "nodemon": "^1.14.12"
}, },
"dependencies": { "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'
import UCISocket from '../../uci-socket/src' // import UCISocket from '../../uci-socket/src'
import EventEmitter from 'events' import EventEmitter from 'events'
import { bindFuncs } from '@uci/utils/src/function'
import { processor, commands, namespaces } from './processing.mjs' 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 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.socket[socket.name]._packetProcess = this._packetProcess.bind(this,socket.name)
}) })
this.bindFuncs = bindFuncs
// console.log('base opts', opts)
} // end constructor } // end constructor
@ -41,17 +44,20 @@ export default class Base extends EventEmitter {
sockets.push(this.socket[name].connect()) 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 } // init
async end(name) {} // TODO end all or named sockets
async send (name,packet) { async send (name,packet) {
if (typeof name !== 'string') { if (typeof name !== 'string') {
packet = name packet = name
let sends = [] let sends = []
for(let name of Object.keys(this.socket)){ for(let name of Object.keys(this.socket)){
if (this.socket[name].type ==='c') { 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])) sends.push(this.socket[name].send.bind(this.socket[name]))
} }
} }
@ -94,6 +100,8 @@ export default class Base extends EventEmitter {
return found return found
} }
// TODO confirm Object.assign will be ok as it is not a deep copy
amendConsumerProcessing(funcs,trans) { amendConsumerProcessing(funcs,trans) {
if (trans) { if (trans) {
if (!this._defaultCmds.c[trans]) this._defaultCmds.c[trans] ={} if (!this._defaultCmds.c[trans]) this._defaultCmds.c[trans] ={}
@ -120,8 +128,10 @@ export default class Base extends EventEmitter {
} }
} }
beforeHook (type,funcs){} // takes and returns a packet
afterHook(type,funcs){} 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 // here you can add namespaced functions for packet commands
consumersProcessor(func) { consumersProcessor(func) {
@ -154,6 +164,8 @@ export default class Base extends EventEmitter {
this._packetProcess = func this._packetProcess = func
} }
/* /*
* *
* Private Methods * Private Methods

View File

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