import type { OutgoingMessage } from '../messages/OutgoingMessage'; import { ClientTransaction } from './ClientTransaction'; export class ClientTransactionManager extends Map { private counter = 0; private maxCounter = 100000; constructor(public readonly prefix: string) { super(); } public create(): ClientTransaction { const num = this.next(); const transaction = new ClientTransaction(`${this.prefix}-${num}`); this.set(transaction.id, transaction); return transaction; } public end(message: OutgoingMessage): boolean { const { transactionId } = message; if (transactionId && this.has(transactionId)) { const transaction = this.get(transactionId); transaction!.reply(message); this.delete(transactionId); } return false; } private next(): number { if (this.counter >= this.maxCounter) { this.counter = 1; } else { this.counter += 1; } return this.counter; } }