Files: b8b3160361f94ba38bb14788f0aee273b4d1bc99 / util.js
990 bytesRaw
1 | var pull = require('pull-stream') |
2 | var Next = require('pull-next') |
3 | |
4 | function 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; i < path.length; i++) |
9 | obj = obj[path[i]] |
10 | return obj |
11 | } |
12 | } |
13 | |
14 | exports.next = function (createStream, opts, property, range) { |
15 | |
16 | range = range || (opts.reverse ? 'lt' : 'gt') |
17 | property = property || 'timestamp' |
18 | |
19 | var last = null, count = -1 |
20 | return Next(function () { |
21 | if(last) { |
22 | if(count === 0) return |
23 | var value = opts[range] = get(last, property) |
24 | if(value == null) return |
25 | last = null |
26 | } |
27 | return pull( |
28 | createStream(opts), |
29 | pull.through(function (msg) { |
30 | count ++ |
31 | if(!msg.sync) { |
32 | last = msg |
33 | } |
34 | }, function (err) { |
35 | //retry on errors... |
36 | if(err) return count = -1 |
37 | //end stream if there were no results |
38 | if(last == null) last = {} |
39 | }) |
40 | ) |
41 | }) |
42 | } |
43 | |
44 |
Built with git-ssb-web