import type { z } from "zod"; import type { RealtimeRegistryProtocol } from "@repo/core-shared/di/bind-protocols"; import type { RealtimeChannelDescriptor } from "./realtime-channel"; import type { IInboundDescriptor } from "./realtime-handler.interface"; export interface IRealtimeHandlerRegistry extends RealtimeRegistryProtocol { register(entry: IInboundDescriptor>): void; getInboundDescriptor( channelName: string, ): IInboundDescriptor | null; list(): IInboundDescriptor[]; /** Register an outbound-only channel so Gate 2 can authorize subscriptions to it. */ registerChannel( descriptor: RealtimeChannelDescriptor, ): void; listChannels(): RealtimeChannelDescriptor[]; } export class RealtimeHandlerRegistry implements IRealtimeHandlerRegistry { private readonly entries = new Map< string, IInboundDescriptor >(); private readonly channels = new Map< string, RealtimeChannelDescriptor >(); register(entry: IInboundDescriptor>): void { this.entries.set( entry.descriptor.name, entry as IInboundDescriptor, ); // Also add the descriptor to the channel map so Gate 2 can authorize subscriptions. this.channels.set(entry.descriptor.name, entry.descriptor); } getInboundDescriptor( channelName: string, ): IInboundDescriptor | null { return this.entries.get(channelName) ?? null; } list(): IInboundDescriptor[] { return Array.from(this.entries.values()); } registerChannel( descriptor: RealtimeChannelDescriptor, ): void { this.channels.set(descriptor.name, descriptor); } listChannels(): RealtimeChannelDescriptor[] { return Array.from(this.channels.values()); } }