| 10 | } |
| 11 | |
| 12 | export class InMemoryBus implements IBus { |
| 13 | private subscribers: Map<string, ((event: z.infer<typeof RunEvent>) => Promise<void>)[]> = new Map(); |
| 14 | |
| 15 | async publish(event: z.infer<typeof RunEvent>): Promise<void> { |
| 16 | const pending: Promise<void>[] = []; |
| 17 | for (const subscriber of this.subscribers.get(event.runId) || []) { |
| 18 | pending.push(subscriber(event)); |
| 19 | } |
| 20 | for (const subscriber of this.subscribers.get('*') || []) { |
| 21 | pending.push(subscriber(event)); |
| 22 | } |
| 23 | await Promise.all(pending); |
| 24 | } |
| 25 | |
| 26 | async subscribe(runId: string, handler: (event: z.infer<typeof RunEvent>) => Promise<void>): Promise<() => void> { |
| 27 | if (!this.subscribers.has(runId)) { |
| 28 | this.subscribers.set(runId, []); |
| 29 | } |
| 30 | this.subscribers.get(runId)!.push(handler); |
| 31 | return () => { |
| 32 | this.subscribers.get(runId)!.splice(this.subscribers.get(runId)!.indexOf(handler), 1); |
| 33 | }; |
| 34 | } |
| 35 | } |
nothing calls this directly
no outgoing calls
no test coverage detected