git ssb

0+

mixmix / scuttle-inject



Tree: f0668f9a8e9bd4c90739103f1c6582a4a483d2ae

Files: f0668f9a8e9bd4c90739103f1c6582a4a483d2ae / README.md

1856 bytesRaw

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