All files / core MessageBusManager.ts

95% Statements 57/60
93.75% Branches 30/32
100% Functions 8/8
94.92% Lines 56/59
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  2x     2x 2x     2x   28x 28x 28x   28x 28x     28x   28x 2x 2x         2x   2x     26x 1x             26x 4x   26x     2x         15x                 2x   5x 1x   4x 1x     3x 3x                 2x   3x 1x   2x 1x     1x 1x                 2x   5x 1x   4x 1x   3x 2x     1x 1x                     2x   8x 1x   7x 1x   6x 2x     4x 4x 4x   2x

import { isNullOrUndefined, isObject, isNullOrEmpty, ArgumentException, ArgumentNullException } from '../common/common';
import { IMessageBus } from '../types/IMessageBus';
import { IMessageBusReceiver } from '../types/IMessageBusReceiver';
import { MessageBus } from './MessageBus';
import { MessageBusPromise } from './MessageBusPromise';
import { Promise } from 'es6-promise';
 
export class MessageBusManager {
 
    private _messageBus: IMessageBus = null;
    private _doLog: boolean = false;
    private _logger: any = console;
    
    constructor(messageBus?: IMessageBus, target?: any) {
        this.register(messageBus, target);
    }
 
    private register = (messageBus?: IMessageBus, target?: any) => {
 
        if (isNullOrUndefined(messageBus) && isNullOrUndefined(target)) {
            const w = window || global;
            Iif (isNullOrUndefined(w.MessageBus)) {
                const bus = new MessageBus();
                w.MessageBus = bus;
                this._messageBus = bus;
            } else {
                this._messageBus = w.MessageBus;
            }
            return;
        }
 
        if (isNullOrUndefined(messageBus)) {
             messageBus = new MessageBus();
        }
 
        /* istanbul ignore next */
        if (isNullOrUndefined(target)) {
            target = window || global;
        }
        if (isNullOrUndefined(target.MessageBus)) {
            target.MessageBus = messageBus;
        }
        this._messageBus = messageBus;
    }
 
    public getMessageBus(): IMessageBus {
        /* istanbul ignore next */
        if (isNullOrUndefined(this._messageBus)) {
            throw new ArgumentException('messageBus', 'No message bus found');
        }
        return this._messageBus;
    }
 
    /**
     * Subscribe to the message bus object
     * 
     * @param {string} topic The topic on which the receiver should be informed
     * @param {IMessageBusReceiver} receiver The receiver which should receive the published data (mostly: <this>)
     */
    public subscribe(topic: string, receiver: IMessageBusReceiver): void {
 
        if (isNullOrEmpty(topic)) {
            throw new ArgumentNullException('topic');
        }
        if (isNullOrUndefined(receiver)) {
            throw new ArgumentNullException('receiver');
        }
 
        const mb = this.getMessageBus();
        mb.subscribe(topic, receiver);
    }
 
    /**
     * Unsubscribe to the message bus object
     * 
     * @param {string} topic The topic on which the receiver should be informed
     * @param {IMessageBusReceiver} receiver The receiver which should receive the published data (mostly: <this>)
     */
    public unsubscribe(topic: string, receiver: IMessageBusReceiver): void {
 
        if (isNullOrEmpty(topic)) {
            throw new ArgumentNullException('topic');
        }
        if (isNullOrUndefined(receiver)) {
            throw new ArgumentNullException('receiver');
        }
 
        const mb = this.getMessageBus();
        mb.unsubscribe(topic, receiver);
    }
 
    /**
     * Publish something to the message bus object
     * 
     * @param {string} topic The topic on which the receiver should be informed
     * @param {*} data The data which should be published to all subscribers
     */
    public publishAll(topic: string, data: any): void {
 
        if (isNullOrEmpty(topic)) {
            throw new ArgumentNullException('topic');
        }
        if (isNullOrUndefined(data)) {
            throw new ArgumentNullException('data');
        }
        if (!isObject(data)) {
            throw new ArgumentException('data', 'Argument must be an object');
        }
 
        const mb = this.getMessageBus();
        mb.publish(topic, data);
    }
 
    /**
     * Publish something to the message bus object as promise.
     * First receiver can handle and return data
     * 
     * @param {string} topic The topic on which the receiver should be informed
     * @param {*} data The data which should be published to all subscribers
     * @param {number} timeout (optional) Timeout to wait for a response
     */
    public publish<TData, TResult>(topic: string, data: TData, timeout?: number, useSync?: boolean): Promise<TResult> {
 
        if (isNullOrEmpty(topic)) {
            throw new ArgumentNullException('topic');
        }
        if (isNullOrUndefined(data)) {
            throw new ArgumentNullException('data');
        }
        if (!isObject(data)) {
            throw new ArgumentException('data', 'Argument must be an object');
        }
 
        const mb = this.getMessageBus();
        const promise = new MessageBusPromise<TData, TResult>(mb, topic, data, useSync, timeout);
        return promise.send();
    }
}