feat(core-realtime): RealtimeHandlerRegistry
This commit is contained in:
44
packages/core-realtime/src/realtime-handler-registry.test.ts
Normal file
44
packages/core-realtime/src/realtime-handler-registry.test.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { describe, it, expect, vi } from "vitest";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { RealtimeHandlerRegistry } from "@/realtime-handler-registry";
|
||||||
|
import { defineRealtimeChannel } from "@/realtime-channel";
|
||||||
|
|
||||||
|
const ch = defineRealtimeChannel(
|
||||||
|
"test.ch",
|
||||||
|
z.object({ x: z.number() }).strict(),
|
||||||
|
{ scope: "authenticated" },
|
||||||
|
);
|
||||||
|
|
||||||
|
describe("RealtimeHandlerRegistry", () => {
|
||||||
|
it("registers and retrieves a handler by channel name", () => {
|
||||||
|
const reg = new RealtimeHandlerRegistry();
|
||||||
|
const handler = vi.fn();
|
||||||
|
reg.register({ descriptor: ch, handler });
|
||||||
|
const got = reg.getInboundDescriptor("test.ch");
|
||||||
|
expect(got).not.toBeNull();
|
||||||
|
expect(got!.descriptor.name).toBe("test.ch");
|
||||||
|
expect(got!.handler).toBe(handler);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null for unknown channel name", () => {
|
||||||
|
const reg = new RealtimeHandlerRegistry();
|
||||||
|
expect(reg.getInboundDescriptor("unknown")).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("list() returns all registered descriptors", () => {
|
||||||
|
const reg = new RealtimeHandlerRegistry();
|
||||||
|
reg.register({ descriptor: ch, handler: vi.fn() });
|
||||||
|
expect(reg.list()).toHaveLength(1);
|
||||||
|
expect(reg.list()[0]!.descriptor.name).toBe("test.ch");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("re-registering the same channel replaces the previous entry", () => {
|
||||||
|
const reg = new RealtimeHandlerRegistry();
|
||||||
|
const h1 = vi.fn();
|
||||||
|
const h2 = vi.fn();
|
||||||
|
reg.register({ descriptor: ch, handler: h1 });
|
||||||
|
reg.register({ descriptor: ch, handler: h2 });
|
||||||
|
expect(reg.getInboundDescriptor("test.ch")!.handler).toBe(h2);
|
||||||
|
expect(reg.list()).toHaveLength(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
24
packages/core-realtime/src/realtime-handler-registry.ts
Normal file
24
packages/core-realtime/src/realtime-handler-registry.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import type { z } from "zod";
|
||||||
|
import type { IInboundDescriptor } from "./realtime-handler.interface";
|
||||||
|
|
||||||
|
export interface IRealtimeHandlerRegistry {
|
||||||
|
register<T>(entry: IInboundDescriptor<string, z.ZodType<T>>): void;
|
||||||
|
getInboundDescriptor(channelName: string): IInboundDescriptor<string, z.ZodType> | null;
|
||||||
|
list(): IInboundDescriptor<string, z.ZodType>[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RealtimeHandlerRegistry implements IRealtimeHandlerRegistry {
|
||||||
|
private readonly entries = new Map<string, IInboundDescriptor<string, z.ZodType>>();
|
||||||
|
|
||||||
|
register<T>(entry: IInboundDescriptor<string, z.ZodType<T>>): void {
|
||||||
|
this.entries.set(entry.descriptor.name, entry as IInboundDescriptor<string, z.ZodType>);
|
||||||
|
}
|
||||||
|
|
||||||
|
getInboundDescriptor(channelName: string): IInboundDescriptor<string, z.ZodType> | null {
|
||||||
|
return this.entries.get(channelName) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
list(): IInboundDescriptor<string, z.ZodType>[] {
|
||||||
|
return Array.from(this.entries.values());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user