invoker

invoke abstraction for reducible

npm install invoker
3 downloads in the last week
10 downloads in the last month

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

npm loves you