git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: a0d45464afed54dcb36fdf48278c914c2204a62e

Files: a0d45464afed54dcb36fdf48278c914c2204a62e / lib / next-stepper.js

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

Built with git-ssb-web