# All files / utils/services/pub-sub pub-sub.service.ts

50% Statements 9/18
18.18% Branches 2/11
0% Functions 0/3
43.75% Lines 7/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    463x
    3x
    3x

    3x
    3x

    3x

    3x

     import { PubSub } from 'graphql-subscriptions';
    import { AmqpPubSub } from 'graphql-rabbitmq-subscriptions';
    import { ConsoleLogger, IConsoleLoggerSettings } from '@cdm-logger/server/lib';
    import * as Logger from 'bunyan';
    import { Service } from '../../container/index';
    import { ConfigService } from '../config/config.service';

    export let pubsub: PubSub | AmqpPubSub;

    const logger: Logger = ConsoleLogger.create('<app name>', <IConsoleLoggerSettings>{
        level: 'info', // Optional: default 'info' ('trace'|'info'|'debug'|'warn'|'error'|'fatal')
        mode: 'raw' // Optional: default 'short' ('short'|'long'|'dev'|'raw')
    });

    @Service()
    export class GapiPubSubService {
        sub: AmqpPubSub | PubSub;
        constructor(
            private pubSub: AmqpPubSub | PubSub | any,
            private configService: ConfigService
        ) {
            if (pubSub) {
                this.sub = pubSub;
            } else if (process.env.NODE_ENV === 'production') {
                this.sub = new AmqpPubSub({
                    config: {
                        host: process.env.AMQP_HOST || this.configService.AMQP_CONFIG.host,
                        port: process.env.AMQP_PORT || this.configService.AMQP_CONFIG.port,
                    },
                    logger,
                });
            } else {
                this.sub = new PubSub();
            }
        }
        asyncIterator<T>(event): AsyncIterator<T> {
            return this.sub.asyncIterator<T>(event);
        }
        publish(signal: string, data: any): boolean {
            return this.sub.publish(signal, data);
        }
    }