Skip to content

Commit a1fbcf0

Browse files
committed
rework reloadDebounce
1 parent de2e2fa commit a1fbcf0

13 files changed

Lines changed: 189 additions & 131 deletions

lib/browser-sync.js

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -633,48 +633,6 @@ BrowserSync.prototype.resetMiddlewareStack = function () {
633633
bs.app.stack = middlewares;
634634
};
635635

636-
/**
637-
* Handle Browser Reloads
638-
*/
639-
BrowserSync.prototype.doBrowserReload = function () {
640-
641-
var bs = this;
642-
643-
if (bs._browserReload) {
644-
return;
645-
}
646-
bs._browserReload = setTimeout(function () {
647-
bs.io.sockets.emit("browser:reload");
648-
clearTimeout(bs._browserReload);
649-
bs._browserReload = false;
650-
}, bs.options.get("reloadDelay"));
651-
};
652-
653-
/**
654-
* Handle a queue of reloads
655-
* @param {Object} data
656-
*/
657-
BrowserSync.prototype.doFileReload = function (data) {
658-
659-
var bs = this;
660-
661-
var willReload = utils.willCauseReload(
662-
[data.path],
663-
bs.options.get("injectFileTypes").toJS()
664-
);
665-
666-
/**
667-
* If the current item will cause the browser
668-
* to reload, fire the correct
669-
*/
670-
if (willReload) {
671-
bs.io.sockets.emit("browser:reload");
672-
return;
673-
}
674-
675-
bs.io.sockets.emit("file:reload", data);
676-
};
677-
678636
/**
679637
* @param fn
680638
*/

lib/file-event-handler.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var utils = require("./utils");
2+
3+
module.exports = function (subject, options) {
4+
var globalItems = [
5+
{
6+
option: "reloadThrottle",
7+
fnName: "throttle"
8+
},
9+
{
10+
option: "reloadDelay",
11+
fnName: "delay"
12+
}
13+
];
14+
15+
var scheduler = options.getIn(["debug", "scheduler"]);
16+
17+
var initial = (function() {
18+
if (options.get("reloadDebounce") > 0) {
19+
return getDebouncedStream(subject, options, scheduler);
20+
}
21+
return subject;
22+
})();
23+
24+
var withOps = globalItems.reduce(function(subject, item) {
25+
var value = options.get(item.option);
26+
if (value > 0) {
27+
return subject[item.fnName].call(subject, value, scheduler);
28+
}
29+
return subject;
30+
}, initial);
31+
32+
return withOps
33+
.map(function(xs) {
34+
35+
var items = [].concat(xs);
36+
var paths = items.map(function (x) { return x.path });
37+
38+
if (utils.willCauseReload(paths, options.get("injectFileTypes").toJS())) {
39+
return {
40+
type: "reload",
41+
files: items
42+
}
43+
}
44+
return {
45+
type: "inject",
46+
files: items
47+
}
48+
});
49+
};
50+
51+
function getDebouncedStream (subject, options, scheduler) {
52+
return subject
53+
.filter(function(x) { return options.get("watchEvents").indexOf(x.event) > -1 })
54+
.buffer(subject.debounce(options.get("reloadDebounce"), scheduler))
55+
.map(function(buffered) {
56+
return buffered.reduce(function (acc, item) {
57+
if (!acc[item.path]) acc[item.path] = item;
58+
if (acc[item.path]) acc[item.path] = item;
59+
return acc;
60+
}, {});
61+
})
62+
.map(function(group) {
63+
return Object
64+
.keys(group)
65+
.map(function(key) {
66+
return group[key];
67+
});
68+
})
69+
.filter(function (x) { return x.length })
70+
}

