feat: Add emitEvent step + cleanup (#7643)

* feat: Add emitEvent step + cleanup

* fix typo

* fix typo
This commit is contained in:
Adrien de Peretti
2024-06-07 11:52:19 +02:00
committed by GitHub
parent 3cd2d60daa
commit 2e77a076b8
19 changed files with 178 additions and 117 deletions

View File

@@ -1,6 +1,7 @@
export * from "./steps/create-remote-links"
export * from "./steps/dismiss-remote-links"
export * from "./steps/remove-remote-links"
export * from "./steps/emit-event"
export * from "./steps/use-remote-query"
export * from "./workflows/batch-links"
export * from "./workflows/create-links"

View File

@@ -0,0 +1,41 @@
import { composeMessage, ModuleRegistrationName } from "@medusajs/utils"
import { createStep, StepExecutionContext } from "@medusajs/workflows-sdk"
type Input = {
eventName: string
source: string
object: string
action?: string
options?: Record<string, any>
data: (
context: StepExecutionContext
) => Promise<Record<any, any>> | Record<any, any>
}
export const emitEventStepId = "emit-event-step"
export const emitEventStep = createStep(
emitEventStepId,
async (input: Input, context) => {
if (!input) {
return
}
const { container } = context
const eventBus = container.resolve(ModuleRegistrationName.EVENT_BUS)
const data_ =
typeof input.data === "function" ? await input.data(context) : input.data
const message = composeMessage(input.eventName, {
data: data_,
action: input.action ?? "",
object: input.object,
source: input.source,
options: input.options,
context,
})
await eventBus.emit([message])
},
async (data: void) => {}
)

View File

@@ -0,0 +1,20 @@
import { ModuleRegistrationName } from "@medusajs/utils"
import { createStep } from "@medusajs/workflows-sdk"
export const releaseEventsStepId = "release-events-step"
export const releaseEventsStep = createStep(
releaseEventsStepId,
async (
input: void,
{
container,
metadata: {
/* eventGroupId */
},
}
) => {
const eventBus = container.resolve(ModuleRegistrationName.EVENT_BUS)
// await eventBus.release
},
async (data: void) => {}
)