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