export class TransactionManager { constructor (options = {}) { this._isInTransaction = false this._rollbacks = [] } begin () { if (this._isInTransaction) { throw (new Error('Already in transaction status.')) } this._rollbacks = [] this._isInTransaction = true } addRollback (rollback) { this._rollbacks.push(rollback) } triggerRollback () { for (let i = this._rollbacks.length - 1; i >= 0; i--) { this._rollbacks[i]() } } success () { this.end() } fail () { this.triggerRollback() this.end() } end () { this._isInTransaction = false this._rollbacks = [] } }