Skip to content
invoke abstraction for reducible
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
test
.gitignore
.npmignore
.testem.json
.travis.yml
LICENCE
README.md
index.js
package.json

README.md

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

Something went wrong with that request. Please try again.