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