git ssb

2+

Dominic / pull-stream



Tree: 9c7a2beba02414831d4a08c2de2021926e39c837

Files: 9c7a2beba02414831d4a08c2de2021926e39c837 / sources.js

3137 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
91 return function next (end, cb) {
92 if(!reads.length)
93 return cb(true)
94 reads[0](end, function (end, data) {
95 if(end) {
96 //if this stream has ended, go to the next queue
97 reads.shift()
98 return next(null, cb)
99 }
100 reads.unshift(createStream(data))
101 cb(end, data)
102 })
103 }
104}
105//width first is just like depth first,
106//but push each new stream onto the end of the queue
107var widthFirst = exports.widthFirst =
108function (start, createStream) {
109 var reads = []
110
111 reads.push(once(start))
112
113 return function next (end, cb) {
114 if(!reads.length)
115 return cb(true)
116 reads[0](end, function (end, data) {
117 if(end) {
118 reads.shift()
119 return next(null, cb)
120 }
121 reads.push(createStream(data))
122 cb(end, data)
123 })
124 }
125}
126
127//this came out different to the first (strm)
128//attempt at leafFirst, but it's still a valid
129//topological sort.
130var leafFirst = exports.leafFirst =
131function (start, createStream) {
132 var reads = []
133 var output = []
134 reads.push(once(start))
135
136 return function next (end, cb) {
137 reads[0](end, function (end, data) {
138 if(end) {
139 reads.shift()
140 if(!output.length)
141 return cb(true)
142 return cb(null, output.shift())
143 }
144 reads.unshift(createStream(data))
145 output.unshift(data)
146 next(null, cb)
147 })
148 }
149}
150
151

Built with git-ssb-web