Skip to content

Commit

Permalink
Async transact
Browse files Browse the repository at this point in the history
  • Loading branch information
matttylr committed Oct 28, 2024
1 parent 44e1600 commit 99d2d9f
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,44 @@ class Store {
try {
const result = f()
if (ownTxn) {
if (readonly)
if (readonly) {
console.log('aborting txn')
this.txn.abort()
else
}
else {
console.log('committing txn')
this.txn.commit()
}
}
return result
} catch (error) {
console.error('Transaction aborted due to an error:', error.message)
this.txn.abort()
throw error
} finally {
if (ownTxn)
this.txn = null
}
}

async transactAsync(f, readonly = false) {
let ownTxn = false
if (!this.txn) {
this.txn = this.env.beginTxn()
ownTxn = true
}

try {
const result = await f()
if (ownTxn) {
if (readonly) {
console.log('aborting txn')
this.txn.abort()
}
else {
console.log('committing txn')
this.txn.commit()
}
}
return result
} catch (error) {
Expand All @@ -170,6 +204,7 @@ class Store {
}
}


iterate() {
return new Iterator(this.env, this.dbi)
}
Expand Down

0 comments on commit 99d2d9f

Please sign in to comment.