Function
wrapTransaction
(apply, fn, db, { begin, commit, rollback, savepoint, release, rollbackTo })
Source from the content-addressed store, hash-verified
| 50 | |
| 51 | // Return a new transaction function by wrapping the given function |
| 52 | const wrapTransaction = (apply, fn, db, { begin, commit, rollback, savepoint, release, rollbackTo }) => function sqliteTransaction() { |
| 53 | let before, after, undo; |
| 54 | if (db.inTransaction) { |
| 55 | before = savepoint; |
| 56 | after = release; |
| 57 | undo = rollbackTo; |
| 58 | } else { |
| 59 | before = begin; |
| 60 | after = commit; |
| 61 | undo = rollback; |
| 62 | } |
| 63 | before.run(); |
| 64 | try { |
| 65 | const result = apply.call(fn, this, arguments); |
| 66 | if (result && typeof result.then === 'function') { |
| 67 | throw new TypeError('Transaction function cannot return a promise'); |
| 68 | } |
| 69 | after.run(); |
| 70 | return result; |
| 71 | } catch (ex) { |
| 72 | if (db.inTransaction) { |
| 73 | undo.run(); |
| 74 | if (undo !== rollback) after.run(); |
| 75 | } |
| 76 | throw ex; |
| 77 | } |
| 78 | }; |
Tested by
no test coverage detected