Skip to content

Commit

Permalink
add event tracking to interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
tkurki committed Dec 3, 2023
1 parent 746bdd9 commit 1289633
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ function startMdns(app: ServerApp & WithConfig) {
}
}

function startInterfaces(app: ServerApp & WithConfig) {
function startInterfaces(app: ServerApp & WithConfig & WithWrappedEmitter) {
debug('Interfaces config:' + JSON.stringify(app.config.settings.interfaces))
const availableInterfaces = require('./interfaces')
_.forIn(availableInterfaces, (theInterface: any, name: string) => {
Expand All @@ -598,14 +598,35 @@ function startInterfaces(app: ServerApp & WithConfig) {
(app.config.settings.interfaces || {})[name]
) {
debug(`Loading interface '${name}'`)
app.interfaces[name] = theInterface(app)
if (app.interfaces[name] && _.isFunction(app.interfaces[name].start)) {
const boundEventMethods = app.wrappedEmitter.bindMethodsById(
`interface:${name}` as EventsActorId
)

const appCopy = {
...app,
...boundEventMethods
}
const handler = {
set(obj: any, prop: any, value: any) {
;(app as any)[prop] = value
;(appCopy as any)[prop] = value
return true
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
get(target: any, prop: string | symbol, receiver: any) {
return (appCopy as any)[prop]
}
}
const _interface = (appCopy.interfaces[name] = theInterface(
new Proxy(appCopy, handler)
))
if (_interface && _.isFunction(_interface.start)) {
if (
_.isUndefined(app.interfaces[name].forceInactive) ||
!app.interfaces[name].forceInactive
_.isUndefined(_interface.forceInactive) ||
!_interface.forceInactive
) {
debug(`Starting interface '${name}'`)
app.interfaces[name].data = app.interfaces[name].start()
_interface.data = _interface.start()
} else {
debug(`Not starting interface '${name}' by forceInactive`)
}
Expand Down

0 comments on commit 1289633

Please sign in to comment.