chore: workflow internals improvementss (#9455)

This commit is contained in:
Adrien de Peretti
2024-10-10 09:11:56 +02:00
committed by GitHub
parent 1b9379be62
commit 34d57870ad
26 changed files with 354 additions and 206 deletions
+23 -1
View File
@@ -1,9 +1,15 @@
import { EventBusTypes } from "@medusajs/types"
import {
EventBusTypes,
InternalModuleDeclaration,
MedusaContainer,
} from "@medusajs/types"
import { ulid } from "ulid"
export abstract class AbstractEventBusModuleService
implements EventBusTypes.IEventBusModuleService
{
protected isWorkerMode: boolean = true
protected eventToSubscribersMap_: Map<
string | symbol,
EventBusTypes.SubscriberDescriptor[]
@@ -16,6 +22,14 @@ export abstract class AbstractEventBusModuleService
return this.eventToSubscribersMap_
}
protected constructor(
container: MedusaContainer,
moduleOptions = {},
moduleDeclaration: InternalModuleDeclaration
) {
this.isWorkerMode = moduleDeclaration.worker_mode !== "server"
}
abstract emit<T>(
data: EventBusTypes.Message<T> | EventBusTypes.Message<T>[],
options: Record<string, unknown>
@@ -63,6 +77,10 @@ export abstract class AbstractEventBusModuleService
subscriber: EventBusTypes.Subscriber,
context?: EventBusTypes.SubscriberContext
): this {
if (!this.isWorkerMode) {
return this
}
if (typeof subscriber !== `function`) {
throw new Error("Subscriber must be a function")
}
@@ -88,6 +106,10 @@ export abstract class AbstractEventBusModuleService
subscriber: EventBusTypes.Subscriber,
context: EventBusTypes.SubscriberContext
): this {
if (!this.isWorkerMode) {
return this
}
if (typeof subscriber !== `function`) {
throw new Error("Subscriber must be a function")
}