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 (data === null) { want = true return } running = true calling = true fn(data, afterRead) calling = false } } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
.npmignore | File | 13 B | 0644 |
|
.travis.yml | File | 62 B | 0644 |
|
LICENSE | File | 1.05 KB | 0644 |
|
README.md | File | 1012 B | 0644 |
|
collaborators.md | File | 352 B | 0644 |
|
index.js | File | 1019 B | 0644 |
|
package.json | File | 1.6 KB | 0644 |
|
test.js | File | 2.03 KB | 0644 |
|