Skip to content

Commit

Permalink
Make AMQP publisher sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed May 24, 2024
1 parent 4297191 commit 9704f54
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/amqp/lib/AmqpQueuePublisherManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('AmqpQueuePublisherManager', () => {
const fakeConsumer = new FakeConsumer(diContainer.cradle, TestEvents.updated)
await fakeConsumer.start()

const publishedMessage = await queuePublisherManager.publish(FakeConsumer.QUEUE_NAME, {
const publishedMessage = queuePublisherManager.publishSync(FakeConsumer.QUEUE_NAME, {
type: 'entity.updated',
payload: {
updatedData: 'msg',
Expand Down
20 changes: 15 additions & 5 deletions packages/amqp/lib/AmqpQueuePublisherManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,25 @@ export class AmqpQueuePublisherManager<
}
}

publish(
publish(): Promise<MessageSchemaType<SupportedEventDefinitions[number]>> {
throw new Error('Please use `publishSync` method for AMQP publisher managers')
}

publishSync(
queue: NonNullable<SupportedEventDefinitions[number]['queueName']>,
message: MessagePublishType<SupportedEventDefinitions[number]>,
precedingEventMetadata?: Partial<MetadataType>,
messageOptions?: AmqpQueueMessageOptions,
): Promise<MessageSchemaType<SupportedEventDefinitions[number]>> {
// Purpose of this override is to provide better name for the first argument
// For AMQP Queues it is going to be queue
return super.publish(queue, message, precedingEventMetadata, messageOptions)
): MessageSchemaType<SupportedEventDefinitions[number]> {
const publisher = this.targetToPublisherMap[queue]
if (!publisher) {
throw new Error(`No publisher for queue ${queue}`)
}

const messageDefinition = this.resolveMessageDefinition(queue, message)
const resolvedMessage = this.resolveMessage(messageDefinition, message, precedingEventMetadata)
publisher.publish(resolvedMessage, messageOptions)
return resolvedMessage
}

protected override resolveEventTarget(
Expand Down
16 changes: 11 additions & 5 deletions packages/core/lib/queues/AbstractPublisherManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ export abstract class AbstractPublisherManager<
if (!publisher) {
throw new Error(`No publisher for target ${eventTarget}`)
}
// ToDo optimize the lookup
const messageDefinition = this.targetToEventMap[eventTarget].find(
(entry) => entry.consumerSchema.shape.type.value === message.type,
)

const messageDefinition = this.resolveMessageDefinition(eventTarget, message)
const resolvedMessage = this.resolveMessage(messageDefinition, message, precedingEventMetadata)

if (this.isAsync) {
Expand All @@ -196,6 +192,16 @@ export abstract class AbstractPublisherManager<
return resolvedMessage
}

protected resolveMessageDefinition(
eventTarget: EventTargets,
message: MessagePublishType<SupportedEventDefinitions[number]>,
) {
// ToDo optimize the lookup
return this.targetToEventMap[eventTarget].find(
(entry) => entry.consumerSchema.shape.type.value === message.type,
)
}

protected resolveMessage(
messageDefinition: EventDefinitionType | undefined,
message: MessagePublishType<SupportedEventDefinitions[number]>,
Expand Down

0 comments on commit 9704f54

Please sign in to comment.