Skip to content

Commit

Permalink
fix: process one batch at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Oct 19, 2022
1 parent adf8b99 commit d4eea8f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/lib/blockstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,32 @@ export class BatchingR2Blockstore extends R2Blockstore {

#scheduled = false

/** @type {Promise<void>|null} */
#processing = null

#scheduleBatchProcessing () {
if (this.#scheduled) return
this.#scheduled = true
setTimeout(() => {

const startProcessing = async () => {
this.#scheduled = false
this.#processBatch()
})
const { promise, resolve } = defer()
this.#processing = promise
try {
await this.#processBatch()
} finally {
this.#processing = null
resolve()
}
}

// If already running, then start when finished
if (this.#processing) {
return this.#processing.then(startProcessing)
}

// If not running, then start on the next tick
setTimeout(startProcessing)
}

async #processBatch () {
Expand Down

0 comments on commit d4eea8f

Please sign in to comment.