Skip to content

Commit

Permalink
fix: some more mod view lifecycle things
Browse files Browse the repository at this point in the history
  • Loading branch information
hackergrrl committed May 22, 2020
1 parent 5f122b7 commit d6d577b
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions views/moderation.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ module.exports = function (cabal, authDb, infoDb) {
// view lifecycle
storeState: function (state, cb) {
state = state.toString('base64')
db.put('state', state, cb)
authDb.put('state', state, cb)
},

fetchState: function (cb) {
db.get('state', function (err, state) {
authDb.get('state', function (err, state) {
if (err && err.notFound) cb()
else if (err) cb(err)
else cb(null, Buffer.from(state, 'base64'))
Expand All @@ -209,22 +209,39 @@ module.exports = function (cabal, authDb, infoDb) {
clearIndex: function (cb) {
var batch = []
var maxSize = 5000
db.open(function () {
pump(db.createKeyStream(), new Writable({
let pending = 2
infoDb.open(function () {
pump(infoDb.createKeyStream(), new Writable({
objectMode: true,
write: function (key, enc, next) {
batch.push({ type: 'del', key })
if (batch.length >= maxSize) {
db.batch(batch, next)
infoDb.batch(batch, next)
} else next()
},
final: function (next) {
if (batch.length > 0) db.batch(batch, next)
if (batch.length > 0) infoDb.batch(batch, next)
else next()
}
}), ondone)
})
authDb.open(function () {
pump(authDb.createKeyStream(), new Writable({
objectMode: true,
write: function (key, enc, next) {
batch.push({ type: 'del', key })
if (batch.length >= maxSize) {
authDb.batch(batch, next)
} else next()
},
final: function (next) {
if (batch.length > 0) authDb.batch(batch, next)
else next()
}
}), ondone)
})
function ondone (err) {
if (--pending) return
if (err) cb(err)
else cb()
}
Expand Down

0 comments on commit d6d577b

Please sign in to comment.