0.5.4 Full working removeSocket, removes also correpsonding s or c observers

master
David Kebler 2020-07-26 16:51:37 -07:00
parent cf1182d805
commit da75d1a468
2 changed files with 14 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci/base", "name": "@uci/base",
"version": "0.5.3", "version": "0.5.4",
"description": "Multi type and transport JSON packet communication base class. Used in UCI extended classes", "description": "Multi type and transport JSON packet communication base class. Used in UCI extended classes",
"main": "src/base", "main": "src/base",
"scripts": { "scripts": {
@ -46,11 +46,11 @@
"@uci-utils/bind-funcs": "^0.2.4", "@uci-utils/bind-funcs": "^0.2.4",
"@uci-utils/logger": "^0.0.18", "@uci-utils/logger": "^0.0.18",
"@uci-utils/ready": "^0.3.1", "@uci-utils/ready": "^0.3.1",
"@uci/mqtt": "^0.3.1", "@uci/mqtt": "^0.3.2",
"@uci/socket": "^0.3.2", "@uci/socket": "^0.3.3",
"@uci/websocket": "^0.4.0", "@uci/websocket": "^0.4.1",
"await-to-js": "^2.1.1", "await-to-js": "^2.1.1",
"is-plain-object": "^4.1.0", "is-plain-object": "^4.1.1",
"merge-anything": "^3.0.5" "merge-anything": "^3.0.5"
} }
} }

View File

@ -293,7 +293,7 @@ class Base extends EventEmitter {
} }
/** /**
* removeSocket - TODO not available * removeSocket
* *
* @param {string} name name of socket as created * @param {string} name name of socket as created
* @returns {String | Object } success string or error object * @returns {String | Object } success string or error object
@ -301,20 +301,24 @@ class Base extends EventEmitter {
async removeSocket(name) { async removeSocket(name) {
// NOTE: uci consumers have .end renamed as .close to match socket method for convenience // NOTE: uci consumers have .end renamed as .close to match socket method for convenience
if (!this.getSocket(name)) return 'no socket by that name' if (typeof name !=='string') return 'no socket name passed, nothing to remove'
const socket = this.getSocket(name)
if (!socket) return 'no socket by that name, nothing to remove'
let closeError let closeError
let [err] = await to(this._socket[name].close()) if (typeof socket.close !== 'function') return 'bad socket no close function, nothing to remove'
let [err] = await to(socket.close())
if(err) if (err.code !== 'ERR_SERVER_NOT_RUNNING') { if(err) if (err.code !== 'ERR_SERVER_NOT_RUNNING') {
closeError = {socket:this._socket[name].name, error:err, msg:'socket/consumer closed with errors, but removed'} closeError = {socket:this._socket[name].name, error:err, msg:'socket/consumer closed with errors, but removed'}
} }
this.emit('log', {level:'warn', msg:`socket ${name} has been removed`, socket:this._socket[name].opts}) this.emit('log', {level:'warn', msg:`socket ${name} has been removed`, socket:this._socket[name].opts})
this._socket[name].removeAllListeners() socket.removeAllListeners()
this.ready.removeObserver(socket.type==='c' ? this._socket[name].obsName : `${name}:socket<listening>`)
delete this._socket[name] delete this._socket[name]
return closeError ? closeError : 'success' return closeError ? closeError : 'success'
} }
getSocket(name) { getSocket(name) {
if (name) return this._socket[name] if (name) return this._socket[name] || null
else return this._socket else return this._socket
} }
// returns array of names of sockets that pass filter // returns array of names of sockets that pass filter