Skip to content

Commit

Permalink
Merge pull request #1464 from notclive/master
Browse files Browse the repository at this point in the history
Prevent async callback from being called multiple times
  • Loading branch information
shama committed Feb 14, 2016
2 parents 265a868 + d21272a commit 448f54e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/util/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

'use strict';

var grunt = require('../grunt');

// Construct-o-rama.
function Task() {
// Information about the currently-running task.
Expand Down Expand Up @@ -237,9 +239,9 @@
async = true;
// The returned function should execute asynchronously in case
// someone tries to do this.async()(); inside a task (WTF).
return function(success) {
return grunt.util._.once(function(success) {
setTimeout(function() { complete(success); }, 1);
};
});
};

// Expose some information about the currently-running task.
Expand Down
11 changes: 11 additions & 0 deletions test/util/task_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ exports.Tasks = {
});
task.run('a', 'h').start();
},
'Task#run (async task with multiple callbacks)': function(test) {
test.expect(1);
var task = this.task;
task.registerTask('a', 'Call async callback twice.', function() { var done = this.async(); done(); done(); });
task.registerTask('b', 'Never call async callback.', function() { this.async(); });
task.run('a', 'b').start();
delay(function() {
test.deepEqual(task.current.name, 'b', 'Should be stuck on task with no async callback');
test.done();
});
},
'Task#current': function(test) {
test.expect(8);
var task = this.task;
Expand Down

0 comments on commit 448f54e

Please sign in to comment.