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:
Adrien de Peretti
2024-07-10 19:34:28 +02:00
committed by GitHub
parent 95f29358d1
commit e778870c68
9 changed files with 42 additions and 30 deletions
@@ -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}`