# All files / microservices listeners-controller.ts

73.33% Statements 11/15
100% Branches 0/0
75% Functions 3/4
73.33% Lines 11/15

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x
    2x

    2x

     import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
    import { ListenerMetadataExplorer } from './listener-metadata-explorer';
    import { Server } from './server/server';
    import { ClientProxyFactory } from './client/client-proxy-factory';
    import { MetadataScanner } from '@nestjs/core/metadata-scanner';
    import { CustomTransportStrategy } from './interfaces';
    import { ClientsContainer } from './container';
    import { RpcContextCreator } from './context/rpc-context-creator';

    export class ListenersController {
      private readonly metadataExplorer = new ListenerMetadataExplorer(
        new MetadataScanner(),
      );

      constructor(
        private readonly clientsContainer: ClientsContainer,
        private readonly contextCreator: RpcContextCreator,
      ) {}

      public bindPatternHandlers(
        instance: Controller,
        server: Server & CustomTransportStrategy,
        module: string,
      ) {
        const patternHandlers = this.metadataExplorer.explore(instance);
        patternHandlers.forEach(({ pattern, targetCallback }) => {
          const proxy = this.contextCreator.create(
            instance,
            targetCallback,
            module,
          );
          server.addHandler(pattern, proxy);
        });
      }

      public bindClientsToProperties(instance: Controller) {
        for (const {
          property,
          metadata,
        } of this.metadataExplorer.scanForClientHooks(instance)) {
          const client = ClientProxyFactory.create(metadata);

          this.clientsContainer.addClient(client);
          Reflect.set(instance, property, client);
        }
      }
    }