├── .editorconfig ├── .gitignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 4 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | [*.json] 16 | indent_size = 2 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | .DS_Store 10 | coverage 11 | 12 | pids 13 | logs 14 | results 15 | 16 | npm-debug.log 17 | node_modules 18 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "-W068": true, 3 | "node": true, 4 | "esnext": true, 5 | "bitwise": true, 6 | "camelcase": true, 7 | "curly": true, 8 | "eqeqeq": true, 9 | "immed": true, 10 | "indent": 4, 11 | "latedef": true, 12 | "newcap": true, 13 | "noarg": true, 14 | "quotmark": "single", 15 | "regexp": true, 16 | "undef": true, 17 | "unused": true, 18 | "strict": true, 19 | "trailing": true, 20 | "smarttabs": true, 21 | "white": true 22 | } 23 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | .editorconfig 3 | .jshintrc 4 | .travis.yml 5 | coverage 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.11' 5 | after_script: 6 | - npm run coveralls 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Vsevolod Strukchinsky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [gulp](https://github.com/gulpjs/gulp)-batch [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] 2 | > Event batcher for gulp-watch'er. 3 | 4 | This is problem solver for [this issue](https://github.com/gulpjs/gulp/issues/80) with [gulp.watch](https://github.com/gulpjs/gulp#gulpwatchglob-cb) and [gulp-mocha](https://github.com/sindresorhus/gulp-mocha). 5 | Long story short - example below without `batch`'ing will call mocha as many times, as many files was changed (for example `git checkout` can touch dozens files). 6 | 7 | Also it can be used with [`gulp-watch`](https://github.com/floatdrop/gulp-watch) to provide batching callback handler, instead of streaming events from gaze. 8 | 9 | ## Usage 10 | 11 | Main purpose for this module is running tests in `gulp-watch`. So here it is: 12 | 13 | ```js 14 | var gulp = require('gulp'); 15 | var batch = require('gulp-batch'); 16 | 17 | gulp.watch(['lib/**', 'test/**'], batch(function(events, cb) { 18 | events.on('data', console.log).on('end', cb); 19 | })); 20 | ``` 21 | 22 | ## API 23 | 24 | ### batch([options,] callback, [errorHandler]) 25 | 26 | This function creates batcher for provided callback. 27 | It will call it, when bunch of events happens near in time, so you will 28 | be running your test only once per `git checkout` command (for example). 29 | 30 | __Callback signature__: `function(events, done)`. 31 | 32 | * `events` - is `Stream` of incoming events. 33 | * `done` - is callback for your function signal to batch, that you are done. This allows to run your callback as soon as previous end. Error can be passed as argument. 34 | 35 | __Options__: 36 | 37 | * `limit` - Maximum events number, that gets into one batch (default: `undefined` - unlimited) 38 | * `timeout` - Interval in milliseconds, that counts as "no more events will arrive" (default: `100`) 39 | 40 | __Errors__: 41 | 42 | All errors in batched function will be passed to `errorHandler`. 43 | 44 | __Returns__: 45 | 46 | Wrapped callback, that will gather events and call callback. 47 | 48 | # License 49 | 50 | (MIT License) 51 | 52 | Copyright (c) 2013 Vsevolod Strukchinsky (floatdrop@gmail.com) 53 | 54 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 55 | 56 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 57 | 58 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 59 | 60 | [npm-url]: https://npmjs.org/package/gulp-batch 61 | [npm-image]: http://img.shields.io/npm/v/gulp-batch.svg?style=flat 62 | 63 | [travis-url]: https://travis-ci.org/floatdrop/gulp-batch 64 | [travis-image]: http://img.shields.io/travis/floatdrop/gulp-batch.svg?style=flat 65 | 66 | [coveralls-url]: https://coveralls.io/r/floatdrop/gulp-batch 67 | [coveralls-image]: http://img.shields.io/coveralls/floatdrop/gulp-batch.svg?style=flat 68 | 69 | [depstat-url]: https://david-dm.org/floatdrop/gulp-batch 70 | [depstat-image]: http://img.shields.io/david/floatdrop/gulp-batch.svg?style=flat 71 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var array = require('stream-array'); 4 | var asyncDone = require('async-done'); 5 | 6 | module.exports = function (opts, cb, errorHandler) { 7 | if (typeof opts === 'function') { 8 | errorHandler = cb; 9 | cb = opts; 10 | opts = {}; 11 | } 12 | 13 | if (typeof cb !== 'function') { 14 | throw new Error('Provided callback is not a function: ' + cb); 15 | } 16 | 17 | opts.timeout = opts.timeout || 100; 18 | 19 | var batch = []; 20 | var holdOn; 21 | var timeout; 22 | 23 | function setupFlushTimeout() { 24 | if (!holdOn && batch.length) { 25 | timeout = setTimeout(flush, opts.timeout); 26 | } 27 | } 28 | 29 | function flush() { 30 | holdOn = true; 31 | var currentBatch = batch; 32 | batch = []; 33 | asyncDone(cb.bind(cb, array(currentBatch)), function (err) { 34 | holdOn = false; 35 | if (err && typeof errorHandler === 'function') { errorHandler(err); } 36 | setupFlushTimeout(); 37 | }); 38 | } 39 | 40 | return function (event) { 41 | batch.push(event); 42 | 43 | if (timeout) { clearTimeout(timeout); } 44 | 45 | if (opts.limit && batch.length >= opts.limit) { 46 | flush(); 47 | } else { 48 | setupFlushTimeout(); 49 | } 50 | }; 51 | }; 52 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-batch", 3 | "version": "1.0.5", 4 | "description": "Event batcher for gulp-watcher", 5 | "main": "index.js", 6 | "keywords": [ 7 | "gulp", 8 | "batch", 9 | "throttle", 10 | "debounce", 11 | "gulpfriendly", 12 | "watch", 13 | "mocha" 14 | ], 15 | "scripts": { 16 | "test": "istanbul cover _mocha -- test/*.js --reporter spec", 17 | "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" 18 | }, 19 | "repository": { 20 | "type": "git", 21 | "url": "git://github.com/floatdrop/gulp-batch.git" 22 | }, 23 | "author": "Vsevolod Strukchinsky", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/floatdrop/gulp-batch/issues" 27 | }, 28 | "devDependencies": { 29 | "coveralls": "^2.11.2", 30 | "istanbul": "^0.3.5", 31 | "mocha": "^2.0.1", 32 | "mocha-lcov-reporter": "0.0.1", 33 | "should": "^4.3.1", 34 | "stream-assert": "^2.0.2" 35 | }, 36 | "dependencies": { 37 | "async-done": "^1.0.0", 38 | "stream-array": "^1.0.1" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 'use strict'; 3 | 4 | var batch = require('..'); 5 | var assert = require('stream-assert'); 6 | require('should'); 7 | 8 | var defaultTimeout = 10; 9 | 10 | var opts = { 11 | timeout: defaultTimeout 12 | }; 13 | 14 | describe('gulp-batch', function () { 15 | 16 | it('should throw, if we provide invalid callback', function () { 17 | batch.should.throw(/Provided callback is not a function/); 18 | (function () { batch('string'); }).should.throw(/Provided callback is not a function/); 19 | }); 20 | 21 | it('should batch sync calls to stream', function (done) { 22 | var receiver = batch(opts, function (events) { 23 | events.pipe(assert.length(2)) 24 | .pipe(assert.end(done)); 25 | }); 26 | receiver('one'); 27 | receiver('two'); 28 | }); 29 | 30 | it('should batch async calls to stream', function (done) { 31 | var receiver = batch(opts, function (events) { 32 | events.pipe(assert.length(2)) 33 | .pipe(assert.end(done)); 34 | }); 35 | receiver('one'); 36 | setTimeout(receiver, 5, 'two'); 37 | }); 38 | 39 | it('should flush, if we exceed timeout', function (done) { 40 | var i = 0; 41 | var receiver = batch({ timeout: 5 }, function (events, cb) { 42 | events.pipe(assert.length(1)) 43 | .on('assertion', done); 44 | if (++i === 2) { done(); } 45 | cb(); 46 | }); 47 | 48 | receiver('one'); 49 | setTimeout(receiver, defaultTimeout + 10, 'two'); 50 | }); 51 | 52 | it('should flush, if we exceed limit', function (done) { 53 | var i = 0; 54 | var receiver = batch({ limit: 1 }, function (events) { 55 | events.pipe(assert.length(1)) 56 | .on('assertion', done); 57 | if (++i === 2) { done(); } 58 | }); 59 | 60 | receiver('one'); 61 | receiver('two'); 62 | }); 63 | 64 | it('should hold events, while current batch is processing', function (done) { 65 | var i = 0; 66 | var receiver = batch({ timeout: 5 }, function (events, cb) { 67 | setTimeout(cb, defaultTimeout * 2); 68 | if (++i === 2) { done(); } 69 | }); 70 | 71 | receiver('one'); 72 | setTimeout(receiver, defaultTimeout, 'two'); 73 | }); 74 | 75 | it('should pass throwen error to errorHandler', function (done) { 76 | var receiver = batch(opts, function (cb) { 77 | throw new Error('Bang!'); 78 | }, function (err) { 79 | err.message.should.eql('Bang!'); 80 | done(); 81 | }); 82 | 83 | receiver('one'); 84 | }); 85 | 86 | it('should pass cb error to errorHandler', function (done) { 87 | var receiver = batch(function (events, cb) { 88 | cb(new Error('Bang!')); 89 | }, function (err) { 90 | err.message.should.eql('Bang!'); 91 | done(); 92 | }); 93 | 94 | receiver('one'); 95 | }); 96 | }); 97 | --------------------------------------------------------------------------------