0.1.1 • Published 11 years ago
recurse-stream v0.1.1
recurse-stream
A functionally recursive pull stream
Example
var readstream = require("recurse-stream")
var isError = require("recurse-stream/is-error")
// a readable stream is a function that takes a writable stream
var cursor = cursorStream(someDb, { /* ... */ })
// a writable stream is just a function (chunk, recurse). It calls recurse
// when it wants to read more
cursor(function writable(chunk, recurse) {
if (chunk === null) {
/* cursor ended */
} else if (isError(chunk)) {
/* a oops happened */
} else {
/* got chunk from database yay! */
// remember to call recurse to read more
recurse()
}
})
function cursorStream(db, opts) {
var source
return readstream(function (writable, recurse) {
if (!source) {
source = db.cursor(opts)
}
cursor.nextObject(function (er, item) {
if (er) return writable(er)
if (item === null) return writable(null)
writable(item, recurse)
})
})
}
Installation
npm install recurse-stream
Contributors
- Raynos