| 12 | * @publicApi |
| 13 | */ |
| 14 | export class IoAdapter extends AbstractWsAdapter { |
| 15 | private readonly disconnectMap = new WeakMap<Socket, Observable<any>>(); |
| 16 | |
| 17 | public create( |
| 18 | port: number, |
| 19 | options?: ServerOptions & { namespace?: string; server?: any }, |
| 20 | ): Server { |
| 21 | if (!options) { |
| 22 | return this.createIOServer(port); |
| 23 | } |
| 24 | const { namespace, server, ...opt } = options; |
| 25 | return server && isFunction(server.of) |
| 26 | ? server.of(namespace) |
| 27 | : namespace |
| 28 | ? this.createIOServer(port, opt).of(namespace) |
| 29 | : this.createIOServer(port, opt); |
| 30 | } |
| 31 | |
| 32 | public createIOServer(port: number, options?: any): any { |
| 33 | if (this.httpServer && port === 0) { |
| 34 | return new Server(this.httpServer, options); |
| 35 | } |
| 36 | return new Server(port, options); |
| 37 | } |
| 38 | |
| 39 | public bindMessageHandlers( |
| 40 | socket: Socket, |
| 41 | handlers: MessageMappingProperties[], |
| 42 | transform: (data: any) => Observable<any>, |
| 43 | ) { |
| 44 | let disconnect$ = this.disconnectMap.get(socket); |
| 45 | if (!disconnect$) { |
| 46 | disconnect$ = fromEvent(socket, DISCONNECT_EVENT).pipe(share(), first()); |
| 47 | this.disconnectMap.set(socket, disconnect$); |
| 48 | } |
| 49 | |
| 50 | handlers.forEach(({ message, callback, isAckHandledManually }) => { |
| 51 | const source$ = fromEvent(socket, message).pipe( |
| 52 | mergeMap((payload: any) => { |
| 53 | const { data, ack } = this.mapPayload(payload); |
| 54 | return transform(callback(data, ack)).pipe( |
| 55 | filter((response: any) => !isNil(response)), |
| 56 | map((response: any) => [response, ack, isAckHandledManually]), |
| 57 | ); |
| 58 | }), |
| 59 | takeUntil(disconnect$), |
| 60 | ); |
| 61 | source$.subscribe(([response, ack, isAckHandledManually]) => { |
| 62 | if (response.event) { |
| 63 | return socket.emit(response.event, response.data); |
| 64 | } |
| 65 | if (!isAckHandledManually && isFunction(ack)) { |
| 66 | ack(response); |
| 67 | } |
| 68 | }); |
| 69 | }); |
| 70 | } |
| 71 |
nothing calls this directly
no outgoing calls
no test coverage detected