MCPcopy Create free account
hub / github.com/srcbookdev/srcbook / constructor

Method constructor

packages/web/src/clients/websocket/channel.ts:37–75  ·  view source on GitHub ↗
(client: WebSocketClient, topic: string, events: { incoming: I; outgoing: O })

Source from the content-addressed store, hash-verified

35 private readonly receive: <K extends keyof I & string>(event: K, payload: z.TypeOf<I[K]>) => void;
36
37 constructor(client: WebSocketClient, topic: string, events: { incoming: I; outgoing: O }) {
38 this.topic = topic;
39 this.queue = [];
40 this.client = client;
41 this.events = events;
42 this.callbacks = {};
43 this.subscribed = false;
44 this.subscriptionId = null;
45
46 this.receive = <K extends keyof I & string>(event: K, payload: z.TypeOf<I[K]>) => {
47 if (event === 'subscribed') {
48 this.receiveSubscribedEvent(payload);
49 return;
50 }
51
52 // Ignore events until we are subscribed.
53 if (!this.subscribed) {
54 return;
55 }
56
57 const schema = this.events.incoming[event];
58
59 if (schema === undefined) {
60 throw new Error(`Channel received unknown event '${event}' for topic '${this.topic}'`);
61 }
62
63 const result = schema.safeParse(payload);
64
65 if (!result.success) {
66 throw new Error(
67 `Channel received invalid payload for '${event}' and topic '${this.topic}':\n\n${JSON.stringify(payload)}\n\n`,
68 );
69 }
70
71 for (const callback of this.callbacks[event] || []) {
72 callback(result.data);
73 }
74 };
75 }
76
77 subscribe() {
78 if (this.subscribed) {

Callers

nothing calls this directly

Calls 2

callbackFunction · 0.50

Tested by

no test coverage detected