git ssb

0+

mixmix / scuttle-inject



Branch:
mix irving committed 1.0.0Latest: 4ed2496 on 6/18/2018, 2:26:31 AM
📄.gitignore
📄README.md
📄index.js
📄package-lock.json
📄package.json
README.md

Scuttle-inject

Takes a particular sort of nested tree of methods and returns a version of that tree which has a scuttlebutt server injected into each method.

This pattern means you only need to inject a scuttlebot once, makes it easier to test your methods, and handles being able to inject different sorts of server for you.

Example usage

// methods.js
module.exports = {
  async: {
    publishPoll: function (server) {
      return function (opts, cb) {
        // check the opts before publishing
        const cleanOpts = clean(opts) 

        server.publish(cleanOpts, cb)
      }
    },
    // getPoll: (key, cb) => {}
  },
  pull: {
    myPolls: function (server) {
      return function (opts) {
        const defaultQuery = { ... }
        const query = Object.assign({}, defaultQuery, opts)

        return server.query.read(opts)
      }
    }
    // openPolls: (opts) => {},
    // closedPolls: (opts) => {}
  }
}

Injecting server once (somehwhere high-level):

const inject = require('scuttle-inject')
cosnt methods = require('./methods')

const scuttle = inject(methods, server)
// assume you're in a context where you have a server

Using the scuttle helper:

const opts = { ... }
scuttle.async.publishPoll(opts, cb)

API

inject(server, methods, pluginDeps)

Built with git-ssb-web