-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.js
42 lines (36 loc) · 1.09 KB
/
stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var ReadQueue = require('pull-read-queue')
function stream (content, isTop) {
var queue = ReadQueue(5, 20, function (data) {
content.dispatchEvent(new CustomEvent('hasmore', {target: content, detail: {
bottom: !isTop, top: !!isTop, count: queue.getLength()
}}))
})
content.addEventListener('readymore', function (ev) {
if(!(isTop ? ev.detail.top : ev.detail.bottom)) return
var l = Math.min(queue.getLength(), 10)
var first
for(var i = 0; i < l; i++) {
var el = queue.shift()
if(!first) first = el
if(!isTop)
content.appendChild(el)
else {
if(content.firstChild)
content.insertBefore(el, content.firstChild)
else
content.appendChild(el)
}
}
if(queue.getLength() > 0)
content.dispatchEvent(new CustomEvent('hasmore', {target: content, detail: {
bottom: !isTop, top: !!isTop, count: queue.getLength()
}}))
})
return queue
}
exports.top = function (content, isTop) {
return stream(content, true)
}
exports.bottom = function (content) {
return stream(content, false)
}