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'
|
||||||
// import { Socket, Consumer } from '../../uci-socket/src'
|
import { Socket, Consumer } from '../../uci-socket/src'
|
||||||
import packet from './packet.mjs'
|
import packet from './packet.mjs'
|
||||||
import EventEmitter from 'events'
|
import EventEmitter from 'events'
|
||||||
|
|
||||||
const USOCKET = __dirname + '/unix.sock'
|
const USOCKET = __dirname + '/unix.sock'
|
||||||
|
|
||||||
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
|
||||||
|
|
||||||
export default class Base extends EventEmitter {
|
export default class Base extends EventEmitter {
|
||||||
constructor(opts={}) {
|
constructor(opts={}) {
|
||||||
super()
|
super()
|
||||||
opts.path = opts.path || USOCKET
|
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
|
this.desc = opts.desc // additional details for humans
|
||||||
// attach unix socket OR consumer(default) only if path is supplied
|
|
||||||
this.socket={}
|
this.socket={}
|
||||||
opts.sockets.split(/[,:|\s]+/).forEach( sock => {
|
opts.sockets.split(/[,:|\s]+/).forEach( sock => {
|
||||||
opts.name = this.id +':'+sock
|
opts.id = this.id +':'+sock
|
||||||
if (!opts[sock]) opts[sock] = {}
|
if (!opts[sock]) opts[sock] = {}
|
||||||
switch (sock) {
|
switch (sock) {
|
||||||
case
|
case
|
||||||
|
@ -43,7 +40,7 @@ export default class Base extends EventEmitter {
|
||||||
|
|
||||||
async init (context) {
|
async init (context) {
|
||||||
let sockets = []
|
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)
|
this.registerPacketContext(context)
|
||||||
for(let type of Object.keys(this.socket)){
|
for(let type of Object.keys(this.socket)){
|
||||||
if (type.indexOf('s')!==-1) {
|
if (type.indexOf('s')!==-1) {
|
||||||
|
@ -59,11 +56,11 @@ export default class Base extends EventEmitter {
|
||||||
|
|
||||||
registerPacketContext(type, context) {
|
registerPacketContext(type, context) {
|
||||||
if (typeof type === 'string') {
|
if (typeof type === 'string') {
|
||||||
this.socket[type].packet.context = Object.assign({},context)
|
this.socket[type].packet.context = context
|
||||||
} else {
|
} else {
|
||||||
context=type
|
context=type
|
||||||
for(let type of Object.keys(this.socket)){
|
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
|
} // end class
|
||||||
|
|
Loading…
Reference in New Issue