import { Transaction } from './Transaction'; import { ConstructorType } from '../types'; export class TransactionManager { private createdTransactions: Transaction[] = []; constructor(private transaction: Map, Transaction> = new Map()) {} setTransaction(key: string | Symbol | ConstructorType, transaction: Transaction) { return this.transaction.set(key, transaction); } hasTransaction(key: string | Symbol | ConstructorType) { return this.transaction.has(key); } deleteTransaction(key: string | Symbol | ConstructorType) { return this.transaction.delete(key); } getTransaction(type: string | Symbol | ConstructorType): Transaction | undefined { const transaction = this.transaction.get(type); if (transaction) { this.createdTransactions.push(transaction); return transaction as Transaction; } else { return undefined; } } async try() { // console.log('try', this.transaction); } // async catch(e: any) { // console.log('error', e, this.transaction); for (let createdTransaction of this.createdTransactions) { try { await createdTransaction.catch(e); } catch (e) {} } } async finally() { // console.log('finally', this.transaction); for (let createdTransaction of this.createdTransactions) { try { await createdTransaction.finally(); } catch (e) {} } this.transaction.clear(); } }