|
1
|
import { |
|
2
|
EventBusTypes, |
|
3
|
IEventBusModuleService, |
|
4
|
Message, |
|
5
|
Subscriber, |
|
6
|
} from "@medusajs/types" |
|
7
|
|
|
8
|
export class EventBusServiceMock implements IEventBusModuleService { |
|
9
|
protected readonly subscribers_: Map<string | symbol, Set<Subscriber>> = |
|
10
|
new Map() |
|
11
|
|
|
12
|
async emit<T>( |
|
13
|
messages: Message<T> | Message<T>[], |
|
14
|
options?: Record<string, unknown> |
|
15
|
): Promise<void> { |
|
16
|
const messages_ = Array.isArray(messages) ? messages : [messages] |
|
17
|
|
|
18
|
for (const message of messages_) { |
|
19
|
const subscribers = this.subscribers_.get(message.name) |
|
20
|
const starSubscribers = this.subscribers_.get("*") |
|
21
|
|
|
22
|
for (const subscriber of [ |
|
23
|
...(subscribers ?? []), |
|
24
|
...(starSubscribers ?? []), |
|
25
|
]) { |
|
26
|
const { options, ...payload } = message |
|
27
|
await subscriber(payload) |
|
28
|
} |
|
29
|
} |
|
30
|
} |
|
31
|
|
|
32
|
subscribe(event: string | symbol, subscriber: Subscriber): this { |
|
33
|
this.subscribers_.set(event, new Set([subscriber])) |
|
34
|
return this |
|
35
|
} |
|
36
|
|
|
37
|
unsubscribe( |
|
38
|
event: string | symbol, |
|
39
|
subscriber: Subscriber, |
|
40
|
context?: EventBusTypes.SubscriberContext |
|
41
|
): this { |
|
42
|
return this |
|
43
|
} |
|
44
|
|
|
45
|
releaseGroupedEvents(eventGroupId: string): Promise<void> { |
|
46
|
throw new Error("Method not implemented.") |
|
47
|
} |
|
48
|
clearGroupedEvents(eventGroupId: string): Promise<void> { |
|
49
|
throw new Error("Method not implemented.") |
|
50
|
} |
|
51
|
} |