Generator-emitted scaffold (pnpm turbo gen core-package realtime) plus the story-00-precedent coverage repairs (coverage provider devDep, symbols.ts exclude + tested allowlist mirror) and three minimal tests covering generator-emitted realtime code the template suite misses. Squash of 31d85e0 + review-fix cf11b38. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016j8z4VHjedXDTjEDNg7qHK
29 lines
1011 B
TypeScript
29 lines
1011 B
TypeScript
import type { z } from "zod";
|
|
import type { IRealtimeBroadcaster } from "./realtime-broadcaster.interface";
|
|
import type { RealtimeChannelDescriptor } from "./realtime-channel";
|
|
|
|
type Listener<T> = (payload: T) => Promise<void> | void;
|
|
|
|
export class InMemoryRealtimeBroadcaster implements IRealtimeBroadcaster {
|
|
private readonly listeners = new Map<string, Listener<unknown>[]>();
|
|
|
|
async broadcast<T>(
|
|
descriptor: RealtimeChannelDescriptor<string, z.ZodType<T>>,
|
|
payload: T,
|
|
): Promise<void> {
|
|
descriptor.schema.parse(payload);
|
|
const arr = this.listeners.get(descriptor.name) ?? [];
|
|
for (const l of arr) await l(payload);
|
|
}
|
|
|
|
// Test-friendly: lets unit tests subscribe directly without a Socket.IO server.
|
|
subscribe<T>(
|
|
descriptor: RealtimeChannelDescriptor<string, z.ZodType<T>>,
|
|
listener: Listener<T>,
|
|
): void {
|
|
const arr = this.listeners.get(descriptor.name) ?? [];
|
|
arr.push(listener as Listener<unknown>);
|
|
this.listeners.set(descriptor.name, arr);
|
|
}
|
|
}
|