git ssb

0+

Dominic / ssb-feed



Tree: 92f33038e6bc357aed4d1c740fa53ac251b75137

Files: 92f33038e6bc357aed4d1c740fa53ac251b75137 / index.js

1558 bytesRaw
1var cont = require('cont')
2var util = require('./util')
3
4var ssbKeys = require('ssb-keys')
5
6function isFunction (f) {
7 return 'function' === typeof f
8}
9
10function isObject (o) {
11 return (
12 o && 'object' === typeof o
13 && !Buffer.isBuffer(o)
14 && !Array.isArray(o)
15 )
16}
17
18module.exports = function (ssb, keys) {
19
20 if(!ssb.add)
21 throw new Error('*must* install feeds on this ssb instance')
22
23 function getPrev(next) {
24 ssb.getLatest(keys.id, next)
25 }
26 function noop (err) {
27 if (err) throw err
28 }
29
30 var queue = null
31 var prev = null
32 var writing = false
33 var publish =
34 cont(function (type, message, cb) {
35 // argument variations
36 if (isFunction(message)) { cb = message; message = type } // add(msgObj, cbFn)
37 else if (isObject(message)) { message.type = type } // add(typeStr, mgObj, cbFn)
38 else { message = { type: type, value: message } } // add(typeStr, msgStr, cbFn)
39
40 var err = util.isInvalidContent(message)
41 if(err) return cb(err)
42
43 if(ssb.add.queue) {
44 return ssb.add.queue(keys.id, function (key, value) {
45 return util.create(keys, null, message, value, key)
46 }, cb)
47 }
48
49 ssb.getLatest(keys.id, function (err, data) {
50 var msg = data
51 ? util.create(keys, null, message, data.value, data.key)
52 : util.create(keys, null, message, null, null)
53
54 ssb.add(msg, cb)
55 })
56
57 return this
58 })
59
60 return {
61 id: keys.id,
62 keys: keys,
63 add: publish,
64 publish: publish
65 }
66}
67
68
69
70

Built with git-ssb-web