all files / dist/satellites/ events.js

65.71% Statements 46/70
36.67% Branches 11/30
66.67% Functions 12/18
58.49% Lines 31/53
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 205 206 207 208 209 210 211 212 213 214 215 216 217                      18×                                                                                                                                                                                                                                     18×     18× 18×                                                                                                                      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _async = require('async');
 
var _async2 = _interopRequireDefault(_async);
 
var _utils = require('../utils');
 
var _utils2 = _interopRequireDefault(_utils);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/**
 * Class to manage events.
 *
 * The developers can use this to manipulate data during the
 * execution or to extend functionalities adding new behaviours
 * to existing logic. The listeners must be stored in
 * <moduleName>/listeners.
 */
 
var EventsManager = function () {
 
  /**
   * Create a new instance.
   *
   * @param api   API reference object.
   */
 
 
  /**
   * API reference object.
   *
   * @type {null}
   */
 
  function EventsManager(api) {
    _classCallCheck(this, EventsManager);
 
    this.api = null;
    this.events = new Map();
    this.api = api;
  }
 
  // -------------------------------------------------------------------------------------------------------- [Commands]
 
  /**
   * Fire an event.
   *
   * @param eventName   Event to fire.
   * @param params      Params to pass to the listeners.
   * @param callback    Callback function.
   */
 
 
  /**
   * Map with all registered events and listeners.
   *
   * @type {Map}
   */
 
 
  _createClass(EventsManager, [{
    key: 'fire',
    value: function fire(eventName, params) {
      var callback = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
 
      var self = this;
 
      // variable to store listener response data
      var responseData = params;
 
      // check if exists listeners for this event
      if (self.events.has(eventName)) {
        // execute the listeners async in series
        _async2.default.each(self.events.get(eventName), function (listener, callback) {
          return listener(self.api, responseData, callback);
        }, function () {
          // execute the callback function
          if (typeof callback === 'function') {
            return callback(responseData);
          }
        });
 
        return;
      }
 
      // execute the callback function
      if (typeof callback === 'function') {
        return callback(responseData);
      }
    }
 
    /**
     * Register a new listener for an event.
     *
     * @param eventName       Event
     * @param eventFunction
     */
 
  }, {
    key: 'listener',
    value: function listener(eventName, eventFunction) {
      var self = this;
 
      // if there is no listener for this event, create a new entry
      // with an empty array
      if (!self.events.has(eventName)) {
        self.events.set(eventName, []);
      }
 
      // get the array with all registered listeners for this event
      var listeners = self.events.get(eventName);
 
      // register the new listener
      listeners.push(eventFunction);
    }
 
    // --------------------------------------------------------------------------------------------------- [Other Methods]
 
    /**
     * Iterate over all active modules and
     *
     * @param next
     */
 
  }, {
    key: 'loadListeners',
    value: function loadListeners(next) {
      var self = this;
 
      // iterate all active modules
      self.api.modules.modulesPaths.forEach(function (modulePath) {
        // build path for the module listeners folder
        var listenersFolderPath = modulePath + '/listeners';
 
        // check if the listeners
        Eif (!_utils2.default.directoryExists(listenersFolderPath)) {
          return;
        }
 
        // get all listeners files
        _utils2.default.recursiveDirectoryGlob(listenersFolderPath, 'js').forEach(function (listenerPath) {
          // require listener file
          var listener = require(listenerPath);
 
          // if is a collection iterate it
          if (listener instanceof Array) {
            listener.forEach(function (l) {
              return self.listener(l.event, l.run);
            });
          } else {
            self.listener(listener.event, listener.run);
          }
        });
      });
 
      // end listeners loading
      next();
    }
  }]);
 
  return EventsManager;
}();
 
/**
 * Satellite to load the event manager.
 */
 
 
var _class = function () {
  function _class() {
    _classCallCheck(this, _class);
 
    this.loadPriority = 20;
  }
 
  /**
   * Satellite load priority.
   *
   * @type {number}
   */
 
 
  _createClass(_class, [{
    key: 'load',
 
 
    /**
     * Satellite loading function.
     *
     * @param api   API reference object.
     * @param next  Callback function.
     */
    value: function load(api, next) {
      // make events api available in all platform
      api.events = new EventsManager(api);
 
      // load listeners
      api.events.loadListeners(next);
    }
  }]);
 
  return _class;
}();
 
exports.default = _class;
//# sourceMappingURL=events.js.map