Skip to content

Commit

Permalink
lib: change event signature
Browse files Browse the repository at this point in the history
Changes event message signature to always send an array
of arbitrary arguments. This makes it work same way as
EventEmitter does and also makes its usage similar to
call action.

PR-URL: #187
Fixes: #134
Fixes: #162
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Mykola Bilochub <nbelochub@gmail.com>
  • Loading branch information
lundibundi authored and belochub committed Jan 22, 2018
1 parent cb7ed8b commit 2637db2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
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();
});
});

0 comments on commit 2637db2

Please sign in to comment.