-
Notifications
You must be signed in to change notification settings - Fork 627
Expand file tree
/
Copy pathSfuMessageDispatcher.ts
More file actions
26 lines (21 loc) · 1011 Bytes
/
SfuMessageDispatcher.ts
File metadata and controls
26 lines (21 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import {ClientMessage,ClientMessageType} from "@repo/common/sfu";
type PayloadType<T extends ClientMessageType> = Extract<ClientMessage, { type: T }>['payload'];
class SfuMessageDispatcher {
private handlers: Map<ClientMessageType, ((payload: any) => void)[]> = new Map();
registerHandler<T extends ClientMessageType>(type: T, handler: (payload: PayloadType<T>) => Promise<void>): () => void {
const existing = this.handlers.get(type) || [];
existing.push(handler);
this.handlers.set(type, existing);
// Return a function to deregister the handler
return () => this.handlers.set(type, existing.filter((h) => h !== handler));
}
dispatch(event: MessageEvent) {
const message = JSON.parse(event.data) as ClientMessage;
console.log('Received message:', message);
const handler = this.handlers.get(message.type);
if (handler) {
handler.forEach((h) => h(message.payload));
}
}
}
export default SfuMessageDispatcher;