-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimpl-custom-seq.js
43 lines (40 loc) · 1.03 KB
/
impl-custom-seq.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
42
43
const {
Promise,
co,
promisify,
hexint
} = require('./utils')
module.exports = function ({ createQueueStream }) {
return {
tip: co(function* ({ queue, getCheckpoint }) {
// get the seq of last dequeued item
let prev = yield getCheckpoint()
return new Promise(resolve => {
const stream = createQueueStream(queue, { values: false })
.on('data', function ({ seq }) {
if (prev && seq > prev + 1) {
// we hit a gap
resolve(prev)
stream.destroy()
}
prev = seq
})
.on('end', () => resolve(prev))
})
}),
batchEnqueuer: function ({ db, queue }) {
const batchAsync = promisify(db.batch.bind(db))
return co(function* ({ data }) {
const batch = data.map(({ seq, value }) => {
return {
type: 'put',
key: hexint(seq),
value
}
})
yield batchAsync(batch)
return data.map(data => data.seq)
})
}
}
}