feat(core, event-bus): Compensate emit event step utility (#13281)

* feat(core, event-bus): Compensate emit event step utility

* tests

* Update changeset to remove integration-tests-modules

Removed integration-tests-modules from changeset.

* revert test script
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2025-08-25 15:08:09 +02:00
1 parent 73a25abecb
commit fc49253273
11 files changed
+547 -22

No files matched your search

@@ -5,6 +5,7 @@ import {
import { Modules } from "@medusajs/framework/utils"
import {
StepExecutionContext,
StepResponse,
createStep,
} from "@medusajs/framework/workflows-sdk"
@@ -85,6 +86,25 @@ export const emitEventStep = createStep(
}
await eventBus.emit(message)
return new StepResponse({
eventGroupId: context.eventGroupId,
eventName: input.eventName,
})
},
async (data: void) => {}
async (data, context) => {
if (!data || !data?.eventGroupId) {
return
}
const { container } = context
const eventBus: IEventBusModuleService = container.resolve(
Modules.EVENT_BUS
)
await eventBus.clearGroupedEvents(data!.eventGroupId, {
eventNames: [data!.eventName],
})
}
)
@@ -3,14 +3,14 @@ import { Message, Subscriber, SubscriberContext } from "./common"
export interface IEventBusModuleService {
/**
* This method emits one or more events. Subscribers listening to the event(s) are executed asynchronously.
*
*
* @param data - The details of the events to emit.
* @param options - Additional options for the event.
*
*
* @example
* await eventModuleService.emit({
* name: "user.created",
* data: {
* await eventModuleService.emit({
* name: "user.created",
* data: {
* user_id: "user_123"
* }
* })
@@ -22,12 +22,12 @@ export interface IEventBusModuleService {
/**
* This method adds a subscriber to an event. It's mainly used internally to register subscribers.
*
*
* @param eventName - The name of the event to subscribe to.
* @param subscriber - The subscriber function to execute when the event is emitted.
* @param context - The context of the subscriber.
* @returns The instance of the Event Module
*
*
* @example
* eventModuleService.subscribe("user.created", async (data) => {
* console.log("User created", data)
@@ -41,12 +41,12 @@ export interface IEventBusModuleService {
/**
* This method removes a subscriber from an event. It's mainly used internally to unregister subscribers.
*
*
* @param eventName - The name of the event to unsubscribe from.
* @param subscriber - The subscriber function to remove.
* @param context - The context of the subscriber.
* @returns The instance of the Event Module
*
*
* @example
* eventModuleService.unsubscribe("user.created", async (data) => {
* console.log("User created", data)
@@ -61,9 +61,9 @@ export interface IEventBusModuleService {
/**
* This method emits all events in the specified group. Grouped events are useful when you have distributed transactions
* where you need to explicitly group, release and clear events upon lifecycle events of a transaction.
*
*
* @param eventGroupId - The ID of the event group.
*
*
* @example
* await eventModuleService.releaseGroupedEvents("group_123")
*/
@@ -71,11 +71,19 @@ export interface IEventBusModuleService {
/**
* This method removes all events in the specified group. Grouped events are useful when you have distributed transactions
* where you need to explicitly group, release and clear events upon lifecycle events of a transaction.
*
*
* @param eventGroupId - The ID of the event group.
*
* @param options - Additional options for the event.
* @param options.eventNames - The names of the events to clear. If not provided, The group will
* be entirely cleared.
*
* @example
* await eventModuleService.clearGroupedEvents("group_123")
*/
clearGroupedEvents(eventGroupId: string): Promise<void>
clearGroupedEvents(
eventGroupId: string,
options?: {
eventNames?: string[]
}
): Promise<void>
}
+10 -2
View File
@@ -38,8 +38,16 @@ export abstract class AbstractEventBusModuleService
*/
// Given a eventGroupId, all the grouped events will be released
abstract releaseGroupedEvents(eventGroupId: string): Promise<void>
// Given a eventGroupId, all the grouped events will be cleared
abstract clearGroupedEvents(eventGroupId: string): Promise<void>
// Given a eventGroupId, all the grouped events will be cleared unless eventNames are provided
// If eventNames are provided, only the events that match the eventNames will be cleared from the
// group
abstract clearGroupedEvents(
eventGroupId: string,
options?: {
eventNames?: string[]
}
): Promise<void>
protected storeSubscribers({
event,