Skip to content

Commit

Permalink
add helper for atomic flows
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Dec 14, 2024
1 parent 7d9ac2c commit 2bb9497
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,24 @@ class HypercoreStorage {
return new WriteBatch(this, this.db.write(), null)
}

// helper for atomic flows
async createWriteBatchAndLock (atomizer, lock) {
assert(this.destroyed === false)
if (atomizer) atomizer.enter()

try {
await lock.lock()
} catch (err) {
if (atomizer) atomizer.exit()
throw err
}

const batch = this.createWriteBatch(atomizer)
if (atomizer) atomizer.exit()

return batch
}

createBlockStream (opts = {}) {
assert(this.destroyed === false)
return createStream(this, createBlockStream, opts)
Expand Down

0 comments on commit 2bb9497

Please sign in to comment.