git ssb

0+

Piet / ssb-loomio



Tree: 1370996e72773275a544e0bdd26ee0f6db0e49d8

Files: 1370996e72773275a544e0bdd26ee0f6db0e49d8 / lib / inject.js

1714 bytesRaw
1const { map } = require('libnested')
2const { onceTrue, watch, Value } = require('mutant')
3// const Struct = require('../struct')
4
5// auto-inject the ssb-server to all methods to reduce repitition
6module.exports = function inject (server, methods, pluginDeps = []) {
7 switch (typeof server) {
8 case 'object': // just a calssic ssb server
9 checkPlugins(server, pluginDeps)
10 return map(methods, (fn, path) => fn(server))
11
12 case 'function': // hopefully an observeable which will contain an ssb server
13 return injectObsServer(server, methods)
14
15 default:
16 throw new Error('scuttle-blog expects an ssb server (or obs that contains one)')
17 }
18}
19
20//TODO fix this plugins Deps thing D:
21function injectObsServer (server, methods, pluginDeps = []) {
22 onceTrue(server, server => checkPlugins(server, pluginDeps))
23
24 return map(methods, (fn, path) => {
25 if (path.includes('async')) {
26 return function () {
27 onceTrue(
28 server,
29 server => fn(server).apply(null, arguments)
30 )
31 }
32 }
33
34 // NOTE - both `obs` and `sync` methods will return observeables
35 return function () {
36 var result = Value({})
37 // var result = Struct({}) // WARNING - this shouldn't be copied for other apps, only works with obs.get method here. Probably breaks sync.isBlog
38 onceTrue(
39 server,
40 server => {
41 var res = fn(server).apply(null, arguments)
42 watch(res, res => result.set(res))
43 }
44 )
45 return result
46 }
47 })
48}
49
50function checkPlugins (server, pluginDeps) {
51 pluginDeps.forEach(p => {
52 if (!server[p]) throw new Error(`scuttle-blog needs a scuttlebot server with the ${p} plugin installed`)
53 })
54}
55

Built with git-ssb-web