Files: 4087ed91f7e663172d051c9afe142fa7b7669f1e / lib / pull-group-until.js
846 bytesRaw
1 | module.exports = function GroupUntil (check) { |
2 | let ended = false |
3 | let queue = [] |
4 | return function (read) { |
5 | return function (end, cb) { |
6 | // this means that the upstream is sending an error. |
7 | if (end) { |
8 | ended = end |
9 | return read(ended, cb) |
10 | } |
11 | // this means that we read an end before. |
12 | if (ended) return cb(ended) |
13 | |
14 | read(null, function next (end, data) { |
15 | ended = ended || end |
16 | |
17 | if (ended) { |
18 | if (!queue.length) { |
19 | return cb(ended) |
20 | } |
21 | |
22 | const _queue = queue |
23 | queue = [] |
24 | return cb(null, _queue) |
25 | } |
26 | |
27 | if (check(queue, data)) { |
28 | queue.push(data) |
29 | read(null, next) |
30 | } else { |
31 | const _queue = queue |
32 | queue = [data] |
33 | cb(null, _queue) |
34 | } |
35 | }) |
36 | } |
37 | } |
38 | } |
39 |
Built with git-ssb-web