Skip to content

Commit 99d2d9f

Browse files
committed
Async transact
1 parent 44e1600 commit 99d2d9f

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

store.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,44 @@ class Store {
154154
try {
155155
const result = f()
156156
if (ownTxn) {
157-
if (readonly)
157+
if (readonly) {
158+
console.log('aborting txn')
158159
this.txn.abort()
159-
else
160+
}
161+
else {
162+
console.log('committing txn')
163+
this.txn.commit()
164+
}
165+
}
166+
return result
167+
} catch (error) {
168+
console.error('Transaction aborted due to an error:', error.message)
169+
this.txn.abort()
170+
throw error
171+
} finally {
172+
if (ownTxn)
173+
this.txn = null
174+
}
175+
}
176+
177+
async transactAsync(f, readonly = false) {
178+
let ownTxn = false
179+
if (!this.txn) {
180+
this.txn = this.env.beginTxn()
181+
ownTxn = true
182+
}
183+
184+
try {
185+
const result = await f()
186+
if (ownTxn) {
187+
if (readonly) {
188+
console.log('aborting txn')
189+
this.txn.abort()
190+
}
191+
else {
192+
console.log('committing txn')
160193
this.txn.commit()
194+
}
161195
}
162196
return result
163197
} catch (error) {
@@ -170,6 +204,7 @@ class Store {
170204
}
171205
}
172206

207+
173208
iterate() {
174209
return new Iterator(this.env, this.dbi)
175210
}

0 commit comments

Comments
 (0)