From f794ff5ba32c979675149ea2de0e45435f664ab4 Mon Sep 17 00:00:00 2001 From: Rob Richardson Date: Mon, 7 Oct 2013 13:26:36 -0700 Subject: [PATCH] Support promise or callback async tasks --- index.js | 21 +++++++++++++++++---- test/tasks.js | 28 +++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 9484cccb4..586b7dadf 100644 --- a/index.js +++ b/index.js @@ -135,7 +135,18 @@ module.exports = gulp = { console.log('['+task.name+' started]'); } task.running = true; - var p = task.fn.call(gulp); + var cb = function (err) { + if (err) { + throw new Error(err); + } + task.running = false; + task.done = true; + if (gulp.verbose) { + console.log('['+task.name+' calledback]'); + } + gulp._runStep.call(gulp); + }; + var p = task.fn.call(gulp, cb); if (p && p.done) { // wait for promise to resolve // FRAGILE: ASSUME: Q promises @@ -145,15 +156,17 @@ module.exports = gulp = { if (gulp.verbose) { console.log('['+task.name+' resolved]'); } - gulp._runStep(); + gulp._runStep.call(gulp); }); // .done() with no onRejected so failure is thrown - } else { - // no promise, we're done + } else if (!task.fn.length) { + // no promise, no callback, we're done if (gulp.verbose) { console.log('['+task.name+' finished]'); } task.running = false; task.done = true; + //} else { + // FRAGILE: ASSUME: callback } }, src: require('./lib/createInputStream'), diff --git a/test/tasks.js b/test/tasks.js index 8d42003cc..805f20f0c 100644 --- a/test/tasks.js +++ b/test/tasks.js @@ -60,7 +60,7 @@ describe('gulp tasks', function() { gulp.reset(); done(); }); - it('should run all asynchronous tasks', function(done) { + it('should run all async promise tasks', function(done) { var a, fn, fn2; a = 0; fn = function() { @@ -90,6 +90,32 @@ describe('gulp tasks', function() { }); gulp.isRunning.should.equal(true); }); + it('should run all async callback tasks', function(done) { + var a, fn, fn2; + a = 0; + fn = function(cb) { + setTimeout(function () { + ++a; + cb(null); + },1); + }; + fn2 = function(cb) { + setTimeout(function () { + ++a; + cb(null); + },1); + }; + gulp.task('test', fn); + gulp.task('test2', fn2); + gulp.run('test'); + gulp.run('test2', function () { + gulp.isRunning.should.equal(false); + a.should.equal(2); + gulp.reset(); + done(); + }); + gulp.isRunning.should.equal(true); + }); it('should run task scoped to gulp', function(done) { var a, fn; a = 0;