invoker

invoke abstraction for reducible

invoker

invoke abstraction for reducible

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 (nextinitial) {
        var invoke = invoker(next, initial, cleanup)
 
        var cursor = someDb.query(query)
        cursor.forEach(function (errvalue) {
            invoke(err || value)
        })
 
        cursor.onEnd(function (err) {
            invoke(err || end)
        })
 
        function cleanup(callback) {
            cursor.close(callback)
        }
    })
}
var reducible = require("reducible")
var end = require("reducible/end")
 
function dbReduce(query) {
 
    return reducible(function (nextinitial) {
        var state = initial
        var ended = false
 
        var cursor = someDb.query(query)
        cursor.forEach(function (errvalue) {
            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)
            })
        }
    })
}

npm install invoker

  • Raynos