chore: Ensure the events are emitted with the same shape all accross (#8063)
* chore: Ensure the events are emitted with the same shape all accross * fixes: * rm unsues type * types * fix tests
This commit is contained in:
committed by
GitHub
parent
95f29358d1
commit
e778870c68
@@ -37,6 +37,7 @@ describe("LocalEventBusService", () => {
|
||||
expect(eventEmitter.emit).toHaveBeenCalledTimes(1)
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("eventName", {
|
||||
data: { hi: "1234" },
|
||||
eventName: "eventName",
|
||||
})
|
||||
})
|
||||
|
||||
@@ -51,9 +52,11 @@ describe("LocalEventBusService", () => {
|
||||
expect(eventEmitter.emit).toHaveBeenCalledTimes(2)
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-1", {
|
||||
data: { hi: "1234" },
|
||||
eventName: "event-1",
|
||||
})
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-2", {
|
||||
data: { hi: "5678" },
|
||||
eventName: "event-2",
|
||||
})
|
||||
})
|
||||
|
||||
@@ -144,6 +147,7 @@ describe("LocalEventBusService", () => {
|
||||
expect(eventEmitter.emit).toHaveBeenCalledTimes(1)
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-1", {
|
||||
data: { test: "1" },
|
||||
eventName: "event-1",
|
||||
})
|
||||
|
||||
expect((eventBus as any).groupedEventsMap_.get("group-1")).toHaveLength(
|
||||
@@ -155,7 +159,7 @@ describe("LocalEventBusService", () => {
|
||||
|
||||
jest.clearAllMocks()
|
||||
eventEmitter.emit = jest.fn((data) => data)
|
||||
eventBus.releaseGroupedEvents("group-1")
|
||||
await eventBus.releaseGroupedEvents("group-1")
|
||||
|
||||
expect(
|
||||
(eventBus as any).groupedEventsMap_.get("group-1")
|
||||
@@ -167,9 +171,13 @@ describe("LocalEventBusService", () => {
|
||||
expect(eventEmitter.emit).toHaveBeenCalledTimes(2)
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-1", {
|
||||
data: { test: "1" },
|
||||
eventName: "event-1",
|
||||
metadata: { eventGroupId: "group-1" },
|
||||
})
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-2", {
|
||||
data: { test: "2" },
|
||||
eventName: "event-2",
|
||||
metadata: { eventGroupId: "group-1" },
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ type InjectedDependencies = {
|
||||
logger: Logger
|
||||
}
|
||||
|
||||
type StagingQueueType = Map<string, { eventName: string; data?: unknown }[]>
|
||||
type StagingQueueType = Map<string, Message[]>
|
||||
|
||||
const eventEmitter = new EventEmitter()
|
||||
eventEmitter.setMaxListeners(Infinity)
|
||||
@@ -72,16 +72,15 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
if (eventGroupId) {
|
||||
await this.groupEvent(eventGroupId, eventData)
|
||||
} else {
|
||||
this.eventEmitter_.emit(eventData.eventName, {
|
||||
data: eventData.data,
|
||||
})
|
||||
const { options, ...eventBody } = eventData
|
||||
this.eventEmitter_.emit(eventData.eventName, eventBody)
|
||||
}
|
||||
}
|
||||
|
||||
// Groups an event to a queue to be emitted upon explicit release
|
||||
private async groupEvent<T = unknown>(
|
||||
eventGroupId: string,
|
||||
eventData: MessageBody<T>
|
||||
eventData: Message<T>
|
||||
) {
|
||||
const groupedEvents = this.groupedEventsMap_.get(eventGroupId) || []
|
||||
|
||||
@@ -94,9 +93,9 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
const groupedEvents = this.groupedEventsMap_.get(eventGroupId) || []
|
||||
|
||||
for (const event of groupedEvents) {
|
||||
const { eventName, data } = event
|
||||
const { options, ...eventBody } = event
|
||||
|
||||
this.eventEmitter_.emit(eventName, { data })
|
||||
this.eventEmitter_.emit(event.eventName, eventBody)
|
||||
}
|
||||
|
||||
this.clearGroupedEvents(eventGroupId)
|
||||
@@ -109,10 +108,9 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
subscribe(event: string | symbol, subscriber: Subscriber): this {
|
||||
const randId = ulid()
|
||||
this.storeSubscribers({ event, subscriberId: randId, subscriber })
|
||||
this.eventEmitter_.on(event, async (...args) => {
|
||||
this.eventEmitter_.on(event, async (data: MessageBody) => {
|
||||
try {
|
||||
// @ts-ignore
|
||||
await subscriber(...args)
|
||||
await subscriber(data)
|
||||
} catch (e) {
|
||||
this.logger_?.error(
|
||||
`An error occurred while processing ${event.toString()}: ${e}`
|
||||
|
||||
@@ -112,8 +112,8 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
||||
|
||||
/**
|
||||
* Emit a single or number of events
|
||||
* @param {Message} data - the data to send to the subscriber.
|
||||
* @param {BulkJobOptions} data - the options to add to bull mq
|
||||
* @param eventsData
|
||||
* @param options
|
||||
*/
|
||||
async emit<T = unknown>(
|
||||
eventsData: Message<T> | Message<T>[],
|
||||
@@ -136,10 +136,10 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
||||
|
||||
for (const event of eventsToGroup) {
|
||||
const groupId = event.metadata?.eventGroupId!
|
||||
const array = groupEventsMap.get(groupId) ?? []
|
||||
const groupEvents = groupEventsMap.get(groupId) ?? []
|
||||
|
||||
array.push(event)
|
||||
groupEventsMap.set(groupId, array)
|
||||
groupEvents.push(event)
|
||||
groupEventsMap.set(groupId, groupEvents)
|
||||
}
|
||||
|
||||
const promises: Promise<unknown>[] = []
|
||||
@@ -258,7 +258,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
||||
|
||||
const subscribersResult = await Promise.all(
|
||||
subscribersInCurrentAttempt.map(async ({ id, subscriber }) => {
|
||||
return await subscriber(data, eventName)
|
||||
return await subscriber(data)
|
||||
.then(async (data) => {
|
||||
// For every subscriber that completes successfully, add their id to the list of completed subscribers
|
||||
completedSubscribersInCurrentAttempt.push(id)
|
||||
|
||||
+5
@@ -682,6 +682,11 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
expect.objectContaining({
|
||||
data: { id: productCategoryOne.id },
|
||||
eventName: "product-category.deleted",
|
||||
metadata: {
|
||||
action: "",
|
||||
object: "",
|
||||
source: "",
|
||||
},
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
+5
@@ -282,6 +282,11 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
{
|
||||
eventName: "product-collection.deleted",
|
||||
data: { id: collectionId },
|
||||
metadata: {
|
||||
action: "",
|
||||
object: "",
|
||||
source: "",
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user