From ac28ac324dfd282b2b083ffb2d37e95fd90188f9 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Mon, 18 May 2015 17:48:41 -0700 Subject: [PATCH] fix(gulp): continue watching when tasks throw Closes #1915 --- tools/build/watch.js | 6 ++++-- tools/build/watch.spec.js | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/tools/build/watch.js b/tools/build/watch.js index 15a600ca72..31d536f626 100644 --- a/tools/build/watch.js +++ b/tools/build/watch.js @@ -49,7 +49,7 @@ function watch(globs, opts, tasks) { watcher.close = function() { if (timeoutId !== null) clearTimeout(timeoutId); close(); - } + }; var eventsRecorded = 0; // Number of events recorded var timeoutId = null; // If non-null, event capture window is open @@ -75,13 +75,15 @@ function watch(globs, opts, tasks) { } function tasksDone(err) { - if (err) throw err; if (eventsRecorded) { // eventsRecorded has increased during the run, run again on the next turn timeoutId = setTimeout(invokeCallback, 0); } else { timeoutId = null; } + if (!useRunSequence && err) { + console.log('Watch task error:', err.toString()); + } } } diff --git a/tools/build/watch.spec.js b/tools/build/watch.spec.js index b1f06dce6b..fa93563f8c 100644 --- a/tools/build/watch.spec.js +++ b/tools/build/watch.spec.js @@ -72,6 +72,28 @@ describe('watch()', function() { }); + it('should continue to trigger callbacks if task throws', function() { + var calls = 0; + spyOn(console, 'log'); + function cb(done) { + calls += 1; + if (calls === 1) throw new Error('oops!'); + done(); + } + + var watcher = watch('./$$fake_path/**/*', { delay: 10 }, cb); + + watcher._emit('change', './$$fake_path/test1.txt'); + timeout.flush(); + expect(calls).toBe(1); + expect(console.log).toHaveBeenCalledWith('Watch task error:', 'Error: oops!'); + + watcher._emit('change', './$$fake_path/test2.txt'); + timeout.flush(); + expect(calls).toBe(2); + }); + + it('should cancel pending callback if FSWatcher is closed', function() { var cb = jasmine.createSpy('callback'); var watcher = watch('./$$fake_path/**/*', { delay: 10 }, cb);