recurse-stream

0.1.1 • Public • Published

recurse-stream

build status dependency status

browser support

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)
        })
    })
}

Example transforms

var array = require("recurse-stream/array")
var map = require("recurse-stream/map")
var mapAsync = require("recurse-stream/mapAsync")

var stream = array.readList([1, 2, 3])

var doubles = map(stream, function(i) { return i * 2 })
var squares = mapAsync(stream, function (i, cb)  {
    process.nextTick(function () {
        cb(i * i)
    })
})

squares(function print(chunk, recurse) {
    console.log("chunk", chunk)

    recurse && recurse()
})

Installation

npm install recurse-stream

Contributors

  • Raynos

MIT Licenced

Readme

Keywords

none

Package Sidebar

Install

npm i recurse-stream

Weekly Downloads

1

Version

0.1.1

License

none

Last publish

Collaborators

  • raynos