Skip to content

Commit

Permalink
Simplify any handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosGamero committed Sep 2, 2024
1 parent 74ff154 commit 7f6c902
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions packages/core/lib/events/DomainEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export class DomainEventEmitter<SupportedEvents extends CommonEventDefinition[]>
string,
Handlers<EventHandler<CommonEventDefinitionPublisherSchemaType<SupportedEvents[number]>>>
>
private readonly anyHandlers: Handlers<AnyEventHandler<SupportedEvents>>

constructor(
deps: DomainEventEmitterDependencies<SupportedEvents>,
options: {
Expand All @@ -68,7 +66,6 @@ export class DomainEventEmitter<SupportedEvents extends CommonEventDefinition[]>
resolveHandlerSpy<CommonEventDefinitionConsumerSchemaType<SupportedEvents[number]>>(options)

this.eventHandlerMap = {}
this.anyHandlers = { background: [], foreground: [] }
}

get handlerSpy(): PublicHandlerSpy<
Expand Down Expand Up @@ -161,8 +158,9 @@ export class DomainEventEmitter<SupportedEvents extends CommonEventDefinition[]>
* Register handler for all events supported by the emitter
*/
public onAny(handler: AnyEventHandler<SupportedEvents>, isBackgroundHandler = false) {
if (isBackgroundHandler) this.anyHandlers.background.push(handler)
else this.anyHandlers.foreground.push(handler)
for (const supportedEvent of this.eventRegistry.supportedEvents) {
this.on(supportedEvent.consumerSchema.shape.type.value, handler, isBackgroundHandler)
}
}

private async handleEvent<SupportedEvent extends SupportedEvents[number]>(
Expand All @@ -173,8 +171,7 @@ export class DomainEventEmitter<SupportedEvents extends CommonEventDefinition[]>
background: [],
}

const fgHandlers = [...eventHandlers.foreground, ...this.anyHandlers.foreground]
for (const handler of fgHandlers) {
for (const handler of eventHandlers.foreground) {
const transactionId = randomUUID()
let isSuccessfull = false
try {
Expand All @@ -190,8 +187,7 @@ export class DomainEventEmitter<SupportedEvents extends CommonEventDefinition[]>
}
}

const bgHandlers = [...eventHandlers.background, ...this.anyHandlers.background]
for (const handler of bgHandlers) {
for (const handler of eventHandlers.background) {
const transactionId = randomUUID()
// not sure if we should use startWithGroup or start, using group to group all handlers for the same event type
// should it be eventId + eventType or just eventType?
Expand Down

0 comments on commit 7f6c902

Please sign in to comment.