Skip to content

gulp.watch(glob, cb) and gulp.watch(glob).pipe(...) #84

Closed

Description

@floatdrop

This is really confuses me. If you read this article you can see, that gulp.watch was used like this:

gulp.task('default', function(){
    gulp.watch(['./scss/*.scss', './js/*.js'], function(){
        gulp.run('lint', 'sass', 'scripts');
    });
});

This is why gulp-batch exists - if you change multiple files these guys 'lint', 'sass', 'scripts' will be runned multiple times.

If you look at gulp api - all methods should support streaming, but grunt.watch not. I think this is a source of confusion. If it can be refactored into stream, then it will be clearer (a little) that it will emit file by file like src does.

gulp.task('default', function(){
    gulp.watch('./js/*.js').pipe(jshint());
    gulp.watch('./scss/*.scss').pipe(sass()).pipe(gulp.dest('./css'));
});

But there is another problem with watch - people often want do not what it does. Now "watch is getting changed file and calls callback on each". What often needed - is "if any of this files changed - rerun task. Once. And be aware of multiple files can change at once".

And for second thing streams are bad:

gulp.task('default', function(){
    gulp.watch(['./js/*.js']).pipe(batch()).pipe(magic(function(events) {
        gulp.run('lint', 'sass', 'scripts');
    }));
});

As you can see, even if we batched incoming files, we can assure, that grunt.run will be called separately and not interfere with already running one. (or I miss something in streams - may be hacks on backpressure?)

So I propose make callback version of watch batching by default and streaming version - is just streaming version.

Activity

  1. floatdrop commented on Jan 3, 2014

    @floatdrop
    ContributorAuthor

    This is a way of solving #76

  2. dashed commented on Jan 3, 2014

    @dashed
    Contributor

    Wouldn't the last snippet

    gulp.task('default', function(){
        gulp.watch(['./js/*.js']).pipe(batch()).pipe(magic(function(events) {
            gulp.run('lint', 'sass', 'scripts');
        }));
    });

    be better as

    gulp.task('default', function(){
        gulp.watch(['./js/*.js'], batch({timeout: 2000}, function (events, cb) {
            gulp.run('lint', 'sass', 'scripts');
        }));
    });

    ?


    IMO, the first and last snippets seem to be anti-patterns and no one should use gulp.run in that manner for the problems you've just described.

    If batching were to be baked into gulp.watch, wouldn't batch limit of 1 make gulp.watch behave as it is currently implemented?

    Assuming #13 is done, the revised API could be: gulp.watch(glob [[, batch_opts], cb])

    API scenario:

    1. gulp.watch(glob [, batch_opts], cb) - events are deferred to callback with optional batch'ness. Doesn't return stream.
    2. gulp.watch(glob [, batch_opts]) - assumed to be streaming, also with optional batch'ness. Does return stream.

    In all scenarios, batch_opts.limit = 1 by default.

  3. floatdrop commented on Jan 3, 2014

    @floatdrop
    ContributorAuthor

    Well, after looking at articles on gulp I think, baking in batching in watch is a solution of many problems, that are already floated up. I predict many copy-pasted watch code from mentioned article, that potentially broken.

    Your API scenario is bad, because you introducing duplicate behaviour (by default, but still) - so what the point? Here is my version:

    1. gulp.watch(glob [, opts], cb) - are batching by default (by the way - opts already there), but can be tuned with opts
    2. gulp.watch(glob [, batch_opts]) - return stream without batch'ness.

    P.s. batchness to stream can be added with using .pipe so no need to include it.

  4. dashed commented on Jan 3, 2014

    @dashed
    Contributor

    I hadn't meant batch_opts to be specifically for batching; more of gulp.watch options.

    What would be a good value for the hypothetical opts.batch.limit for scenario 1 other than opts.batch.limit = 1? Isn't it batching in all values of opts.batch.limit anyways?

    I made my API scenario (in particular scenario 1) to be compatible with current behaviour.

    If the callback version of gulp.watch has batching, there should be no reason its streaming counterpart shouldn't.

    But I just realized scenario 2 seems to be weird. What if we break it down to see what it should do (from what I understand). Assume given streaming gulp.watch with batch:

    1. Collect event(s) for batching according to batch rules.

    2. Drain collected event(s).

    3. Collect any new event(s) as new batch; but don't drain yet. A listener could be hooked via .on('end') to count events. Drain when count satisfy batch rules (make sure to drain accordingly when it overflows). Repeat step 3.

      This requires something like { end: false }.

    I think, as in the 3rd snippet, you suggested some magic function to collect all the batched events down the stream? I'm unsure how to do that :)

  5. yocontra commented on Jan 3, 2014

    @yocontra
    Member

    This conversation is about

    • Batching
    • Watch streams

    There are already a few open issues regarding these. Should I close everything and consolidate it somewhere?

  6. dashed commented on Jan 3, 2014

    @dashed
    Contributor

    Good point. Feel free to close/reference as you see fit.

  7. yocontra commented on Jan 3, 2014

    @yocontra
    Member

    Also please note complexities with watch streams are

    • Plugins that need all files and a stream above it that ends (concat, browserify, mocha, etc.)
    • Tasks have to end at some point otherwise the task dependency system provided by orchestator won't work
  8. yocontra commented on Jan 4, 2014

    @yocontra
    Member

    Possible solution to the "needs all the files" problem

    • Option that will make the watch stream re-emit the glob as if it was a new src stream every time one file changes
  9. dashed commented on Jan 4, 2014

    @dashed
    Contributor

    That could work.

  10. floatdrop commented on Jan 4, 2014

    @floatdrop
    ContributorAuthor

    So gulp-watch is in alpha-ready version. It implements API that I described at the issue. You can see its readme section for information.

    @contra re-emitting glob is rare case. I thing watching will be on all source files + test files and mocha must get only test files.

    As @contra sayd in IRC - the best route to get stuff in is to just make it in userland, work out the bugs there, then if it seems like the right fit we can pull it into core - so more versions of watch api implemented in plugins will be awesome.

  11. yocontra commented on Jan 4, 2014

    @yocontra
    Member

    Nice job @floatdrop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions