git ssb

16+

Dominic / patchbay



Tree: edabe8be49e24684f650eea0bbebd38e65df4f9f

Files: edabe8be49e24684f650eea0bbebd38e65df4f9f / util.js

1099 bytesRaw
1var pull = require('pull-stream')
2var Next = require('pull-next')
3
4function get (obj, path) {
5 if(!obj) return undefined
6 if('string' === typeof path) return obj[path]
7 if(Array.isArray(path)) {
8 for(var i = 0; obj && i < path.length; i++)
9 obj = obj[path[i]]
10 return obj
11 }
12}
13
14function clone (obj) {
15 var _obj = {}
16 for(var k in obj) _obj[k] = obj[k]
17 return _obj
18}
19
20exports.next = function (createStream, opts, property, range) {
21
22 range = range || (opts.reverse ? 'lt' : 'gt')
23 property = property || 'timestamp'
24
25 var last = null, count = -1
26 return Next(function () {
27 if(last) {
28 if(count === 0) return
29 var value = opts[range] = get(last, property)
30 if(value == null) return
31 last = null
32 }
33 return pull(
34 createStream(clone(opts)),
35 pull.through(function (msg) {
36 count ++
37 if(!msg.sync) {
38 last = msg
39 }
40 }, function (err) {
41 //retry on errors...
42 if(err) return count = -1
43 //end stream if there were no results
44 if(last == null) last = {}
45 })
46 )
47 })
48}
49
50
51
52

Built with git-ssb-web