Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Commit fd6ce60

Browse files
committed
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.
1 parent 7f085a2 commit fd6ce60

File tree

2 files changed

+37
-46
lines changed

2 files changed

+37
-46
lines changed

lib/server.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ var defaults = {
5959
* @api public
6060
*/
6161
function Server(opts, callback) {
62+
63+
if (!(this instanceof Server)) {
64+
return new Server(opts, callback);
65+
}
66+
6267
EventEmitter.call(this);
6368

6469
this.opts = extend(true, {}, defaults, opts);
@@ -83,7 +88,9 @@ function Server(opts, callback) {
8388
this.ascoltatore = opts.ascoltatore || ascoltatori.build(this.opts.backend);
8489
this.ascoltatore.on("error", this.emit.bind(this));
8590

86-
that.once("ready", callback);
91+
that.once("ready", function() {
92+
callback(null, that);
93+
});
8794

8895
async.series([
8996
function(cb) {

test/server_spec.js

+29-45
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ describe("mosca.Server", function() {
7979
});
8080
}
8181

82+
it("should pass itself in the callback", function(done) {
83+
secondInstance = new mosca.Server(moscaSettings(), function(err, server) {
84+
expect(server === secondInstance).to.be.true;
85+
done();
86+
});
87+
});
88+
89+
it("should allow to be called like a function", function(done) {
90+
var func = mosca.Server;
91+
secondInstance = func(moscaSettings(), function(err, server) {
92+
expect(server === secondInstance).to.be.true;
93+
done();
94+
});
95+
});
96+
8297
it("should support connecting and disconnecting", function(done) {
8398
buildClient(done, function(client) {
8499

@@ -626,7 +641,9 @@ describe("mosca.Server", function() {
626641
type: "mqtt"
627642
};
628643
settings.port = nextPort();
629-
secondInstance = new mosca.Server(settings, cb);
644+
secondInstance = new mosca.Server(settings, function() {
645+
cb();
646+
});
630647
},
631648

632649
function(cb) {
@@ -737,7 +754,9 @@ describe("mosca.Server", function() {
737754
type: "mqtt"
738755
};
739756
settings.port = settings.port + 1000;
740-
secondInstance = new mosca.Server(settings, cb);
757+
secondInstance = new mosca.Server(settings, function() {
758+
cb();
759+
});
741760
},
742761

743762
function(cb) {
@@ -756,61 +775,26 @@ describe("mosca.Server", function() {
756775
]);
757776
});
758777

759-
it("should support specifying an Ascoltatore instead of backend options in a tree-based topology", function(done) {
760-
var d = donner(2, done);
778+
it("should support specifying an Ascoltatore instead of backend options", function(done) {
761779

762780
async.waterfall([
763781

764-
function(cb) {
765-
buildAndConnect(d, function(client1) {
766-
cb(null, client1);
767-
});
768-
},
769-
770-
function(client1, cb) {
771-
client1.on("publish", function(packet) {
772-
expect(packet.payload).to.be.eql("some data");
773-
client1.disconnect();
774-
});
775-
776-
var subscriptions = [{
777-
topic: "hello/#",
778-
qos: 0
779-
}
780-
];
781-
782-
client1.subscribe({
783-
subscriptions: subscriptions,
784-
messageId: 42
785-
});
786-
client1.on("suback", function() {
787-
cb(null);
788-
});
789-
},
790-
791782
function(cb) {
792783
settings.ascoltatore = ascoltatori.build({
793-
port: settings.port,
794-
type: "mqtt",
795784
json: false
796785
});
797786
settings.port = settings.port + 1000;
798-
secondInstance = new mosca.Server(settings, cb);
787+
mosca.Server(settings, cb);
799788
},
800789

801-
function(cb) {
802-
buildAndConnect(d, function(client2) {
803-
cb(null, client2);
804-
});
805-
},
806-
807-
function(client2, cb) {
808-
client2.publish({
809-
topic: "hello/world",
810-
payload: "some data"
790+
function(server, cb) {
791+
secondInstance = server;
792+
buildAndConnect(done, function(client) {
793+
client.disconnect();
794+
cb(null);
811795
});
812-
client2.disconnect();
813796
}
797+
814798
]);
815799
});
816800

0 commit comments

Comments
 (0)