Files
medusa-store/packages/core/medusa-test-utils/src/events.ts
Stevche Radevski ee924b1b28 feat: Add a simple configurable notifications subscriber (#7331)
* feat: Add a simple configurable notifications subscriber that is configurable

* Proposal on awaiting all subscribers to run

* fix: Clean up wait subscribers util and notifications test

---------

Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
2024-05-16 13:36:09 +02:00

29 lines
878 B
TypeScript

import { IEventBusModuleService } from "@medusajs/types"
// Allows you to wait for all subscribers to execute for a given event. Only works with the local event bus.
export const waitSubscribersExecution = (
eventName: string,
eventBus: IEventBusModuleService
) => {
const subscriberPromises: Promise<any>[] = []
;(eventBus as any).eventEmitter_.listeners(eventName).forEach((listener) => {
;(eventBus as any).eventEmitter_.removeListener("order.created", listener)
let ok, nok
const promise = new Promise((resolve, reject) => {
ok = resolve
nok = reject
})
subscriberPromises.push(promise)
const newListener = async (...args2) => {
return await listener.apply(eventBus, args2).then(ok).catch(nok)
}
;(eventBus as any).eventEmitter_.on("order.created", newListener)
})
return Promise.all(subscriberPromises)
}