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>
This commit is contained in:
Stevche Radevski
2024-05-16 13:36:09 +02:00
committed by GitHub
co-authored by adrien2p
parent 2e42e053d4
commit ee924b1b28
27 changed files with 295 additions and 740 deletions
@@ -0,0 +1,28 @@
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)
}
+2 -1
View File
@@ -1,6 +1,7 @@
export * as TestDatabaseUtils from "./database"
export { default as IdMap } from "./id-map"
export * as TestEventUtils from "./events"
export * as JestUtils from "./jest"
export { default as IdMap } from "./id-map"
export { default as MockManager } from "./mock-manager"
export { default as MockRepository } from "./mock-repository"
export * from "./init-modules"
@@ -78,7 +78,7 @@ export function moduleIntegrationTestRunner({
const moduleOptions_: InitModulesOptions = {
injectedDependencies: {
[ContainerRegistrationKeys.PG_CONNECTION]: connection,
eventBusService: new MockEventBusService(),
["eventBusModuleService"]: new MockEventBusService(),
[ContainerRegistrationKeys.LOGGER]: console,
...injectedDependencies,
},