Skip to content

Commit

Permalink
Rewrite gulp-batch using bach
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Sep 20, 2014
1 parent 217ae09 commit 839a096
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 265 deletions.
1 change: 1 addition & 0 deletions .jshintrc
@@ -1,4 +1,5 @@
{
"-W068": true,
"node": true,
"esnext": true,
"bitwise": true,
Expand Down
5 changes: 5 additions & 0 deletions .npmignore
@@ -0,0 +1,5 @@
test
.editorconfig
.jshintrc
.travis.yml
coverage
27 changes: 6 additions & 21 deletions README.md
Expand Up @@ -12,7 +12,7 @@ Main purpose for this module is running tests in `gulp-watch`. So here it is:

```js
// npm i gulp gulp-watch gulp-mocha gulp-batch

var gulp = require('gulp');
var mocha = require('gulp-mocha');
var batch = require('gulp-batch');
Expand All @@ -25,7 +25,7 @@ gulp.src(['lib/**', 'test/**'], batch(function(events) {

## API

### batch([options,] callback)
### batch([options,] callback, [errorHandler])

This function creates batcher for provided callback.
It will call it, when bunch of events happens near in time, so you will
Expand All @@ -38,31 +38,16 @@ __Callback signature__: `function(events, [done])`.

__Options__:

* `debounce` - Minimal interval between calling callback after `done` (only works with async callback) (default: `0`)
* `limit` - Maximum events number, that gets into one batch (default: `undefined` - unlimited)
* `timeout` - Interval in milliseconds, that counts as "no more events will arrive" (default: `200`)

__Returns__:

Wrapped callback, that will gather events and call callback.

## How to catch errors

From version 0.3.0 `gulp-batch` supports domains. This code should clarify, how to catch errors from `gulp-batch`:
__Errors__:

```js
var domain = require('domain').create();

domain.on('error', function (err) {
console.log(err);
});
All errors in batched function will be passed to `errorHandler`.

var receiver = domain.bind(batch({ timeout: 10 }, function () {
throw new Error('Bang!');
}));
__Returns__:

receiver('one');
```
Wrapped callback, that will gather events and call callback.

# License

Expand Down
4 changes: 0 additions & 4 deletions enable-power-assert.js

This file was deleted.

24 changes: 0 additions & 24 deletions gulpfile.js

This file was deleted.

68 changes: 14 additions & 54 deletions index.js
@@ -1,9 +1,11 @@
'use strict';

var es = require('event-stream');
var array = require('stream-array');
var bach = require('bach');

module.exports = function (opts, cb) {
module.exports = function (opts, cb, errorHandler) {
if (typeof opts === 'function') {
errorHandler = cb;
cb = opts;
opts = {};
}
Expand All @@ -12,79 +14,37 @@ module.exports = function (opts, cb) {
throw new Error('Provided callback is not a function: ' + cb);
}

opts.debounce = opts.debounce || 0;
opts.timeout = opts.timeout || 200;
opts.timeout = opts.timeout || 100;

var batch = [];
var holdOn;
var timeout;

function brace() {
function setupFlushTimeout() {
if (!holdOn && batch.length) {
timeout = setTimeout(flush, opts.timeout);
}
}

function async() {
holdOn = true;
return function (err) {
if (err) {
holdOn = false;
return domain.emit('error', err);
}

if (opts.debounce) {
setTimeout(function () {
holdOn = false;
brace();
}, opts.debounce);
} else {
holdOn = false;
brace();
}
};
}

var domain = require('domain').create();

function flush() {
if (!batch.length) { return; }
var _batch = es.readArray(batch);
var streamError;
var holdOn = true;
var waiter = bach.parallel(cb.bind(cb, array(batch)));
batch = [];
if (cb.length < 2) {
var r = domain.bind(cb)(_batch);
if (r && typeof r.pipe === 'function') {
var asyncCb = async();
// wait for stream to end
r.on('error', function (err) {
streamError = err;
});
r.on('data', function () {
streamError = null; // The error wasn't fatal, move along
});
r.once('end', function () {
asyncCb(streamError);
});
}
} else {
domain.bind(cb)(_batch, async());
}
waiter(function (err) {
holdOn = false;
if (err && typeof errorHandler === 'function') { errorHandler(err); }
});
}

var f = function (event) {
return function (event) {
batch.push(event);

if (timeout) { clearTimeout(timeout); }

if (opts.limit && batch.length >= opts.limit) {
flush();
} else {
brace();
setupFlushTimeout();
}
};

f.domain = domain;

return f;
};
19 changes: 9 additions & 10 deletions package.json
Expand Up @@ -13,7 +13,7 @@
"mocha"
],
"scripts": {
"test": "istanbul test _mocha --report html -- test/*.js --reporter spec",
"test": "istanbul cover _mocha -- test/*.js --reporter spec",
"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"
},
"repository": {
Expand All @@ -26,17 +26,16 @@
"url": "https://github.com/floatdrop/gulp-batch/issues"
},
"devDependencies": {
"gulp": "~3.2.2",
"coveralls": "~2.6.0",
"mocha": "~1.14.0",
"coveralls": "^2.11.1",
"istanbul": "^0.3.2",
"mocha": "^1.21.4",
"mocha-lcov-reporter": "0.0.1",
"istanbul": "~0.1.44",
"gulp-mocha": "~0.2.0",
"espower-loader": "~0.3.1",
"power-assert": "~0.3.1",
"async": "~0.2.9"
"should": "^4.0.4",
"stream-assert": "^2.0.1"
},
"dependencies": {
"event-stream": "~3.1.0"
"array-stream": "^0.1.1",
"bach": "^0.3.0",
"stream-array": "^0.1.3"
}
}

0 comments on commit 839a096

Please sign in to comment.