Skip to content

Commit

Permalink
Run again if interrupt off and triggered during previous run. Closes G…
Browse files Browse the repository at this point in the history
  • Loading branch information
shama committed May 1, 2013
1 parent 044f6a7 commit d01491b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
16 changes: 15 additions & 1 deletion tasks/lib/taskrunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module.exports = function(grunt) {
this.nameArgs = [];
// A list of changed files to feed to task runs for livereload
this.changedFiles = Object.create(null);
// Flag to set if interrupt is false and a change happened during a task run
this.needsAnotherRun = false;
}
util.inherits(Runner, EE);

Expand Down Expand Up @@ -148,6 +150,7 @@ module.exports = function(grunt) {
self.interrupt();
} else {
// Dont interrupt the tasks running
self.needsAnotherRun = true;
return;
}
}
Expand Down Expand Up @@ -215,15 +218,26 @@ module.exports = function(grunt) {
var self = this;
if (self.running === false) { return; }
self.running = false;

var time = 0;
self._queue.forEach(function(name, i) {
var target = self._targets[name];
if (!target) { return; }
if (target.startedAt !== false) {
time += target.complete();
self._queue[i] = null;
if (self.needsAnotherRun !== true) {
self._queue[i] = null;
}
}
});

// If it needs another run
if (self.needsAnotherRun === true) {
self.needsAnotherRun = false;
self.run();
return;
}

var elapsed = (time > 0) ? Number(time / 1000) : 0;
self.changedFiles = Object.create(null);
self.emit('end', elapsed);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/tasks/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* grunt-contrib-watch
* http://gruntjs.com/
*
* Copyright (c) 2012 "Cowboy" Ben Alman, contributors
* Copyright (c) 2013 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/

module.exports = function(grunt) {
'use strict';
grunt.registerMultiTask('echo', 'A task that echos a message.', function() {
var msg = this.data.message || 'I do absolutely nothing.';
var wait = this.data.wait || 0;
var wait = this.data.wait || grunt.option('wait') || 0;
var fail = this.data.fail || false;
var done = this.async();

Expand Down
20 changes: 19 additions & 1 deletion test/tasks/watch_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,23 @@ exports.watchConfig = {
test.equal(grunt.util._(result).count('Waiting...'), 2, 'Should have displayed "Wating..." twice');
test.done();
});
}
},
runAgainNotInterrupted: function(test) {
test.expect(1);
var cwd = path.resolve(fixtures, 'multiTargets');
var assertWatch = helper.assertTask(['watch:one', '--wait=4000'], {cwd:cwd});
assertWatch([function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
setTimeout(function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
}, 2000);
setTimeout(function() {
grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
}, 3000);
}], function(result) {
helper.verboseLog(result);
test.equal(grunt.util._(result).count('one has changed'), 2, 'Should have ran the one task twice.');
test.done();
});
},
};

0 comments on commit d01491b

Please sign in to comment.