-
Notifications
You must be signed in to change notification settings - Fork 0
/
satiate.js
36 lines (36 loc) · 1.2 KB
/
satiate.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
module.exports = function (source, amount) {
let got = []
const iterator = {
done: false,
type: source.type,
next (trampoline, consume, terminator = iterator) {
source.next(trampoline, items => {
if (got.length == 0) {
if (items.length >= amount) {
consume(items)
} else {
got.push.apply(got, items)
trampoline.sync(() => iterator.next(trampoline, consume, terminator))
}
} else {
got.push.apply(got, items)
if (got.length >= amount) {
const items = got
got = []
consume(items)
} else {
trampoline.sync(() => iterator.next(trampoline, consume, terminator))
}
}
}, {
set done (value) {
terminator.done = value
if (got.length != 0) {
consume(got)
}
}
})
}
}
return iterator
}