Skip to content

Commit

Permalink
[minor] use callback style so fn is actually set in square#build
Browse files Browse the repository at this point in the history
  • Loading branch information
Swaagie committed Nov 28, 2013
1 parent 0190434 commit afd0add
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
24 changes: 10 additions & 14 deletions lib/square.js
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ Square.prototype.hold = function hold(file) {
* @param {Object} previous old square config used to compare against
* @api public
*/
Square.prototype.refresh = function refresh(files, previous) {
Square.prototype.refresh = function refresh(files, previous, fn) {
var bundle = this.package.bundle
, extensions = []
, changes = []
Expand Down Expand Up @@ -1282,16 +1282,12 @@ Square.prototype.refresh = function refresh(files, previous) {
Object.keys(bundle).forEach(find.bind(this, true));
}

if (!changes.length) {
// Just pretend we build stuff, so non bundle files will trigger refreshes.
this.emit('build', changes, [ 'css', 'js' ]);
} else {
// Trigger a new build from the given extensions.
this.emit('changed', _.unique(changes));
this.build(this.package.configuration.platform, _.unique(extensions));
}
// Just pretend we build stuff, so non bundle files will trigger refreshes.
if (!changes.length) return fn(null, changes, ['css', 'js']);

return this;
// Trigger a new build from the given extensions.
this.emit('changed', _.unique(changes));
this.build(this.package.configuration.platform, _.unique(extensions), fn);
};

/**
Expand Down Expand Up @@ -1533,12 +1529,12 @@ Square.prototype.build = function build(platform, extensions, fn) {
return memo;
}, {});

if (err && !args.function) return self.critical(err);
if (args.function) return args.function.call(self, err, files);

// Emit that we are ready.
self.logger.info('Successfully generated %s', Object.keys(files).join(', ').green);
self.emit('build', files, extensions);

if (err && !args.function) self.critical(err);
if (args.function) args.function.call(self, err, files);
fn(null, files, extensions);
}

// Make sure that we actually have parsed package file that we can build
Expand Down
36 changes: 22 additions & 14 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,11 @@ function Watcher(square, port, silent) {
this.socket = this.live.call(this, port);
this.config = path.resolve(process.env.PWD, this.square.package.location);

// Notify external listeners that we are idly watching.
this.defer = function defer() {
square.emit('idle', self.silent);
};

// Initialize the live reload, also trigger watching and process the file list.
this.init = function init() {
self.watch.apply(self, arguments[1]);
};

// If a build finished, emit a refresh and start the spinner.
this.square.on('build', function notifyBrowser(files, extensions) {
self.socket.emit('refresh', files, extensions);
self.defer();
});

// Require fs.notify and findit, trigger the watch.
async.parallel([canihaz['fs.notify'], canihaz.findit], this.init);
}
Expand All @@ -67,6 +56,15 @@ function getPath(path, callback) {
});
}

/**
* Notify external listeners that we are idly watching.
*
* @api private;
*/
Watcher.prototype.defer = function defer() {
this.square.emit('idle', this.silent);
};

/**
* Force a rebuild of the active square configuration if the user
* changed any files within the bundle.
Expand Down Expand Up @@ -105,6 +103,8 @@ Watcher.prototype.rebuild = function rebuild() {
* @api public
*/
Watcher.prototype.refresher = function refresher(err, changes, previous) {
var watch = this;

if (err) {
return this.square.logger.error(
'Watcher error %s, canceling watch operations on %s'
Expand All @@ -114,7 +114,15 @@ Watcher.prototype.refresher = function refresher(err, changes, previous) {

// Start processing, stop the spinner momentarily.
this.square.emit('processing');
this.square.refresh(changes, previous);
this.square.refresh(changes, previous, function notifyBrowser(err, files, extensions) {
if (err) return watch.square.logger.error(
'Builder error %s, not refreshing the browser',
err.message
);

watch.socket.emit('refreshBrowser', files, extensions);
watch.defer();
});

// Empty line and notification
if (!this.silent) {
Expand Down Expand Up @@ -270,7 +278,7 @@ Watcher.prototype.watch = function watch(Notify, findit) {
}
});

process.nextTick(this.defer);
process.nextTick(this.defer.bind(this));
};

/**
Expand Down Expand Up @@ -319,7 +327,7 @@ Watcher.prototype.live = function live(port) {

// start listening for changes that are emitted by the watch function and
// broadcast it to every connected user
EventEmitter.on('refresh', function refresh(files, extensions) {
EventEmitter.on('refreshBrowser', function refresh(files, extensions) {
io.sockets.emit('refresh', files, extensions);
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/square.api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ describe('[square] API', function () {
done();
});

square.build();
square.build(null, null, noop);
});

it('should transform ~ to the $HOME path in file names', function (done) {
Expand All @@ -984,7 +984,7 @@ describe('[square] API', function () {
done();
});

square.build();
square.build(null, null, noop);
});

it('should prefix the file content with a license header', function (done) {
Expand All @@ -1000,7 +1000,7 @@ describe('[square] API', function () {
done();
});

square.build();
square.build(null, null, noop);
});

it('should not write when writable is set to false', function (done) {
Expand Down
8 changes: 0 additions & 8 deletions test/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ describe('[square] watch API', function () {
expect(watcher.square).to.be.instanceof(Square);
});

it('register event listener to trigger on build', function () {
var build = sinon.spy(square, 'on');
watcher = new Watch(square, 8888, true);
expect(build).to.be.calledOnce;
expect(build).to.be.calledWith('build');
build.restore();
});

it('asynchronously loads required modules for watch', function () {
var parallel = sinon.spy(async, 'parallel');
watcher = new Watch(square, 8888, true);
Expand Down

0 comments on commit afd0add

Please sign in to comment.