Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: change event signature #187

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Connection.prototype.callback = function(packetId, error, result) {
// Send an event packet over the connection
// interfaceName - name of an interface
// eventName - name of an event
// args - event arguments as an object
// args - event arguments as an array
//
Connection.prototype.emitRemoteEvent = function(
interfaceName, eventName, args
Expand Down Expand Up @@ -539,7 +539,7 @@ Connection.prototype._processEventPacket = function(packet, keys) {

const remoteProxy = this.remoteProxies[interfaceName];
if (remoteProxy) {
remoteProxy.emit(eventName, eventArgs, true);
remoteProxy._emitLocal(eventName, eventArgs);
}
};

Expand Down
25 changes: 12 additions & 13 deletions lib/remote-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@ function RemoteProxy(connection, interfaceName, methods = []) {

util.inherits(RemoteProxy, events.EventEmitter);

// Emit an event. By default, when the dontRetranslate parameter is false or
// undefined, the event is also emmited on the other part of the JSTP
// connection so all the 'on' handlers will work on both sides regardless of
// where event has been emitted. However, you can turn it off by setting
// dontRetranslate to true.
// Emit an event.
// eventName - name of an event
// eventArgs - object of event arguments
// dontRetranslate - turn off sending the corresponding event packet over the
// JSTP connection
// eventArgs - event arguments
//
RemoteProxy.prototype.emit = function(eventName, eventArgs, dontRetranslate) {
if (!dontRetranslate) {
this._connection.emitRemoteEvent(this._interfaceName, eventName, eventArgs);
}
RemoteProxy.prototype.emit = function(eventName, ...eventArgs) {
this._connection.emitRemoteEvent(this._interfaceName, eventName, eventArgs);
this._emitLocal(eventName, eventArgs);
};

events.EventEmitter.prototype.emit.call(this, eventName, eventArgs);
// Emit local event.
// eventName - name of an event
// eventArgs - array of event arguments
//
RemoteProxy.prototype._emitLocal = function(eventName, eventArgs = []) {
events.EventEmitter.prototype.emit.call(this, eventName, ...eventArgs);
};

// Create a method in a remote proxy that will call the corresponding remote
Expand Down
2 changes: 1 addition & 1 deletion test/unit/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ describe('JSTP Connection', () => {

describe('event', () => {
testPacketSending('event', (connection, transport) => {
const eventArgs = { arg: 'value' };
const eventArgs = [ 'value' ];

const sendSpy = chai.spy((data) => {
const packet = jstp.parse(data);
Expand Down
10 changes: 5 additions & 5 deletions test/unit/remote-proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('RemoteProxy', () => {
emitRemoteEvent() { },

processEventPacket() {
proxy.emit('testEvent', 'payload', true);
proxy._emitLocal('testEvent', ['payload1', 'payload2']);
}
};

Expand Down Expand Up @@ -64,22 +64,22 @@ describe('RemoteProxy', () => {
it('must emit events through the network and locally', () => {
const handler = chai.spy();
proxy.on('testEvent', handler);
proxy.emit('testEvent', 'payload');
proxy.emit('testEvent', 'payload1', 'payload2');

expect(eventSpy).to.have.been.called.exactly(1);
expect(eventSpy).to.have.been.called.with(
'testInterface', 'testEvent', 'payload');
'testInterface', 'testEvent', ['payload1', 'payload2']);

expect(handler).to.have.been.called.exactly(1);
expect(handler).to.have.been.called.with('payload');
expect(handler).to.have.been.called.with('payload1', 'payload2');
});

it('must not re-emit events back', () => {
const handler = chai.spy();
proxy.on('testEvent', handler);
connectionMock.processEventPacket();

expect(handler).to.have.been.called.with('payload');
expect(handler).to.have.been.called.with('payload1', 'payload2');
expect(eventSpy).to.not.have.been.called();
});
});