All files / core MessageBus.ts

100% Statements 52/52
93.33% Branches 28/30
100% Functions 7/7
100% Lines 50/50
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167  3x               3x   37x 37x 37x 37x 37x                                     37x 37x 37x 37x     3x   48x 48x   48x 1x   47x 2x     45x 41x   4x       3x   8x 8x   8x 1x   7x 2x     5x   5x 5x 5x 4x         1x 1x   1x     13x   13x 13x   13x 1x   12x 1x     11x 3x     8x 11x   11x 11x                                                                                                                     3x

import { isNullOrEmpty, ArgumentException, ArgumentNullException, isNullOrUndefined, isFunction, isArray } from '../common/common';
import { IMessageBus } from '../types/IMessageBus';
import { IMessageBusReceiver } from '../types/IMessageBusReceiver';
 
type TopicsMap = {
    [key: string]: Array<IMessageBusReceiver>
}
 
export class MessageBus implements IMessageBus {
 
    private _doLog: boolean = false;
    private _topics: TopicsMap = {};
    private _useSync: boolean = false;
    private _delay: number = 0;
    private _logger: any = console;
 
    constructor(useSync?: boolean, delay?: number, enableLogging?: boolean, logger?: any) {
        /* istanbul ignore next */
        if (isNullOrUndefined(useSync)) {
            useSync = false;
        }
        /* istanbul ignore next */
        if (isNullOrUndefined(delay)) {
            delay = 0;
        }
        /* istanbul ignore next */
        if (isNullOrUndefined(enableLogging)) {
            enableLogging = false;
        }
        /* istanbul ignore next */
        if (isNullOrUndefined(logger)) {
            logger = console;
        }
        this._useSync = useSync;
        this._delay = delay;
        this._doLog = enableLogging;
        this._logger = logger;
    }
 
    public subscribe(topic: string, receiver: IMessageBusReceiver) {
 
        this.warn('Subscribe to topic: ' + topic);
        this.log(receiver);
 
        if (isNullOrEmpty(topic)) {
            throw new ArgumentNullException('topic');
        }
        if (isNullOrUndefined(receiver) || !isFunction(receiver.receive)) {
            throw new ArgumentNullException('receiver');
        }
 
        if (!this._topics[topic]) {
            this._topics[topic] = [receiver];
        } else {
            this._topics[topic].push(receiver);
        }
    }
 
    public unsubscribe(topic: string, receiver: IMessageBusReceiver): IMessageBusReceiver {
 
        this.warn('Unsubscribe to topic: ' + topic);
        this.log(receiver);
 
        if (isNullOrEmpty(topic)) {
            throw new ArgumentNullException('topic');
        }
        if (isNullOrUndefined(receiver) || !isFunction(receiver.receive)) {
            throw new ArgumentNullException('receiver');
        }
 
        Eif (!isNullOrUndefined(this._topics[topic]
            && isArray(this._topics[topic]))) {
            const map = this._topics[topic];
            for (let i = 0; i < map.length; i++) {
                if (map[i] === receiver) {
                    return map.splice(i, 1)[0] as IMessageBusReceiver;
                }
            }
        }
 
        this.warn('Unsubscribe failed: ' + topic);
        this.log(receiver);
 
        return null;
    }
 
    public publish(topic: string, data: any) {
 
        this.warn('Publish to topic: ' + topic);
        this.log(data);
 
        if (isNullOrEmpty(topic)) {
            throw new ArgumentNullException('topic');
        }
        if (isNullOrUndefined(data)) {
            throw new ArgumentNullException('data');
        }
 
        if (isNullOrEmpty(this._topics[topic])) {
            return;
        }
 
        this._topics[topic].forEach((receiver) => {
            Eif (!isNullOrUndefined(receiver) && isFunction(receiver.receive)) {
                // ensure to run async
                this.runAsync(() => {
                    receiver.receive(data);
                });
            } else {
                /* istanbul ignore next */
                this.error('The receiver is undefined or does not implement IMessageBusReceiver');
            }
        });
    }
 
    /* istanbul ignore next */
    private log(message: string | any) {
        if (this._doLog !== true) {
            return;
        }
        if (isNullOrUndefined(this._logger)) {
            throw new ArgumentException('logger');
        }
        if (!isNullOrUndefined(this._logger.log)) {
            this._logger.log(message);
        }
    }
 
    /* istanbul ignore next */
    private warn(message: string) {
        if (this._doLog !== true) {
            return;
        }
        if (isNullOrUndefined(this._logger)) {
            throw new ArgumentException('logger');
        }
        if (!isNullOrUndefined(this._logger.warn)) {
            this._logger.warn(message);
        }
    }
 
    /* istanbul ignore next */
    private error(message: string) {
        if (this._doLog !== true) {
            return;
        }
        if (isNullOrUndefined(this._logger)) {
            throw new ArgumentException('logger');
        }
        if (!isNullOrUndefined(this._logger.error)) {
            this._logger.error(message);
        }
    }
 
    /* istanbul ignore next */
    private runAsync(func: () => void) {
        if (this._useSync === true) {
            this.warn('Running in sync. Do not do this in production!');
            func();
        } else {
            setTimeout(() => {
                func();
            }, this._delay);
        }
    }
}