Skip to content

Commit

Permalink
lazily add bus event listeners for interface
Browse files Browse the repository at this point in the history
  • Loading branch information
acrisci committed Aug 2, 2019
1 parent 8ffad88 commit bd08f64
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
59 changes: 59 additions & 0 deletions lib/client/proxy-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,65 @@ class ProxyInterface extends EventEmitter {
this.$properties = [];
this.$methods = [];
this.$signals = [];
this.$listeners = {};

let getEventDetails = (eventName) => {
let signal = this.$signals.find((s) => s.name === eventName);
if (!signal) {
return [null, null];
}

let detailedEvent = JSON.stringify({
path: this.$object.path,
interface: this.$name,
member: eventName
});

return [signal, detailedEvent];
}

this.on('removeListener', (eventName, listener) => {
let [signal, detailedEvent] = getEventDetails(eventName);

if (!signal) {
return;
}

this.$object.bus._signals.off(detailedEvent, this._getEventListener(signal));

This comment has been minimized.

Copy link
@leonardo-k

leonardo-k Aug 5, 2019

in node version < 10 the ' .off ' function is not supported .
i think that call " removeListener " function instead of .off would be better , so we can cover node since version v0.1.26

});

this.on('newListener', (eventName, listener) => {
let [signal, detailedEvent] = getEventDetails(eventName);

if (!signal) {
return;
}

this.$object.bus._signals.on(detailedEvent, this._getEventListener(signal));
});
}

_getEventListener(signal) {
if (this.$listeners[signal.name]) {
return this.$listeners[signal.name];
}

let obj = this.$object;
let bus = obj.bus;

this.$listeners[signal.name] = (msg) => {
let {body, signature, sender} = msg;
if (bus._nameOwners[obj.name] !== sender) {
return;
}
if (signature !== signal.signature) {
console.error(`warning: got signature ${signature} for signal ${iface.$name}.${signal.name} (expected ${signal.signature})`);
return;
}
this.emit.apply(this, [signal.name].concat(body));
};

return this.$listeners[signal.name];
}

static _fromXml(object, xml) {
Expand Down
18 changes: 0 additions & 18 deletions lib/client/proxy-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,6 @@ class ProxyObject {
if (iface !== null) {
this.interfaces[iface.$name] = iface;
this.bus._addMatch(`type='signal',sender=${this.name},interface='${iface.$name}',path='${this.path}'`);
for (let signal of iface.$signals) {
let event = JSON.stringify({
path: this.path,
interface: iface.$name,
member: signal.name
});
this.bus._signals.on(event, (msg) => {
let {body, signature, sender} = msg;
if (this.bus._nameOwners[this.name] !== sender) {
return;
}
if (signature !== signal.signature) {
console.error(`warning: got signature ${signature} for signal ${iface.$name}.${signal.name} (expected ${signal.signature})`);
return;
}
iface.emit.apply(iface, [signal.name].concat(body));
});
}
}
}
}
Expand Down

1 comment on commit bd08f64

@leonardo-k
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes have solved my memory leak , if I find other problems or bugs I will create other issues.
the only thing is that the function .off is not supported in oldest node's versions and I had to change it with the "removeListener" function to test it (we are forced an old node version)so if we can substitue it with removeListener i think will be better.
Thank you so much for your help :) I really appreciated it

Please sign in to comment.