git ssb

0+

Piet / ssb-loomio



Tree: 8818456f68cdf8d623639cc4bc69b81a61f90869

Files: 8818456f68cdf8d623639cc4bc69b81a61f90869 / lib / inject.js

1607 bytesRaw
1const { map } = require('libnested')
2const { onceTrue, watch } = require('mutant')
3const Struct = require('../struct')
4
5// auto-inject the ssb-server to all methods to reduce repitition
6module.exports = function inject (server, methods) {
7 switch (typeof server) {
8 case 'object': // just a calssic ssb server
9 checkPlugins(server)
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
20function injectObsServer (server, methods) {
21 onceTrue(server, checkPlugins)
22
23 return map(methods, (fn, path) => {
24 if (path[0] === 'async') {
25 return function () {
26 onceTrue(
27 server,
28 server => fn(server).apply(null, arguments)
29 )
30 }
31 }
32
33 // NOTE - both `obs` and `sync` methods will return observeables
34 return function () {
35 // var result = Value({})
36 var result = Struct({}) // WARNING - this shouldn't be copied for other apps, only works with obs.get method here. Probably breaks sync.isBlog
37 onceTrue(
38 server,
39 server => {
40 var res = fn(server).apply(null, arguments)
41 watch(res, res => result.set(res))
42 }
43 )
44 return result
45 }
46 })
47}
48
49const PLUGIN_DEPS = ['blobs']
50
51function checkPlugins (server) {
52 PLUGIN_DEPS.forEach(p => {
53 if (!server[p]) throw new Error(`scuttle-blog needs a scuttlebot server with the ${p} plugin installed`)
54 })
55}
56

Built with git-ssb-web