git ssb

1+

Dominic / secure-scuttlebutt



Tree: a2e6e922f8d9cb98c60e57c51b85a3fc9aad8494

Files: a2e6e922f8d9cb98c60e57c51b85a3fc9aad8494 / util.js

1114 bytesRaw
1var Map = require('pull-stream/throughs/map')
2
3 // opts standardized to work like levelup api
4 function stdopts (opts) {
5 opts = opts || {}
6 opts.keys = opts.keys !== false //default keys to true
7 opts.values = opts.values !== false //default values to true
8 return opts
9 }
10
11 function msgFmt (keys, values, obj) {
12 if (keys && values)
13 return obj
14 if (keys)
15 return obj.key
16 if (values)
17 return obj.value
18 return null // i guess?
19 }
20
21exports.options = stdopts
22exports.format = msgFmt
23
24exports.lo = null
25exports.hi = undefined
26
27exports.wait = function () {
28 var waiting = [], value
29 return {
30 get: function () { return value },
31 set: function (_value) {
32 value = _value
33 while(waiting.length)
34 waiting.shift()(null, value)
35 },
36 wait: function (cb) {
37 if(value !== undefined) cb(null, value)
38 else waiting.push(cb)
39 }
40 }
41}
42exports.formatStream = function (keys, values) {
43 return Map(function (data) {
44 if(data.sync) return data
45 return keys && values ? data.value : keys ? data.value.key : data.value.value
46 })
47}
48
49

Built with git-ssb-web