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:
1 parent
2e42e053d4
commit
ee924b1b28
27 files changed
+295
-740
No files matched your search
@@ -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)
|
||||
}
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -6,7 +6,7 @@ export const StockLocationModule = {
|
||||
label: "StockLocationService",
|
||||
isRequired: false,
|
||||
isQueryable: true,
|
||||
dependencies: ["eventBusService"],
|
||||
dependencies: ["eventBusModuleService"],
|
||||
defaultModuleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
|
||||
@@ -113,7 +113,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
label: upperCaseFirst(ModuleRegistrationName.STOCK_LOCATION),
|
||||
isRequired: false,
|
||||
isQueryable: true,
|
||||
dependencies: ["eventBusService"],
|
||||
dependencies: [ModuleRegistrationName.EVENT_BUS],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
@@ -126,7 +126,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
label: upperCaseFirst(ModuleRegistrationName.INVENTORY),
|
||||
isRequired: false,
|
||||
isQueryable: true,
|
||||
dependencies: ["eventBusService"],
|
||||
dependencies: [ModuleRegistrationName.EVENT_BUS],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
@@ -228,7 +228,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
label: upperCaseFirst(ModuleRegistrationName.FULFILLMENT),
|
||||
isRequired: false,
|
||||
isQueryable: true,
|
||||
dependencies: ["logger", "eventBusService"],
|
||||
dependencies: ["logger", ModuleRegistrationName.EVENT_BUS],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
@@ -306,7 +306,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
label: upperCaseFirst(ModuleRegistrationName.ORDER),
|
||||
isRequired: false,
|
||||
isQueryable: true,
|
||||
dependencies: ["logger", "eventBusService"],
|
||||
dependencies: ["logger", ModuleRegistrationName.EVENT_BUS],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
@@ -319,7 +319,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
label: upperCaseFirst(ModuleRegistrationName.TAX),
|
||||
isRequired: false,
|
||||
isQueryable: true,
|
||||
dependencies: ["logger", "eventBusService"],
|
||||
dependencies: ["logger", ModuleRegistrationName.EVENT_BUS],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
|
||||
@@ -571,13 +571,8 @@ export function abstractModuleServiceFactory<
|
||||
// TODO: Should use ModuleRegistrationName.EVENT_BUS but it would require to move it to the utils package to prevent circular dependencies
|
||||
(key) => key === "eventBusModuleService"
|
||||
)
|
||||
const hasEventBusService = Object.keys(this.__container__).find(
|
||||
(key) => key === "eventBusService"
|
||||
)
|
||||
|
||||
this.eventBusModuleService_ = hasEventBusService
|
||||
? this.__container__.eventBusService
|
||||
: hasEventBusModuleService
|
||||
this.eventBusModuleService_ = hasEventBusModuleService
|
||||
? this.__container__.eventBusModuleService
|
||||
: undefined
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user