Sha256: 9c24151222afa1c0136d7ec07e5b611b8258f28d2d113f20a4f346a569507d7e

Contents?: true

Size: 1.02 KB

Versions: 30

Compression:

Stored size: 1.02 KB

Contents

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

module.exports = each

function each (stream, fn, cb) {
  var want = true
  var error = null
  var ended = false
  var running = false
  var calling = 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)
    if (!calling) read()
  }

  function read () {
    while (!running && !ended) {
      want = false

      var data = shift(stream)
      if (ended) return
      if (data === null) {
        want = true
        return
      }

      running = true
      calling = true
      fn(data, afterRead)
      calling = false
    }
  }
}

Version data entries

30 entries across 29 versions & 10 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/stream-each/index.js
disco_app-0.18.0 test/dummy/node_modules/stream-each/index.js
disco_app-0.18.2 test/dummy/node_modules/stream-each/index.js
disco_app-0.16.1 test/dummy/node_modules/stream-each/index.js
disco_app-0.15.2 test/dummy/node_modules/stream-each/index.js
disco_app-0.18.4 test/dummy/node_modules/stream-each/index.js
disco_app-0.18.1 test/dummy/node_modules/stream-each/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/stream-each/index.js
disco_app-0.14.0 test/dummy/node_modules/stream-each/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/stream-each/index.js
tang-0.2.1 spec/tang_app/node_modules/stream-each/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/stream-each/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/stream-each/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/stream-each/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/stream-each/index.js
tang-0.2.0 spec/tang_app/node_modules/stream-each/index.js
tang-0.1.0 spec/tang_app/node_modules/stream-each/index.js
tang-0.0.9 spec/tang_app/node_modules/stream-each/index.js
enju_library-0.3.8 spec/dummy/node_modules/stream-each/index.js
ilog-0.4.1 node_modules/stream-each/index.js