Files
medusa-store/packages/modules/caching/integration-tests/__fixtures__/event-bus-mock.ts
Adrien de Peretti b9d6f73320 Feat(): distributed caching (#13435)
RESOLVES CORE-1153

**What**
- This pr mainly lay the foundation the caching layer. It comes with a modules (built in memory cache) and a redis provider.
- Apply caching to few touch point to test

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
2025-09-30 16:19:06 +00:00

52 lines
1.3 KiB
TypeScript

import {
EventBusTypes,
IEventBusModuleService,
Message,
Subscriber,
} from "@medusajs/types"
export class EventBusServiceMock implements IEventBusModuleService {
protected readonly subscribers_: Map<string | symbol, Set<Subscriber>> =
new Map()
async emit<T>(
messages: Message<T> | Message<T>[],
options?: Record<string, unknown>
): Promise<void> {
const messages_ = Array.isArray(messages) ? messages : [messages]
for (const message of messages_) {
const subscribers = this.subscribers_.get(message.name)
const starSubscribers = this.subscribers_.get("*")
for (const subscriber of [
...(subscribers ?? []),
...(starSubscribers ?? []),
]) {
const { options, ...payload } = message
await subscriber(payload)
}
}
}
subscribe(event: string | symbol, subscriber: Subscriber): this {
this.subscribers_.set(event, new Set([subscriber]))
return this
}
unsubscribe(
event: string | symbol,
subscriber: Subscriber,
context?: EventBusTypes.SubscriberContext
): this {
return this
}
releaseGroupedEvents(eventGroupId: string): Promise<void> {
throw new Error("Method not implemented.")
}
clearGroupedEvents(eventGroupId: string): Promise<void> {
throw new Error("Method not implemented.")
}
}