Skip to content

Commit

Permalink
feat(files): pause/resume
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 16, 2014
2 parents 60a70dc + c57edf6 commit a3c697f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ module.exports.reload = require("./lib/public/reload")(browserSync);
*/
module.exports.notify = require("./lib/public/notify")(browserSync);

/**
* Method to pause file change events
*
* @method pause
*/
module.exports.pause = require("./lib/public/pause")(browserSync);

/**
* Method to resume paused watchers
*
* @method resume
*/
module.exports.resume = require("./lib/public/resume")(browserSync);

/**
* Register a plugin. Must implement at least a 'plugin' method that returns a
* callable function.
Expand Down Expand Up @@ -78,3 +92,14 @@ Object.defineProperty(module.exports, "active", {
return browserSync.active;
}
});

/**
* A simple true/false flag to determine if the current instance is paused
*
* @property paused
*/
Object.defineProperty(module.exports, "paused", {
get: function () {
return browserSync.paused;
}
});
5 changes: 4 additions & 1 deletion lib/browser-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var BrowserSync = function () {

this.cwd = process.cwd();
this.active = false;
this.paused = false;
this.config = config;

// Events
Expand Down Expand Up @@ -256,7 +257,8 @@ BrowserSync.prototype.registerInternalEvents = function (options) {

var events = {
"file:changed": function (data) {
if (data.namespace === "core") {

if (!this.paused && data.namespace === "core") {
if (_.isUndefined(data.log)) {
data.log = this.getOption("logFileChanges");
}
Expand Down Expand Up @@ -384,6 +386,7 @@ BrowserSync.prototype.cleanup = function (cb) {
// Reset the flag
this.debug("Setting {magenta:active: false");
this.active = false;
this.paused = false;

this.pluginManager.plugins = {};
this.pluginManager.pluginOptions = {};
Expand Down
12 changes: 12 additions & 0 deletions lib/public/pause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

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

return function () {
browserSync.paused = true;
};
};
12 changes: 12 additions & 0 deletions lib/public/resume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

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

return function () {
browserSync.paused = false;
};
};
23 changes: 23 additions & 0 deletions test/specs/api/init.pause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

var browserSync = require("../../../");

var assert = require("chai").assert;

describe("API: .pause() / .resume() file reloading - ", function () {

it("should be unpaused", function () {
assert.isFalse(browserSync.paused);
});

it("should pause file reload:", function () {
browserSync.pause();
assert.isTrue(browserSync.paused);
});

it("should unpause file reload:", function () {
browserSync.resume();
assert.isFalse(browserSync.paused);
});

});
23 changes: 23 additions & 0 deletions test/specs/e2e/e2e.file.changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ describe("E2E Responding to events", function () {

assert.equal(eventName, "file:reload"); // check correct event sent to client
assert.equal(args.assetFileName, "styles.css"); // Check the asset name is sent
assert.isFalse(instance.paused);
});

it("doesn't fire the file:reload event to the browser when paused", function () {
instance.paused = true;

// Emit the event as it comes from the file-watcher
instance.events.emit("file:changed", {path: "styles.css", log: true, namespace: "core"});

clock.tick();

assert.isTrue(socketsStub.withArgs("file:reload").notCalled); // should not be called
assert.isTrue(instance.paused);

instance.paused = false;

// Emit the event as it comes from the file-watcher
instance.events.emit("file:changed", {path: "styles.css", log: true, namespace: "core"});

clock.tick();

assert.isTrue(socketsStub.withArgs("file:reload").called);
assert.isFalse(instance.paused);
});

it("Sets `log: false` if `log` is undefined in event", function () {
Expand Down

0 comments on commit a3c697f

Please sign in to comment.