git ssb

30+

cel / git-ssb-web



Tree: 2ad0196e8b9972306535f04ebe1f9821a913c758

Files: 2ad0196e8b9972306535f04ebe1f9821a913c758 / lib / paginate.js

1145 bytesRaw
1module.exports = function (onFirst, through, onLast, onEmpty) {
2 var ended, last, first = true, queue = []
3 return function (read) {
4 var mappedRead = through(function (end, cb) {
5 if (ended = end) return read(ended, cb)
6 if (queue.length)
7 return cb(null, queue.shift())
8 read(null, function (end, data) {
9 if (end) return cb(end)
10 last = data
11 cb(null, data)
12 })
13 })
14 return function (end, cb) {
15 var tmp
16 if (ended) return cb(ended)
17 if (ended = end) return read(ended, cb)
18 if (first)
19 return read(null, function (end, data) {
20 if (ended = end) {
21 if (end === true && onEmpty)
22 return onEmpty(cb)
23 return cb(ended)
24 }
25 first = false
26 last = data
27 queue.push(data)
28 if (onFirst)
29 onFirst(data, cb)
30 else
31 mappedRead(null, cb)
32 })
33 mappedRead(null, function (end, data) {
34 if (ended = end) {
35 if (end === true && last)
36 return onLast(last, cb)
37 }
38 cb(end, data)
39 })
40 }
41 }
42}
43
44

Built with git-ssb-web