git ssb

2+

Dominic / pull-stream



Tree: fb4e1d87afd52b57bc2b3b930addc0fe05d9f047

Files: fb4e1d87afd52b57bc2b3b930addc0fe05d9f047 / sources.js

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

Built with git-ssb-web