Skip to content

Commit 5b00707

Browse files
committed
Merge branch 'reload-debounce'
2 parents de2e2fa + 4b14c3b commit 5b00707

File tree

14 files changed

+268
-146
lines changed

14 files changed

+268
-146
lines changed

gulpfile.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
"use strict";
22

33
var gulp = require("gulp");
4-
var contribs = require("gulp-contribs");
5-
var conventionalChangelog = require("gulp-conventional-changelog");
4+
var bs = require("./").create();
5+
var Rx = require("rx");
6+
// var contribs = require("gulp-contribs");
7+
// var conventionalChangelog = require("gulp-conventional-changelog");
8+
//
9+
// gulp.task("contribs", function () {
10+
// gulp.src("README.md")
11+
// .pipe(contribs())
12+
// .pipe(gulp.dest(""));
13+
// });
14+
//
15+
// gulp.task("changelog", function () {
16+
// return gulp.src("CHANGELOG.md", {
17+
// buffer: false
18+
// })
19+
// .pipe(conventionalChangelog({
20+
// preset: "angular"
21+
// }))
22+
// .pipe(gulp.dest("./"));
23+
// });
624

7-
gulp.task("contribs", function () {
8-
gulp.src("README.md")
9-
.pipe(contribs())
10-
.pipe(gulp.dest(""));
11-
});
25+
gulp.task("dev", function() {
26+
27+
bs.init({
28+
server: "test/fixtures",
29+
open: false,
30+
reloadDelay: 1000,
31+
reloadDebounce: 1000
32+
});
1233

13-
gulp.task("changelog", function () {
14-
return gulp.src("CHANGELOG.md", {
15-
buffer: false
16-
})
17-
.pipe(conventionalChangelog({
18-
preset: "angular"
19-
}))
20-
.pipe(gulp.dest("./"));
34+
gulp.src("lib/**")
35+
.pipe(bs.stream())
2136
});

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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
var utils = require("./utils");
2+
3+
/**
4+
* Apply the operators that apply to the 'file:changed' event
5+
* @param {Rx.Observable} subject
6+
* @param options
7+
* @return {Rx.Observable<{type: string, files: Array<any>}>}
8+
*/
9+
function fileChanges(subject, options) {
10+
var operators = [
11+
{
12+
option: "reloadThrottle",
13+
fnName: "throttle"
14+
},
15+
{
16+
option: "reloadDelay",
17+
fnName: "delay"
18+
}
19+
];
20+
21+
var scheduler = options.getIn(["debug", "scheduler"]);
22+
23+
/**
24+
* if the 'reloadDebounce' option was provided, create
25+
* a stream buffered/debounced stream of events
26+
*/
27+
var initial = (function() {
28+
if (options.get("reloadDebounce") > 0) {
29+
return getAggregatedDebouncedStream(subject, options, scheduler);
30+
}
31+
return subject;
32+
})();
33+
34+
return applyOperators(operators, initial, options, scheduler)
35+
.map(function(xs) {
36+
37+
var items = [].concat(xs);
38+
var paths = items.map(function (x) { return x.path });
39+
40+
if (utils.willCauseReload(paths, options.get("injectFileTypes").toJS())) {
41+
return {
42+
type: "reload",
43+
files: items
44+
}
45+
}
46+
return {
47+
type: "inject",
48+
files: items
49+
}
50+
});
51+
}
52+
module.exports.fileChanges = fileChanges;
53+
54+
/**
55+
* Apply the operators that apply to the 'browser:reload' event
56+
* @param {Rx.Observable} subject
57+
* @param options
58+
* @returns {Rx.Observable}
59+
*/
60+
function applyReloadOperators (subject, options) {
61+
var operators = [
62+
{
63+
option: "reloadDebounce",
64+
fnName: "debounce"
65+
},
66+
{
67+
option: "reloadThrottle",
68+
fnName: "throttle"
69+
},
70+
{
71+
option: "reloadDelay",
72+
fnName: "delay"
73+
}
74+
];
75+
76+
return applyOperators(operators, subject, options, options.getIn(["debug", "scheduler"]));
77+
}
78+
module.exports.applyReloadOperators = applyReloadOperators;
79+
80+
/**
81+
* @param items
82+
* @param subject
83+
* @param options
84+
* @param scheduler
85+
*/
86+
function applyOperators (items, subject, options, scheduler) {
87+
return items.reduce(function(subject, item) {
88+
var value = options.get(item.option);
89+
if (value > 0) {
90+
return subject[item.fnName].call(subject, value, scheduler);
91+
}
92+
return subject;
93+
}, subject);
94+
}
95+
96+
/**
97+
* @param subject
98+
* @param options
99+
* @param scheduler
100+
*/
101+
function getAggregatedDebouncedStream (subject, options, scheduler) {
102+
return subject
103+
.filter(function(x) { return options.get("watchEvents").indexOf(x.event) > -1 })
104+
.buffer(subject.debounce(options.get("reloadDebounce"), scheduler))
105+
.map(function(buffered) {
106+
return buffered.reduce(function (acc, item) {
107+
if (!acc[item.path]) acc[item.path] = item;
108+
if (acc[item.path]) acc[item.path] = item;
109+
return acc;
110+
}, {});
111+
})
112+
.map(function(group) {
113+
return Object
114+
.keys(group)
115+
.map(function(key) {
116+
return group[key];
117+
});
118+
})
119+
.filter(function (x) { return x.length })
120+
}

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: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22

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

69
module.exports = function (bs) {
710

811
var events = {
9-
/**
10-
* File changes
11-
*/
12-
"file:changed": function (data) {
13-
fileUtils.changedFile(bs, data);
14-
},
1512
/**
1613
* File reloads
1714
* @param data
1815
*/
1916
"file:reload": function (data) {
20-
bs.doFileReload(data);
17+
bs.io.sockets.emit("file:reload", data);
2118
},
2219
/**
2320
* Browser Reloads
2421
*/
2522
"browser:reload": function () {
26-
bs.doBrowserReload();
23+
bs.io.sockets.emit("browser:reload");
2724
},
2825
/**
2926
* Browser Notify
@@ -82,4 +79,28 @@ module.exports = function (bs) {
8279
Object.keys(events).forEach(function (event) {
8380
bs.events.on(event, events[event]);
8481
});
82+
83+
var reloader = fileHandler.applyReloadOperators(fromEvent(bs.events, "_browser:reload"), bs.options)
84+
.subscribe(function() {
85+
bs.events.emit("browser:reload");
86+
});
87+
88+
var handler = fileHandler.fileChanges(fromEvent(bs.events, "file:changed"), bs.options)
89+
.subscribe(function (x) {
90+
if (x.type === "reload") {
91+
bs.events.emit("browser:reload");
92+
}
93+
if (x.type === "inject") {
94+
x.files.forEach(function(data) {
95+
if (!bs.paused && data.namespace === "core") {
96+
bs.events.emit("file:reload", fileUtils.getFileInfo(data, bs.options));
97+
}
98+
});
99+
}
100+
});
101+
102+
bs.registerCleanupTask(function() {
103+
handler.dispose();
104+
reloader.dispose();
105+
});
85106
};

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
});

0 commit comments

Comments
 (0)