git ssb

2+

Dominic / pull-stream



Tree: d16bf70750241a1ef2c32bf1af8d11c04852c21f

Files: d16bf70750241a1ef2c32bf1af8d11c04852c21f / sources.js

3184 bytesRaw
1
2var keys = exports.keys =
3function (object) {
4 return values(Object.keys(object))
5}
6
7var once = exports.once =
8function (value) {
9 return function (abort, cb) {
10 if(abort) return cb(abort)
11 if(value != null) {
12 var _value = value; value = null
13 cb(null, _value)
14 } else
15 cb(true)
16 }
17}
18
19var values = exports.values = exports.readArray =
20function (array) {
21 if(!Array.isArray(array))
22 array = Object.keys(array).map(function (k) {
23 return array[k]
24 })
25 var i = 0
26 return function (end, cb) {
27 if(end)
28 return cb && cb(end)
29 cb(i >= array.length || null, array[i++])
30 }
31}
32
33
34var count = exports.count =
35function (max) {
36 var i = 0; max = max || Infinity
37 return function (end, cb) {
38 if(end) return cb && cb(end)
39 if(i > max)
40 return cb(true)
41 cb(null, i++)
42 }
43}
44
45var infinite = exports.infinite =
46function (generate) {
47 generate = generate || Math.random
48 return function (end, cb) {
49 if(end) return cb && cb(end)
50 return cb(null, generate())
51 }
52}
53
54var defer = exports.defer = function () {
55 var _read, cbs = [], _end
56
57 var read = function (end, cb) {
58 if(!_read) {
59 _end = end
60 cbs.push(cb)
61 }
62 else _read(end, cb)
63 }
64 read.resolve = function (read) {
65 if(_read) throw new Error('already resolved')
66 _read = read
67 if(!_read) throw new Error('no read cannot resolve!' + _read)
68 while(cbs.length)
69 _read(_end, cbs.shift())
70 }
71 read.abort = function(err) {
72 read.resolve(function (_, cb) {
73 cb(err || true)
74 })
75 }
76 return read
77}
78
79var empty = exports.empty = function () {
80 return function (abort, cb) {
81 cb(true)
82 }
83}
84
85var depthFirst = exports.depthFirst =
86function (start, createStream) {
87 var reads = []
88
89 reads.unshift(once(start))
90// reads.unshift(createStream(start))
91
92 return function next (end, cb) {
93 if(!reads.length)
94 return cb(true)
95 reads[0](end, function (end, data) {
96 if(end) {
97 //if this stream has ended, go to the next queue
98 reads.shift()
99 return next(null, cb)
100 }
101 reads.unshift(createStream(data))
102 cb(end, data)
103 })
104 }
105}
106//width first is just like depth first,
107//but push each new stream onto the end of the queue
108var widthFirst = exports.widthFirst =
109function (start, createStream) {
110 var reads = []
111
112 reads.push(once(start))
113
114 return function next (end, cb) {
115 if(!reads.length)
116 return cb(true)
117 reads[0](end, function (end, data) {
118 if(end) {
119 reads.shift()
120 return next(null, cb)
121 }
122 reads.push(createStream(data))
123 cb(end, data)
124 })
125 }
126}
127
128//this came out different to the first (strm)
129//attempt at leafFirst, but it's still a valid
130//topological sort.
131var leafFirst = exports.leafFirst =
132function (start, createStream) {
133 var reads = []
134 var output = []
135 reads.push(once(start))
136
137 return function next (end, cb) {
138 reads[0](end, function (end, data) {
139 if(end) {
140 reads.shift()
141 if(!output.length)
142 return cb(true)
143 return cb(null, output.shift())
144 }
145 reads.unshift(createStream(data))
146 output.unshift(data)
147 next(null, cb)
148 })
149 }
150}
151
152

Built with git-ssb-web