Files: 545734ec6384c1b2b71f539ea4acfe56af123b1e / sources.js
2848 bytesRaw
1 | |
2 | var keys = exports.keys = |
3 | function (object) { |
4 | return values(Object.keys(object)) |
5 | } |
6 | |
7 | var values = exports.values = exports.readArray = |
8 | function (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 | |
22 | var count = exports.count = |
23 | function (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 | |
33 | var infinite = exports.infinite = |
34 | function (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 | |
42 | var 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 | |
67 | var depthFirst = exports.depthFirst = |
68 | function (start, createStream) { |
69 | var reads = [] |
70 | |
71 | reads.unshift(createStream(start)) |
72 | |
73 | return function next (end, cb) { |
74 | if(!reads.length) |
75 | return cb(true) |
76 | reads[0](end, function (end, data) { |
77 | if(end) { |
78 | //if this stream has ended, go to the next queue |
79 | reads.shift() |
80 | return next(null, cb) |
81 | } |
82 | reads.unshift(createStream(data)) |
83 | cb(end, data) |
84 | }) |
85 | } |
86 | } |
87 | //width first is just like depth first, |
88 | //but push each new stream onto the end of the queue |
89 | var widthFirst = exports.widthFirst = |
90 | function (start, createStream) { |
91 | var reads = [] |
92 | |
93 | reads.push(createStream(start)) |
94 | |
95 | return function next (end, cb) { |
96 | if(!reads.length) |
97 | return cb(true) |
98 | reads[0](end, function (end, data) { |
99 | if(end) { |
100 | reads.shift() |
101 | return next(null, cb) |
102 | } |
103 | reads.push(createStream(data)) |
104 | cb(end, data) |
105 | }) |
106 | } |
107 | } |
108 | |
109 | //this came out different to the first (strm) |
110 | //attempt at leafFirst, but it's still a valid |
111 | //topological sort. |
112 | var leafFirst = exports.leafFirst = |
113 | function (start, createStream) { |
114 | var reads = [] |
115 | var output = [] |
116 | reads.push(createStream(start)) |
117 | |
118 | return function next (end, cb) { |
119 | reads[0](end, function (end, data) { |
120 | if(end) { |
121 | reads.shift() |
122 | if(!output.length) |
123 | return cb(true) |
124 | return cb(null, output.shift()) |
125 | } |
126 | reads.unshift(createStream(data)) |
127 | output.unshift(data) |
128 | next(null, cb) |
129 | }) |
130 | } |
131 | } |
132 | |
133 |
Built with git-ssb-web