var eos = require('end-of-stream')

module.exports = each

function each (stream, fn, cb) {
  var waiting = false
  var error = null
  var ended = false

  stream.on('readable', onreadable)
  read(null)
  if (cb) eos(stream, {readable: true, writable: false}, done)
  return stream

  function done (err) {
    error = err
    ended = true
    if (waiting) return cb(error)
  }

  function onreadable () {
    if (waiting) read(null)
  }

  function read (err) {
    if (err) {
      waiting = true
      stream.destroy(err)
      return
    }
    var data = stream.read()
    waiting = !data
    if (waiting && ended) return cb(error)
    if (data) fn(data, read)
  }
}

var eos = require('end-of-stream')

module.exports = each

function each (stream, fn, cb) {
  var want = true
  var error = null
  var ended = false
  var running = false

  stream.on('readable', onreadable)
  onreadable()

  if (cb) eos(stream, {readable: true, writable: false}, done)
  return stream

  function done (err) {
    if (!error) error = err
    ended = true
    if (!running) cb(error)
  }

  function onreadable () {
    if (want) read()
  }

  function afterRead (err) {
    running = false

    if (err) {
      error = err
      if (ended) return cb(error)
      stream.destroy(err)
      return
    }
    if (ended) return cb(error)
    read()
  }

  function read () {
    if (ended || running) return
    want = false

    var data = stream.read()
    if (!data) {
      want = true
      return
    }

    running = true
    fn(data, afterRead)
  }
}

fix end bug