Skip to content

Commit

Permalink
refactor public reload/stream methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane authored and Shane Osbourne committed Apr 9, 2015
1 parent 66367c6 commit 653050b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 54 deletions.
33 changes: 33 additions & 0 deletions lib/public/public-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";

module.exports = {
/**
* Emit the internal `file:change` event
* @param {EventEmitter} emitter
* @param {string} path
* @param {boolean} [log]
*/
emitChangeEvent: function emitChangeEvent (emitter, path, log) {
emitter.emit("file:changed", {
path: path,
log: log,
namespace: "core",
event: "change"
});
},
/**
* Emit the internal `browser:reload` event
* @param {EventEmitter} emitter
*/
emitBrowserReload: function emitChangeEvent (emitter) {
emitter.emit("browser:reload");
},
/**
* Emit the internal `stream:changed` event
* @param {EventEmitter} emitter
* @param {Array} changed
*/
emitStreamChangedEvent: function (emitter, changed) {
emitter.emit("stream:changed", {changed: changed});
}
};
45 changes: 20 additions & 25 deletions lib/public/reload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var utils = require("../utils");
var publicUtils = require("./public-utils");
var _ = require("lodash");
var defaultConfig = require("../default-config");
var stream = require("./stream");
Expand All @@ -11,20 +12,12 @@ var stream = require("./stream");
*/
module.exports = function (emitter) {

function emitReload(path, log) {
emitter.emit("file:changed", {
path: path,
log: log,
namespace: "core",
event: "change"
});
}

function emitBrowserReload() {
emitter.emit("browser:reload");
}

return function (arg) {
/**
* Inform browsers about file changes.
*
* eg: reload("core.css")
*/
function browserSyncReload (opts) {

/**
* BACKWARDS COMPATIBILITY:
Expand All @@ -34,10 +27,10 @@ module.exports = function (emitter) {
* for that signature here and defer to the
* dedicated `.stream()` method instead.
*/
if (_.isObject(arg)) {
if (!Array.isArray(arg) && Object.keys(arg).length) {
if (arg.stream === true) {
return stream(emitter)(arg);
if (_.isObject(opts)) {
if (!Array.isArray(opts) && Object.keys(opts).length) {
if (opts.stream === true) {
return stream(emitter)(opts);
}
}
}
Expand All @@ -46,17 +39,19 @@ module.exports = function (emitter) {
return emitReload(arg, true);
}

if (Array.isArray(arg)) {
if (Array.isArray(opts)) {

if (utils.willCauseReload(arg, defaultConfig.injectFileTypes)) {
return emitBrowserReload();
if (utils.willCauseReload(opts, defaultConfig.injectFileTypes)) {
return publicUtils.emitBrowserReload(emitter);
}

return arg.forEach(function (filepath) {
emitReload(filepath, true);
return opts.forEach(function (filepath) {
publicUtils.emitChangeEvent(emitter, filepath, true);
});
}

return emitBrowserReload();
};
return publicUtils.emitBrowserReload(emitter);
}

return browserSyncReload;
};
74 changes: 49 additions & 25 deletions lib/public/stream.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
"use strict";

var path = require("path");
var path = require("path");
var anymatch = require("anymatch");
var utils = require("./public-utils");

/**
* @param emitter
* @returns {Function}
*/
module.exports = function (emitter) {

function emitReload(path, log) {
emitter.emit("file:changed", {
path: path,
log: log,
namespace: "core"
});
}

function emitBrowserReload() {
emitter.emit("browser:reload");
}

function emitInfo(changed) {
emitter.emit("stream:changed", {changed: changed});
}

return function (opts) {
/**
* Return a transform/through stream that listens to file
* paths and fires internal BrowserSync events.
* @param {{once: boolean, match: string|array}} [opts]
* @returns {Stream.Transform}
*/
function browserSyncThroughStream (opts) {

opts = opts || {};
var emitted = false;
Expand All @@ -34,9 +26,35 @@ module.exports = function (emitter) {

reload._transform = function (file, encoding, next) {

var stream = this;

/**
* End is always called to send the current file down
* stream. BrowserSync never acts upon a stream,
* we only `listen` to it.
*/
function end () {
stream.push(file); // always send the file down-stream
next();
}

/**
* If {match: <pattern>} was provided, test the
* current filepath against it
*/
if (opts.match) {
if (!anymatch(opts.match, file.path)) {
return end();
}
}

/**
* if {once: true} provided, emit the reload event for the
* first file only
*/
if (opts.once === true && !emitted) {

emitBrowserReload();
utils.emitBrowserReload(emitter);

emitted = true;

Expand All @@ -49,26 +67,32 @@ module.exports = function (emitter) {
if (file.path) {

emitted = true;
emitReload(file.path, false);
utils.emitChangeEvent(emitter, file.path, false);
changed.push(path.basename(file.path));
}
}
}

this.push(file); // always send the file down-stream

next();
end();
};

/**
* When this current operation has finished, emit the
* steam:changed event so that any loggers can pick up it
* @param next
* @private
*/
reload._flush = function (next) {

if (changed.length) {
emitInfo(changed);
utils.emitStreamChangedEvent(emitter, changed);
}

next();
};

return reload;
};
}

return browserSyncThroughStream;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"pre-release": "npm test && npm run pro-local && npm run pro"
},
"dependencies": {
"anymatch": "^1.1.0",
"async-each-series": "^0.1.1",
"browser-sync-client": "^1.0.1",
"browser-sync-ui": "^0.5.2",
Expand Down
33 changes: 30 additions & 3 deletions test/specs/api/init.reload.stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe("API: .stream()", function () {
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles.css",
log: false,
namespace: "core"
namespace: "core",
event: "change"
});
});
it("should accept multiple files in stream", function () {
Expand All @@ -47,12 +48,14 @@ describe("API: .stream()", function () {
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles.css",
log: false,
namespace: "core"
namespace: "core",
event: "change"
});
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles2.css",
log: false,
namespace: "core"
namespace: "core",
event: "change"
});
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
changed: ["styles.css", "styles2.css"]
Expand All @@ -76,4 +79,28 @@ describe("API: .stream()", function () {
sinon.assert.calledOnce(emitterStub);
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
});
it("only emits file-changed event if filter matched", function () {
var stream = browserSync.stream({match: "**/*.js"});
stream.write(new File({path: "/users/shane/styles.js"}));
stream.write(new File({path: "core.css"}));
stream.end();
sinon.assert.calledThrice(emitterStub);
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "/users/shane/styles.js",
log: false,
namespace: "core",
event: "change"
});
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
changed: ["styles.js"]
});
});
it("only emits file-changed event if filter returns no results", function () {
var stream = browserSync.stream({match: "**/*.md"});
stream.write(new File({path: "/users/shane/styles.js"}));
stream.write(new File({path: "core.css"}));
stream.write(new File({path: "index.html"}));
stream.end();
sinon.assert.notCalled(emitterStub);
});
});
3 changes: 2 additions & 1 deletion test/specs/api/init.reload.stream.noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe("API: .stream() noop", function () {
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles.css",
log: false,
namespace: "core"
namespace: "core",
event: "change"
});
done();
});
Expand Down

0 comments on commit 653050b

Please sign in to comment.