From fd6ce60a1321c99934223f7158e7857cdc76bce0 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 25 Jul 2013 18:10:16 +0100 Subject: [PATCH] Improved the constructor of the mosca.Server. The constructor can now be called without the 'new' and it yields the server as the second object of the callback. --- lib/server.js | 9 +++++- test/server_spec.js | 74 ++++++++++++++++++--------------------------- 2 files changed, 37 insertions(+), 46 deletions(-) diff --git a/lib/server.js b/lib/server.js index 7c0aa06..3c69acc 100644 --- a/lib/server.js +++ b/lib/server.js @@ -59,6 +59,11 @@ var defaults = { * @api public */ function Server(opts, callback) { + + if (!(this instanceof Server)) { + return new Server(opts, callback); + } + EventEmitter.call(this); this.opts = extend(true, {}, defaults, opts); @@ -83,7 +88,9 @@ function Server(opts, callback) { this.ascoltatore = opts.ascoltatore || ascoltatori.build(this.opts.backend); this.ascoltatore.on("error", this.emit.bind(this)); - that.once("ready", callback); + that.once("ready", function() { + callback(null, that); + }); async.series([ function(cb) { diff --git a/test/server_spec.js b/test/server_spec.js index 8df030f..aff3aa9 100644 --- a/test/server_spec.js +++ b/test/server_spec.js @@ -79,6 +79,21 @@ describe("mosca.Server", function() { }); } + it("should pass itself in the callback", function(done) { + secondInstance = new mosca.Server(moscaSettings(), function(err, server) { + expect(server === secondInstance).to.be.true; + done(); + }); + }); + + it("should allow to be called like a function", function(done) { + var func = mosca.Server; + secondInstance = func(moscaSettings(), function(err, server) { + expect(server === secondInstance).to.be.true; + done(); + }); + }); + it("should support connecting and disconnecting", function(done) { buildClient(done, function(client) { @@ -626,7 +641,9 @@ describe("mosca.Server", function() { type: "mqtt" }; settings.port = nextPort(); - secondInstance = new mosca.Server(settings, cb); + secondInstance = new mosca.Server(settings, function() { + cb(); + }); }, function(cb) { @@ -737,7 +754,9 @@ describe("mosca.Server", function() { type: "mqtt" }; settings.port = settings.port + 1000; - secondInstance = new mosca.Server(settings, cb); + secondInstance = new mosca.Server(settings, function() { + cb(); + }); }, function(cb) { @@ -756,61 +775,26 @@ describe("mosca.Server", function() { ]); }); - it("should support specifying an Ascoltatore instead of backend options in a tree-based topology", function(done) { - var d = donner(2, done); + it("should support specifying an Ascoltatore instead of backend options", function(done) { async.waterfall([ - function(cb) { - buildAndConnect(d, function(client1) { - cb(null, client1); - }); - }, - - function(client1, cb) { - client1.on("publish", function(packet) { - expect(packet.payload).to.be.eql("some data"); - client1.disconnect(); - }); - - var subscriptions = [{ - topic: "hello/#", - qos: 0 - } - ]; - - client1.subscribe({ - subscriptions: subscriptions, - messageId: 42 - }); - client1.on("suback", function() { - cb(null); - }); - }, - function(cb) { settings.ascoltatore = ascoltatori.build({ - port: settings.port, - type: "mqtt", json: false }); settings.port = settings.port + 1000; - secondInstance = new mosca.Server(settings, cb); + mosca.Server(settings, cb); }, - function(cb) { - buildAndConnect(d, function(client2) { - cb(null, client2); - }); - }, - - function(client2, cb) { - client2.publish({ - topic: "hello/world", - payload: "some data" + function(server, cb) { + secondInstance = server; + buildAndConnect(done, function(client) { + client.disconnect(); + cb(null); }); - client2.disconnect(); } + ]); });