Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

read from one pull-stream, then the next, then the next...

when one stream end (unless it errored) call a function
to get the next stream. much like [pull-cat](https://github.com/pull-stream/pull-cat)
when one stream end (unless it errored) call a function to get the next
stream. there is also a function that takes an asynchronous function.

much like [pull-cat](https://github.com/pull-stream/pull-cat)
except creates streams by calling a function instead of takeing them out of an array.

in particular, this is useful for making a read stream that reconnects
Expand Down
36 changes: 36 additions & 0 deletions async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var noop = function () {}

module.exports = function (next) {
var stream
return function (abort, cb) {
if(!cb) throw new Error('callback required!')
if(abort) {
if(stream) stream(abort, cb)
else cb(abort)
}
else more()

function more () {
if(stream) send()
else {
next(function (error, nextStream) {
if(error) return cb(error)
if(!nextStream) return cb(true)
stream = nextStream
send()
})
}
}

function send () {
stream(null, function (err, data) {
if(err) {
stream = null
if(err === true) setTimeout(more, 100)
else cb(err)
}
else cb(null, data)
})
}
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"url": "git://github.com/dominictarr/pull-next.git"
},
"dependencies": {},
"devDependencies": {},
"devDependencies": {
"pull-stream": "^3.4.5"
},
"scripts": {
"test": "set -e; for t in test/*.js; do node $t; done"
},
Expand Down
23 changes: 23 additions & 0 deletions test/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var assert = require('assert')
var pull = require('pull-stream')
var asyncNext = require('../async')

var arrays = [
['a', 'b', 'c'],
['d', 'e', 'f']
]

pull(
asyncNext(function (cb) {
if (arrays.length === 0) cb(null, false)
else cb(null, pull.values(arrays.shift()))
}),
pull.collect(function (err, data) {
assert.ifError(err, 'no error')
assert.deepEqual(
data,
['a', 'b', 'c', 'd', 'e', 'f'],
'pulls all data'
)
})
)