parent
9679f61aae
commit
51986fbcc7
19
src/base.mjs
19
src/base.mjs
|
@ -1,22 +1,19 @@
|
|||
import { Socket, Consumer } from '@uci/socket'
|
||||
// import { Socket, Consumer } from '../../uci-socket/src'
|
||||
// import { Socket, Consumer } from '@uci/socket'
|
||||
import { Socket, Consumer } from '../../uci-socket/src'
|
||||
import packet from './packet.mjs'
|
||||
import EventEmitter from 'events'
|
||||
|
||||
const USOCKET = __dirname + '/unix.sock'
|
||||
|
||||
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
||||
|
||||
export default class Base extends EventEmitter {
|
||||
constructor(opts={}) {
|
||||
super()
|
||||
opts.path = opts.path || USOCKET
|
||||
this.id = opts.id // a unique ecosystem wide id, could be generated
|
||||
this.id = opts.id || opts.name || 'uci:'+ Math.random()*100
|
||||
this.desc = opts.desc // additional details for humans
|
||||
// attach unix socket OR consumer(default) only if path is supplied
|
||||
this.socket={}
|
||||
opts.sockets.split(/[,:|\s]+/).forEach( sock => {
|
||||
opts.name = this.id +':'+sock
|
||||
opts.id = this.id +':'+sock
|
||||
if (!opts[sock]) opts[sock] = {}
|
||||
switch (sock) {
|
||||
case
|
||||
|
@ -43,7 +40,7 @@ export default class Base extends EventEmitter {
|
|||
|
||||
async init (context) {
|
||||
let sockets = []
|
||||
context = context || this // additional context is Base instance (or callsite) if not supplied
|
||||
context = context || this // additional context is Base instance (or callsite) if none supplied
|
||||
this.registerPacketContext(context)
|
||||
for(let type of Object.keys(this.socket)){
|
||||
if (type.indexOf('s')!==-1) {
|
||||
|
@ -59,11 +56,11 @@ export default class Base extends EventEmitter {
|
|||
|
||||
registerPacketContext(type, context) {
|
||||
if (typeof type === 'string') {
|
||||
this.socket[type].packet.context = Object.assign({},context)
|
||||
this.socket[type].packet.context = context
|
||||
} else {
|
||||
context=type
|
||||
for(let type of Object.keys(this.socket)){
|
||||
this.socket[type].packet.context = Object.assign({},context)
|
||||
this.socket[type].packet.context = context
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +118,7 @@ export default class Base extends EventEmitter {
|
|||
}
|
||||
}
|
||||
}
|
||||
this.emit('packet', packet) // intra process, need to set a 'packet' listener to process
|
||||
this.emit('packet', packet) // intra process, need to set a 'packet' listener to process it
|
||||
}
|
||||
|
||||
} // end class
|
||||
|
|
Loading…
Reference in New Issue