MCPcopy
hub / github.com/dyrector-io/dyrectorio / WsNamespace

Class WsNamespace

web/crux/src/websockets/namespace.ts:18–204  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16} from './common'
17
18export default class WsNamespace implements WsSubscription {
19 private readonly logger: Logger
20
21 private readonly clients: Map<string, ClientResources> = new Map()
22
23 private readonly path: string
24
25 readonly params: Record<string, string>
26
27 constructor(match: WsRouteMatch) {
28 this.path = match.path
29 this.params = match.params
30
31 this.logger = new Logger(`${WsNamespace.name} ${this.path}`)
32 }
33
34 close() {
35 this.clients.forEach(it => it.client.subscriptions.delete(this.path))
36 this.clients.clear()
37
38 this.logger.verbose('Closed')
39 }
40
41 getCompleter(clientToken: string): Observable<undefined> {
42 const resources = this.clients.get(clientToken)
43 return resources.completer as Observable<undefined>
44 }
45
46 getParameter(name: string): string | null {
47 return this.params[name] ?? null
48 }
49
50 onSubscribe(
51 client: WsClient,
52 callbacks: WsClientCallbacks,
53 message: WsMessage<SubscriptionMessage>,
54 ): Observable<WsMessage> {
55 if (this.clients.has(client.token)) {
56 return EMPTY
57 }
58
59 const { subscribe, handlers, transform, unsubscribe } = callbacks
60
61 const resources: ClientResources = {
62 client,
63 handlers,
64 transform,
65 unsubscribe,
66 completer: new Subject(),
67 }
68 this.clients.set(client.token, resources)
69
70 client.subscriptions.set(this.path, this)
71
72 this.logger.verbose(`${client.token} subscribed`)
73
74 const res: WsMessage<SubscriptionMessage> = {
75 type: WS_TYPE_SUBBED,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected