Advertisement
lukicdarkoo

Deprecated MEP's module loader

Oct 20th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @namespace utils */
  2.  
  3. const TAG = 'ModuleLoader';
  4.  
  5. /**
  6.  * Make instance of class based on JSON
  7.  *
  8.  * @author Darko Lukic <lukicdarkoo@gmail.com>
  9.  * @memberof utils
  10.  */
  11. class ModuleLoader {
  12.     /**
  13.      * @private
  14.      */
  15.     constructor() {
  16.     }
  17.  
  18.     /**
  19.      * <p>Initialize modules</p>
  20.      *
  21.      * <p>
  22.      * `modulesConfig` must have structure like
  23.      * <pre>
  24.      * <code>
  25.      * {
  26.      *  ModuleID: {
  27.      *      class: 'path/to/class',
  28.      *      init: true|false
  29.      *  },
  30.      *  NextModuleID: {
  31.      *      class: 'path/to/next/class',
  32.      *      init: true|false
  33.      *  }
  34.      * }
  35.      * </code>
  36.      * </pre>
  37.      * </p>
  38.      *
  39.      * @param modulesConfig {Object} - Array of module configs
  40.      * @param simulation {boolean} - Use simulation suffix
  41.      * @returns {Object} - Returns associative array of instantiated modules
  42.      */
  43.     static load(modulesConfig) {
  44.         var modules = {};
  45.  
  46.         for (let moduleName in modulesConfig) {
  47.             if (modulesConfig.hasOwnProperty(moduleName) == false) {
  48.                 continue;
  49.             }
  50.  
  51.             let moduleConfig = modulesConfig[moduleName];
  52.             let init = moduleConfig.init;
  53.             let classPath = moduleConfig.class;
  54.  
  55.             // Do not initialize if `init field == false`
  56.             if (init != false) {
  57.                 let ModuleClass = Mep.require(classPath);
  58.  
  59.                 if (typeof ModuleClass === 'function') {
  60.                     modules[moduleName] = new ModuleClass(moduleName, moduleConfig);
  61.                     Mep.Log.debug(TAG, 'Module loaded', moduleName);
  62.                 } else {
  63.                     Mep.Log.error(TAG, 'There is no module on path', modulePath);
  64.                 }
  65.             }
  66.         }
  67.         return modules;
  68.     }
  69. }
  70.  
  71. module.exports = ModuleLoader;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement