git ssb

2+

Dominic / pull-stream



Tree: 5f1a1047fd206aafe8d09749bcde2ffbf78cc7b1

Files: 5f1a1047fd206aafe8d09749bcde2ffbf78cc7b1 / sources.js

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

Built with git-ssb-web