Skip to content

Commit

Permalink
feat(api): expose sockets to public api
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed Jun 17, 2015
1 parent c32bec6 commit 985682c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,24 @@ function create(name, emitter) {
}
});

/**
* Access to client-side socket for emitting events
*
* @property sockets
*/
Object.defineProperty(instance, "sockets", {
get: function () {
if (!browserSync.active) {
return {
emit: function () {},
on: function () {}
};
} else {
return browserSync.io.sockets;
}
}
});

instances.push(instance);

return instance;
Expand Down
44 changes: 44 additions & 0 deletions test/specs/api/init.sockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";

var browserSync = require("../../../");

var assert = require("chai").assert;

describe("API: .sockets", function () {

it("has access before Browsersync is running via stubs", function (done) {
browserSync.reset();
var bs = browserSync.create();
bs.init({
logLevel: "silent"
}, function (err, bs) {
bs.cleanup();
done();
});
assert.isFunction(bs.sockets.on);
assert.isFunction(bs.sockets.emit);
});
it("has access after Browsersync is running", function (done) {
browserSync.reset();
var bs = browserSync.create();
bs.init({
logLevel: "silent"
}, function (err, _bs) {
assert.isFunction(bs.sockets.emit);
assert.isFunction(bs.sockets.on);
_bs.cleanup();
done();
});
});
it("has access before Browsersync is running via main module export + stubs", function (done) {
browserSync.reset();
var bs = browserSync({
logLevel: "silent"
}, function (err, bs) {
bs.cleanup();
done();
});
assert.isFunction(bs.sockets.on);
assert.isFunction(bs.sockets.emit);
});
});

0 comments on commit 985682c

Please sign in to comment.