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