All files / ima/event DispatcherImpl.js

100% Statements 48/48
85.19% Branches 23/27
100% Functions 10/10
100% Lines 48/48
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204        3x                 3x                 3x             4x             15x                 15x             9x   9x             18x 18x 3x           15x 9x   15x   15x 15x   15x   15x             6x   6x 6x 2x                           6x 6x 6x 6x   6x 3x       6x             7x   7x 1x                 7x 2x 2x       7x                 9x 9x                   15x 15x                             21x   21x 19x     2x                         49x 41x     8x       3x  
import ns from '../namespace';
import Dispatcher from './Dispatcher';
import GenericError from '../error/GenericError';
 
ns.namespace('ima.event');
 
/**
 * An empty immutable map of event listener to scopes, used for a mismatch in
 * the {@codelink _eventListeners} map.
 *
 * @const
 * @type {Map<function (*), Set<?Object>>}
 */
const EMPTY_MAP = Object.freeze(new Map());
 
/**
 * An empty immutable set of event listener scopes, used for a mismatch in the
 * {@codelink _eventListeners} map.
 *
 * @const
 * @type {Set<?Object>}
 */
const EMPTY_SET = Object.freeze(new Set());
 
/**
 * Default implementation of the {@codelink Dispatcher} interface.
 */
export default class DispatcherImpl extends Dispatcher {
  static get $dependencies() {
    return [];
  }
 
  /**
	 * Initializes the dispatcher.
	 */
  constructor() {
    super();
 
    /**
		 * Map of event names to a map of event listeners to a set of scopes to
		 * which the event listener should be bound when being executed due to
		 * the event.
		 *
		 * @type {Map<string, Map<function(*), Set<?Object>>>}
		 */
    this._eventListeners = new Map();
  }
 
  /**
	 * @inheritdoc
	 */
  clear() {
    this._eventListeners.clear();
 
    return this;
  }
 
  /**
	 * @inheritdoc
	 */
  listen(event, listener, scope = null) {
    Eif ($Debug) {
      if (!(listener instanceof Function)) {
        throw new GenericError(
          `The listener must be a function, ${listener} provided.`
        );
      }
    }
 
    if (!this._eventListeners.has(event)) {
      this._createNewEvent(event);
    }
    let listeners = this._getListenersOf(event);
 
    Eif (!listeners.has(listener)) {
      this._createNewListener(event, listener);
    }
    this._getScopesOf(event, listener).add(scope);
 
    return this;
  }
 
  /**
	 * @inheritdoc
	 */
  unlisten(event, listener, scope = null) {
    let scopes = this._getScopesOf(event, listener);
 
    Eif ($Debug) {
      if (!scopes.has(scope)) {
        console.warn(
          'ima.event.DispatcherImpl.unlisten(): the provided ' +
            `listener '${listener}' is not registered for the ` +
            `specified event '${event}' and scope '${scope}'. Check ` +
            `your workflow.`,
          {
            event: event,
            listener: listener,
            scope: scope
          }
        );
      }
    }
 
    scopes.delete(scope);
    Eif (!scopes.size) {
      let listeners = this._getListenersOf(event);
      listeners.delete(listener);
 
      if (!listeners.size) {
        this._eventListeners.delete(event);
      }
    }
 
    return this;
  }
 
  /**
	 * @inheritdoc
	 */
  fire(event, data, imaInternalEvent = false) {
    let listeners = this._getListenersOf(event);
 
    if (!listeners.size && !imaInternalEvent) {
      console.warn(
        `There are no event listeners registered for the ${event} ` + `event`,
        {
          event: event,
          data: data
        }
      );
    }
 
    for (let [listener, scopes] of listeners) {
      for (let scope of scopes) {
        listener.bind(scope)(data);
      }
    }
 
    return this;
  }
 
  /**
	 * Create new Map storage of listeners for the specified event.
	 *
	 * @param {string} event The name of the event.
	 */
  _createNewEvent(event) {
    let listeners = new Map();
    this._eventListeners.set(event, listeners);
  }
 
  /**
	 * Create new Set storage of scopes for the specified event and listener.
	 *
	 * @param {string} event The name of the event.
	 * @param {function(*)} listener The event listener.
	 */
  _createNewListener(event, listener) {
    let scopes = new Set();
    this._eventListeners.get(event).set(listener, scopes);
  }
 
  /**
	 * Retrieves the scopes in which the specified event listener should be
	 * executed for the specified event.
	 *
	 * @param {string} event The name of the event.
	 * @param {function(*)} listener The event listener.
	 * @return {Set<?Object>} The scopes in which the specified listeners
	 *         should be executed in case of the specified event. The returned
	 *         set is an unmodifiable empty set if no listeners are registered
	 *         for the event.
	 */
  _getScopesOf(event, listener) {
    let listenersToScopes = this._getListenersOf(event);
 
    if (listenersToScopes.has(listener)) {
      return listenersToScopes.get(listener);
    }
 
    return EMPTY_SET;
  }
 
  /**
	 * Retrieves the map of event listeners to scopes they are bound to.
	 *
	 * @param {string} event The name of the event.
	 * @return {Map<function(*), Set<?Object>>} A map of event listeners to the
	 *         scopes in which they should be executed. The returned map is an
	 *         unmodifiable empty map if no listeners are registered for the
	 *         event.
	 */
  _getListenersOf(event) {
    if (this._eventListeners.has(event)) {
      return this._eventListeners.get(event);
    }
 
    return EMPTY_MAP;
  }
}
 
ns.ima.event.DispatcherImpl = DispatcherImpl;