Skip to content

Commit 985682c

Browse files
author
Shane Osbourne
committed
feat(api): expose sockets to public api
1 parent c32bec6 commit 985682c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

index.js

+18
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ function create(name, emitter) {
315315
}
316316
});
317317

318+
/**
319+
* Access to client-side socket for emitting events
320+
*
321+
* @property sockets
322+
*/
323+
Object.defineProperty(instance, "sockets", {
324+
get: function () {
325+
if (!browserSync.active) {
326+
return {
327+
emit: function () {},
328+
on: function () {}
329+
};
330+
} else {
331+
return browserSync.io.sockets;
332+
}
333+
}
334+
});
335+
318336
instances.push(instance);
319337

320338
return instance;

test/specs/api/init.sockets.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
3+
var browserSync = require("../../../");
4+
5+
var assert = require("chai").assert;
6+
7+
describe("API: .sockets", function () {
8+
9+
it("has access before Browsersync is running via stubs", function (done) {
10+
browserSync.reset();
11+
var bs = browserSync.create();
12+
bs.init({
13+
logLevel: "silent"
14+
}, function (err, bs) {
15+
bs.cleanup();
16+
done();
17+
});
18+
assert.isFunction(bs.sockets.on);
19+
assert.isFunction(bs.sockets.emit);
20+
});
21+
it("has access after Browsersync is running", function (done) {
22+
browserSync.reset();
23+
var bs = browserSync.create();
24+
bs.init({
25+
logLevel: "silent"
26+
}, function (err, _bs) {
27+
assert.isFunction(bs.sockets.emit);
28+
assert.isFunction(bs.sockets.on);
29+
_bs.cleanup();
30+
done();
31+
});
32+
});
33+
it("has access before Browsersync is running via main module export + stubs", function (done) {
34+
browserSync.reset();
35+
var bs = browserSync({
36+
logLevel: "silent"
37+
}, function (err, bs) {
38+
bs.cleanup();
39+
done();
40+
});
41+
assert.isFunction(bs.sockets.on);
42+
assert.isFunction(bs.sockets.emit);
43+
});
44+
});

0 commit comments

Comments
 (0)