Skip to content

Commit

Permalink
New end function
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 27, 2013
1 parent 8cb7b67 commit fc9d3e6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
21 changes: 15 additions & 6 deletions lib/each.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/each.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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++
Expand Down
48 changes: 48 additions & 0 deletions test/end.coffee
Original file line number Diff line number Diff line change
@@ -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.