Skip to content

Commit

Permalink
add support for object params in notifications; resolves #19
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Kozjak <kozjakm1@gmail.com>
  • Loading branch information
mkozjak committed Oct 10, 2017
1 parent fd2d561 commit 388ebce
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 143 deletions.
288 changes: 155 additions & 133 deletions dist/index.browser-bundle.js

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions dist/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _keys = require("babel-runtime/core-js/object/keys");

var _keys2 = _interopRequireDefault(_keys);

var _regenerator = require("babel-runtime/regenerator");

var _regenerator2 = _interopRequireDefault(_regenerator);
Expand Down Expand Up @@ -343,14 +347,15 @@ exports.default = function (WebSocket) {

// check if any listeners are attached and forward event
if (message.notification && _this4.listeners(message.notification).length) {
if (!message.params.length) return _this4.emit(message.notification);
if (!(0, _keys2.default)(message.params).length) return _this4.emit(message.notification);

var args = [message.notification];

// using for-loop instead of unshift/spread because performance is better
for (var i = 0; i < message.params.length; i++) {
args.push(message.params[i]);
}return _this4.emit.apply(_this4, args);
if (message.params.constructor === Object) args.push(message.params);else
// using for-loop instead of unshift/spread because performance is better
for (var i = 0; i < message.params.length; i++) {
args.push(message.params[i]);
}return _this4.emit.apply(_this4, args);
}

if (!_this4.queue[message.id]) return;
Expand Down
4 changes: 4 additions & 0 deletions dist/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ var Server = function (_EventEmitter) {
});

var namespace = this.namespaces[ns];

if (namespace) {
delete namespace.rpc_methods;
delete namespace.events;
Expand Down Expand Up @@ -284,6 +285,9 @@ var Server = function (_EventEmitter) {
params[_key] = arguments[_key];
}

// flatten an object if no spreading is wanted
if (params.length === 1 && params[0] instanceof Object) params = params[0];

var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions src/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,17 @@ export default (WebSocket) => class Client extends EventEmitter
// check if any listeners are attached and forward event
if (message.notification && this.listeners(message.notification).length)
{
if (!message.params.length)
if (!Object.keys(message.params).length)
return this.emit(message.notification)

const args = [message.notification]

// using for-loop instead of unshift/spread because performance is better
for (let i = 0; i < message.params.length; i++)
args.push(message.params[i])
if (message.params.constructor === Object)
args.push(message.params)
else
// using for-loop instead of unshift/spread because performance is better
for (let i = 0; i < message.params.length; i++)
args.push(message.params[i])

return this.emit.apply(this, args)
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export default class Server extends EventEmitter
// forward emitted event to subscribers
this.on(name, (...params) =>
{
// flatten an object if no spreading is wanted
if (params.length === 1 && params[0] instanceof Object)
params = params[0]

for (const socket_id of this.namespaces[ns].events[name])
{
this.namespaces[ns].clients.get(socket_id).send(JSON.stringify({
Expand Down
12 changes: 12 additions & 0 deletions test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ describe("Client", function()
done()
})
})

it("should receive an event with a single object value", function(done)
{
server.emit("newsUpdate", { foo: "bar", boo: "baz" })

client.once("newsUpdate", function(obj)
{
obj.should.be.an.instanceOf(Object)
expect(obj).to.deep.equal({ foo: "bar", boo: "baz" })
done()
})
})
})

describe(".unsubscribe", function()
Expand Down

0 comments on commit 388ebce

Please sign in to comment.