git ssb

0+

Dominic / ssb-feed



Tree: 9b06ba56f9534d1a30e441adcdd38e4c343744ce

Files: 9b06ba56f9534d1a30e441adcdd38e4c343744ce / index.js

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

Built with git-ssb-web