All files / rxmq.js/src rxmq.js

85.71% Statements 12/14
60% Branches 6/10
100% Functions 2/2
85.71% Lines 12/14
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                              1x           1x           1x                   12x 12x 8x     12x                       1x 1x         1x                           1x 1x                        
import Channel from './channel';
 
/**
 * Rxmq message bus class
 */
class Rxmq {
  /**
   * Represents a new Rxmq message bus.
   * Normally you'd just use a signleton returned by default, but it's also
   * possible to create a new instance of Rxmq should you need it.
   * @constructor
   * @example
   * import {Rxmq} from 'rxmq';
   * const myRxmq = new Rxmq();
   */
  constructor() {
    /**
     * Holds channels definitions
     * @type {Object}
     * @private
     */
    this.channels = {};
    /**
     * Holds channel plugins definitions
     * @type {Object}
     * @private
     */
    this.channelPlugins = [];
  }
 
  /**
   * Returns a channel for given name
   * @param  {String} name  Channel name
   * @return {Channel}      Channel object
   * @example
   * const testChannel = rxmq.channel('test');
   */
  channel(name = 'defaultRxmqChannel') {
    if (!this.channels[name]) {
      this.channels[name] = new Channel(this.channelPlugins);
    }
 
    return this.channels[name];
  }
 
  /**
   * Register new Rxmq plugin
   * @param  {Object} plugin      Plugin object
   * @return {void}
   * @example
   * import myPlugin from 'my-plugin';
   * rxmq.registerPlugin(myPlugin);
   */
  registerPlugin(plugin) {
    for (const prop in plugin) {
      Eif (!this.hasOwnProperty(prop)) {
        /**
         * Hide from esdoc
         * @private
         */
        this[prop] = plugin[prop];
      }
    }
  }
 
  /**
   * Register new Channel plugin
   * @param  {Object} plugin      Channel plugin object
   * @return {void}
   * @example
   * import myChannelPlugin from 'my-channel-plugin';
   * rxmq.registerChannelPlugin(myChannelPlugin);
   */
  registerChannelPlugin(plugin) {
    this.channelPlugins.push(plugin);
    for (const name in this.channels) {
      if (this.channels.hasOwnProperty(name)) {
        this.channels[name].registerPlugin(plugin);
      }
    }
  }
}
 
/**
 * Rxmq bus definition
 */
export default Rxmq;