git ssb

0+

dangerousbeans / patchbay-bootstrap



Tree: 29997160555aa9df4541f65ba3ca7b58d4a8c9c4

Files: 29997160555aa9df4541f65ba3ca7b58d4a8c9c4 / util.js

1522 bytesRaw
1var pull = require('pull-stream')
2var Next = require('pull-next')
3
4function first (list, test) {
5 for(var i in list) {
6 var value = test(list[i], i, list)
7 if(value) return value
8 }
9}
10
11function decorate (list, value, caller) {
12 caller = caller || function (d,e,v) { return d(e, v) }
13
14 return list.reduce(function (element, decorator) {
15 return caller(decorator, element, value) || element
16 }, null)
17}
18
19function get(obj, path) {
20 if(obj == null) return obj
21 if('string' === typeof path) return obj[path]
22 for(var i = 0; i < path.length; i++) {
23 obj = obj[path[i]]
24 if(obj == null) return
25 }
26 return obj
27
28}
29
30exports.first = first
31exports.decorate = decorate
32
33exports.next = function (createStream, opts, property, range) {
34
35 range = range || opts.reverse ? 'lt' : 'gt'
36 property = property || 'timestamp'
37
38 var last = null, count = -1
39 return Next(function () {
40 if(last) {
41 if(count === 0) return
42 var value = opts[range] = get(last, property)
43 if(value === stop) return null
44 }
45 return pull(
46 createStream(opts),
47 pull.through(function (msg) {
48 count ++
49 if(!msg.sync) last = msg
50 }, function (err) {
51 //retry on errors...
52 if(err) count = -1
53 })
54 )
55 })
56}
57
58exports.firstPlug = function (plugs) {
59 if(!Array.isArray(plugs)) throw new Error('plugs must be an array')
60 var args = [].slice.call(arguments)
61 var plugs = args.shift()
62 return exports.first(plugs, function (fn) {
63 return fn.apply(null, args)
64 })
65}
66
67

Built with git-ssb-web