Skip to content

Commit 9d741d0

Browse files
committed
ensure stream api also conforms to reloadDelay and reloadDebounce
1 parent a1fbcf0 commit 9d741d0

5 files changed

Lines changed: 69 additions & 29 deletions

File tree

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/file-event-handler.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ module.exports = function (subject, options) {
2121
return subject;
2222
})();
2323

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);
24+
var withOps = applyOperators(globalItems, initial, options, scheduler);
3125

3226
return withOps
3327
.map(function(xs) {
@@ -48,6 +42,38 @@ module.exports = function (subject, options) {
4842
});
4943
};
5044

45+
function applyReloadOperators (subject, options) {
46+
var globalItems = [
47+
{
48+
option: "reloadDebounce",
49+
fnName: "debounce"
50+
},
51+
{
52+
option: "reloadThrottle",
53+
fnName: "throttle"
54+
},
55+
{
56+
option: "reloadDelay",
57+
fnName: "delay"
58+
}
59+
];
60+
61+
var scheduler = options.getIn(["debug", "scheduler"]);
62+
63+
return applyOperators(globalItems, subject, options, scheduler);
64+
}
65+
module.exports.applyReloadOperators = applyReloadOperators;
66+
67+
function applyOperators (items, subject, options, scheduler) {
68+
return items.reduce(function(subject, item) {
69+
var value = options.get(item.option);
70+
if (value > 0) {
71+
return subject[item.fnName].call(subject, value, scheduler);
72+
}
73+
return subject;
74+
}, subject);
75+
}
76+
5177
function getDebouncedStream (subject, options, scheduler) {
5278
return subject
5379
.filter(function(x) { return options.get("watchEvents").indexOf(x.event) > -1 })

lib/internal-events.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var utils = require("./utils");
44
var fileUtils = require("./file-utils");
55
var Rx = require("rx");
6+
var fromEvent = Rx.Observable.fromEvent;
67
var fileHandler = require("./file-event-handler");
78

89
module.exports = function (bs) {
@@ -79,14 +80,12 @@ module.exports = function (bs) {
7980
bs.events.on(event, events[event]);
8081
});
8182

82-
var reloader = Rx.Observable
83-
.fromEvent(bs.events, "_browser:reload")
84-
.delay(bs.options.get("reloadDelay") || 0, bs.options.getIn(["debug", "scheduler"]))
83+
var reloader = fileHandler.applyReloadOperators(fromEvent(bs.events, "_browser:reload"), bs.options)
8584
.subscribe(function() {
8685
bs.events.emit("browser:reload");
8786
});
8887

89-
var handler = fileHandler(Rx.Observable.fromEvent(bs.events, "file:changed"), bs.options)
88+
var handler = fileHandler(fromEvent(bs.events, "file:changed"), bs.options)
9089
.subscribe(function (x) {
9190
if (x.type === "reload") {
9291
bs.events.emit("browser:reload");

test/specs/api/init.reload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe("API: .reload()", function () {
9898
stream.write(new File({path: "styles2.css"}));
9999
stream.write(new File({path: "styles3.css"}));
100100
stream.end();
101-
sinon.assert.calledOnce(emitterStub);
102101
sinon.assert.calledWithExactly(emitterStub, "_browser:reload");
102+
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
103103
});
104104
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ describe("API: .stream()", function () {
7474
stream.write(new File({path: "styles2.css"}));
7575
stream.write(new File({path: "styles3.css"}));
7676
stream.end();
77-
sinon.assert.calledOnce(emitterStub);
7877
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});
8282
stream.write(new File({path: "styles.js"}));
8383
stream.write(new File({path: "styles2.js"}));
8484
stream.write(new File({path: "styles3.js"}));
8585
stream.end();
86-
sinon.assert.calledOnce(emitterStub);
8786
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"});

0 commit comments

Comments
 (0)