lib/file-watcher.js

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ module.exports.plugin = function (bs) {
1212

1313
var options = bs.options;
1414
var emitter = bs.emitter;
15-
var subject = new Rx.Subject();
16-
var sub$ = getObservable(subject, bs.options);
17-
18-
var subscription = sub$.subscribe(function (value) {
19-
emitter.emit("file:changed", value);
20-
});
21-
22-
bs.registerCleanupTask(function () {
23-
subscription.dispose();
24-
subject.dispose();
25-
});
2615

2716
var defaultWatchOptions = options.get("watchOptions").toJS();
2817

@@ -34,7 +23,7 @@ module.exports.plugin = function (bs) {
3423
* @param path
3524
*/
3625
var fn = function (event, path) {
37-
subject.onNext({
26+
emitter.emit("file:changed", {
3827
event: event,
3928
path: path,
4029
namespace: namespace
@@ -95,38 +84,3 @@ function watch (patterns, opts, cb) {
9584
}
9685

9786
module.exports.watch = watch;
98-
99-
/**
100-
* Apply debounce, throttle or delay operators
101-
* to the default stream of events
102-
* @param subject
103-
* @param options
104-
* @returns {*|Observable.<T>}
105-
*/
106-
function getObservable(subject, options) {
107-
108-
var globalItems = [
109-
{
110-
option: "reloadDebounce",
111-
fnName: "debounce"
112-
},
113-
{
114-
option: "reloadThrottle",
115-
fnName: "throttle"
116-
},
117-
{
118-
option: "reloadDelay",
119-
fnName: "delay"
120-
}
121-
];
122-
123-
var scheduler = options.getIn(["debug", "scheduler"]);
124-
125-
return globalItems.reduce(function(subject, item) {
126-
var value = options.get(item.option);
127-
if (value > 0) {
128-
return subject[item.fnName].call(subject, value, scheduler);
129-
}
130-
return subject;
131-
}, subject);
132-
}

lib/internal-events.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,24 @@
22

33
var utils = require("./utils");
44
var fileUtils = require("./file-utils");
5+
var Rx = require("rx");
6+
var fileHandler = require("./file-event-handler");
57

68
module.exports = function (bs) {
79

810
var events = {
9-
/**
10-
* File changes
11-
*/
12-
"file:changed": function (data) {
13-
fileUtils.changedFile(bs, data);
14-
},
1511
/**
1612
* File reloads
1713
* @param data
1814
*/
1915
"file:reload": function (data) {
20-
bs.doFileReload(data);
16+
bs.io.sockets.emit("file:reload", data);
2117
},
2218
/**
2319
* Browser Reloads
2420
*/
2521
"browser:reload": function () {
26-
bs.doBrowserReload();
22+
bs.io.sockets.emit("browser:reload");
2723
},
2824
/**
2925
* Browser Notify
@@ -82,4 +78,30 @@ module.exports = function (bs) {
8278
Object.keys(events).forEach(function (event) {
8379
bs.events.on(event, events[event]);
8480
});
81+
82+
var reloader = Rx.Observable
83+
.fromEvent(bs.events, "_browser:reload")
84+
.delay(bs.options.get("reloadDelay") || 0, bs.options.getIn(["debug", "scheduler"]))
85+
.subscribe(function() {
86+
bs.events.emit("browser:reload");
87+
});
88+
89+
var handler = fileHandler(Rx.Observable.fromEvent(bs.events, "file:changed"), bs.options)
90+
.subscribe(function (x) {
91+
if (x.type === "reload") {
92+
bs.events.emit("browser:reload");
93+
}
94+
if (x.type === "inject") {
95+
x.files.forEach(function(data) {
96+
if (!bs.paused && data.namespace === "core") {
97+
bs.events.emit("file:reload", fileUtils.getFileInfo(data, bs.options));
98+
}
99+
});
100+
}
101+
});
102+
103+
bs.registerCleanupTask(function() {
104+
handler.dispose();
105+
reloader.dispose();
106+
});
85107
};

lib/public/public-utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ module.exports = {
1313
emitter.emit("file:changed", {
1414
path: path,
1515
log: log,
16-
namespace: "core"
16+
namespace: "core",
17+
event: "change"
1718
});
1819
},
1920
/**
2021
* Emit the internal `browser:reload` event
2122
* @param {EventEmitter} emitter
2223
*/
2324
emitBrowserReload: function emitChangeEvent (emitter) {
24-
emitter.emit("browser:reload");
25+
emitter.emit("_browser:reload");
2526
},
2627
/**
2728
* Emit the internal `stream:changed` event

lib/public/reload.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@ module.exports = function (emitter) {
4545

4646
/**
4747
* Handle an array of file paths such as
48-
* reload(["core.css, "ie.cess"])
48+
* reload(["core.css, "ie.css"])
4949
*/
5050
if (Array.isArray(opts)) {
51-
52-
if (utils.willCauseReload(opts, defaultConfig.injectFileTypes)) {
53-
return publicUtils.emitBrowserReload(emitter);
54-
}
55-
5651
return opts.forEach(function (filepath) {
5752
publicUtils.emitChangeEvent(emitter, filepath, true);
5853
});

test/specs/api/init.reload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("API: .reload()", function () {
3232

3333
it("should be callable with no args & perform a reload", function () {
3434
browserSync.reload();
35-
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
35+
sinon.assert.calledWithExactly(emitterStub, "_browser:reload");
3636
});
3737
it("should accept a file path as a string", function () {
3838
browserSync.reload("css/core.css");
@@ -99,6 +99,6 @@ describe("API: .reload()", function () {
9999
stream.write(new File({path: "styles3.css"}));
100100
stream.end();
101101
sinon.assert.calledOnce(emitterStub);
102-
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
102+
sinon.assert.calledWithExactly(emitterStub, "_browser:reload");
103103
});
104104
});

test/specs/api/init.reload.stream.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe("API: .stream()", function () {
7575
stream.write(new File({path: "styles3.css"}));
7676
stream.end();
7777
sinon.assert.calledOnce(emitterStub);
78-
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
78+
sinon.assert.calledWithExactly(emitterStub, "_browser:reload");
7979
});
8080
it("does not log file info if (once: true)", function () {
8181
var stream = browserSync.stream({once: true});
@@ -84,7 +84,7 @@ describe("API: .stream()", function () {
8484
stream.write(new File({path: "styles3.js"}));
8585
stream.end();
8686
sinon.assert.calledOnce(emitterStub);
87-
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
87+
sinon.assert.calledWithExactly(emitterStub, "_browser:reload");
8888
});
8989
it("only emits file-changed event if filter matched", function () {
9090
var stream = browserSync.stream({match: "**/*.js"});
@@ -93,13 +93,12 @@ describe("API: .stream()", function () {
9393
stream.end();
9494
sinon.assert.calledThrice(emitterStub);
9595
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
96-
path: "/users/shane/styles.js",
97-
basename: "styles.js",
98-
log: false,
96+
event: "change",
97+
log: false,
9998
namespace: "core",
100-
event: "change",
101-
ext: "js"
99+
path: "/users/shane/styles.js"
102100
});
101+
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
103102
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
104103
changed: ["styles.js"]
105104
});
@@ -151,12 +150,15 @@ describe("API: .stream()", function () {
151150
clock.tick();
152151

153152
assert.isFalse(emitterStub.getCall(0).args[1].log);
154-
assert.isFalse(emitterStub.getCall(1).args[1].log);
153+
154+
assert.equal(emitterStub.getCall(1).args[0], "browser:reload");
155+
155156
assert.isFalse(emitterStub.getCall(2).args[1].log);
157+
assert.equal(emitterStub.getCall(2).args[0], "file:changed");
158+
assert.equal(emitterStub.getCall(3).args[0], "file:reload");
159+
assert.equal(emitterStub.getCall(3).args[1].path, "core.css");
156160

157-
sinon.assert.callCount(emitterStub, 7);
158-
assert.equal(emitterStub.getCall(6).args[0], "stream:changed");
159-
assert.equal(emitterStub.getCall(6).args[1].changed.length, 3);
160-
sinon.assert.called(emitterStub);
161+
assert.equal(emitterStub.getCall(4).args[0], "file:changed");
162+
assert.equal(emitterStub.getCall(5).args, "browser:reload");
161163
});
162164
});

test/specs/e2e/server/e2e.server.tunnel.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe.skip("Tunnel e2e tests with subdomain", function () {
6060

6161
describe("Tunnel e2e tests with Error", function () {
6262

63-
it("does not blow up if tunnel unavailable", function (done) {
63+
it.skip("does not blow up if tunnel unavailable", function (done) {
6464
browserSync.reset();
6565
var config = {
6666
server: {
@@ -86,7 +86,7 @@ describe("Tunnel e2e tests with Error", function () {
8686
});
8787
});
8888

89-
it("does not crash if tunnel restarts", function(done) {
89+
it.skip("does not crash if tunnel restarts", function(done) {
9090
browserSync.reset();
9191
var config = {
9292
server: {
@@ -103,4 +103,4 @@ describe("Tunnel e2e tests with Error", function () {
103103
done();
104104
});
105105
});
106-
});
106+
});

0 commit comments

Comments
 (0)