Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
98 lines (72 sloc) 2.05 KB

invoker

build status dependency status

browser support

invoke abstraction for reducible

Example

Imagine your writing a reducible representation of some asynchronous source. invoke gives you some sugar for how to write that reducible

var invoker = require("invoker")
var reducible = require("reducible")
var end = require("reducible/end")

function dbReduce(query) {
    return reducible(function (next, initial) {
        var invoke = invoker(next, initial, cleanup)

        var cursor = someDb.query(query)
        cursor.forEach(function (err, value) {
            invoke(err || value)
        })

        cursor.onEnd(function (err) {
            invoke(err || end)
        })

        function cleanup(callback) {
            cursor.close(callback)
        }
    })
}

Without invoke

var reducible = require("reducible")
var end = require("reducible/end")

function dbReduce(query) {

    return reducible(function (next, initial) {
        var state = initial
        var ended = false

        var cursor = someDb.query(query)
        cursor.forEach(function (err, value) {
            if (!ended) {
                state = next(err || value, state)
            }

            if (isReduced(state)) {
                ended = true
                cleanup()
            }
        })

        cursor.onEnd(function (err) {
            if (!ended) {
                next(err || end, state)
            }
        })

        function cleanup() {
            cursor.close(function (err) {
                if (err) {
                    return next(err, state)
                }

                next(end, state)
            })
        }
    })
}

Installation

npm install invoker

Contributors

  • Raynos

MIT Licenced