import { EmitArg, EventBus, EventBusEmit } from '@/modules/shared/services/eventBus' import { Knex } from 'knex' export const commandFactory = ) => ReturnType>({ db, eventBus, operationFactory }: { db: Knex eventBus: EventBus operationFactory: (arg: { db: Knex; emit: EventBusEmit }) => TOperation }) => async (...args: Parameters): Promise>> => { const trx = await db.transaction() const events: EmitArg[] = [] const emit: EventBusEmit = async ({ eventName, payload }) => { events.push({ eventName, payload }) } const result = await operationFactory({ db, emit })(...args) await trx.commit() for (const event of events) { eventBus.emit(event) } return result as Awaited> }