Skip to content
Browse files

New end function

  • Loading branch information...
1 parent 8cb7b67 commit fc9d3e6a845627a2672c4f1b703cba824d631254 @wdavidw committed Mar 27, 2013
Showing with 73 additions and 14 deletions.
  1. +15 −6 lib/each.js
  2. +10 −8 src/each.coffee
  3. +48 −0 test/end.coffee
View
21 lib/each.js
@@ -21,7 +21,7 @@ Chained and parallel async iterator in one elegant function
module.exports = function(elements) {
- var arglength, eacher, endable, errors, events, isObject, keys, next, parallel, run, sync, times, type;
+ var arglength, eacher, end, endable, errors, events, isObject, keys, next, parallel, run, sync, times, type;
type = typeof elements;
if (elements === null || type === 'undefined') {
elements = [];
@@ -52,6 +52,7 @@ module.exports = function(elements) {
endable = 1;
eacher.paused = 0;
eacher.readable = true;
+ end = false;
eacher.write = function(item) {
var l;
l = arguments.length;
@@ -84,6 +85,15 @@ module.exports = function(elements) {
}
return eacher;
};
+ eacher.on = function(ev, callback) {
+ events[ev].push(callback);
+ return eacher;
+ };
+ eacher.end = function() {
+ end = true;
+ next();
+ return eacher;
+ };
eacher.sync = function(s) {
sync = s;
return eacher;
@@ -119,17 +129,13 @@ module.exports = function(elements) {
});
return eacher;
};
- eacher.on = function(ev, callback) {
- events[ev].push(callback);
- return eacher;
- };
run = function() {
var args, emit, err, error, index, l, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3;
if (eacher.paused) {
return;
}
error = null;
- if (endable === 1 && (eacher.done === eacher.total * times || (errors.length && eacher.started === eacher.done))) {
+ if (endable === 1 && (end || eacher.done === eacher.total * times || (errors.length && eacher.started === eacher.done))) {
eacher.readable = false;
if (errors.length) {
if (parallel !== 1) {
@@ -176,6 +182,9 @@ module.exports = function(elements) {
if (errors.length !== 0) {
break;
}
+ if (end) {
+ break;
+ }
index = Math.floor(eacher.started / times);
eacher.started++;
try {
View
18 src/each.coffee
@@ -43,6 +43,7 @@ module.exports = (elements) ->
endable = 1
eacher.paused = 0
eacher.readable = true
+ end = false
eacher.write = (item) ->
l = arguments.length
if l is 1
@@ -67,6 +68,13 @@ module.exports = (elements) ->
# Sequential (in case parallel is called multiple times)
else parallel = 1
eacher
+ eacher.on = (ev, callback) ->
+ events[ev].push callback
+ eacher
+ eacher.end = ->
+ end = true
+ next()
+ eacher
eacher.sync = (s) ->
sync = s
eacher
@@ -79,10 +87,6 @@ module.exports = (elements) ->
for p in pattern then @files p
return @
endable--
- # if arglength is 0
- # arglength = null
- # eacher.total = 0
- # elements = []
glob pattern, (err, files) ->
eacher.total += files.length
for file in files
@@ -91,14 +95,11 @@ module.exports = (elements) ->
endable++
run()
eacher
- eacher.on = (ev, callback) ->
- events[ev].push callback
- eacher
run = () ->
return if eacher.paused
# This is the end
error = null
- if endable is 1 and (eacher.done is eacher.total * times or (errors.length and eacher.started is eacher.done) )
+ if endable is 1 and (end or eacher.done is eacher.total * times or (errors.length and eacher.started is eacher.done) )
eacher.readable = false
if errors.length
if parallel isnt 1
@@ -123,6 +124,7 @@ module.exports = (elements) ->
while (if parallel is true then (eacher.total * times - eacher.started) > 0 else Math.min( (parallel - eacher.started + eacher.done), (eacher.total * times - eacher.started) ) )
# Stop on synchronously sent error
break if errors.length isnt 0
+ break if end
# Time to call our iterator
index = Math.floor(eacher.started / times)
eacher.started++
View
48 test/end.coffee
@@ -0,0 +1,48 @@
+
+should = require 'should'
+each = if process.env.EACH_COV then require '../lib-cov/each' else require '../lib/each'
+
+describe 'End', ->
+ it 'should work in sequential', (next) ->
+ count = 0
+ eacher = each([0,1,2,3,4,5,6,7,8,9])
+ .parallel(false)
+ .on 'item', (element, next) ->
+ count++
+ return next() if count < 5
+ return eacher.end() if count is 5
+ false.should.be.ok
+ .on 'error', (err) ->
+ next err
+ .on 'end', ->
+ count.should.eql 5
+ next()
+ it 'should work in parallel', (next) ->
+ count = 0
+ eacher = each([0,1,2,3,4,5,6,7,8,9])
+ .parallel(true)
+ .on 'item', (element, next) ->
+ count++
+ return next() if count < 5
+ return eacher.end() if count is 5
+ false.should.be.ok
+ .on 'error', (err) ->
+ next err
+ .on 'end', ->
+ count.should.eql 5
+ next()
+ it 'should work with times', (next) ->
+ count = 0
+ eacher = each()
+ .parallel(false)
+ .times(10)
+ .on 'item', (element, next) ->
+ count++
+ return next() if count < 5
+ return eacher.end() if count is 5
+ false.should.be.ok
+ .on 'error', (err) ->
+ next err
+ .on 'end', ->
+ count.should.eql 5
+ next()

0 comments on commit fc9d3e6

Please sign in to comment.
Something went wrong with that request. Please try again.