'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Dashboard *| (C) Copyright IBM Corp. 2019 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['../../lib/@waca/core-client/js/core-client/utils/ClassFactory'], function (ClassFactory) { var BoardModuleFactory = function () { function BoardModuleFactory(props) { _classCallCheck(this, BoardModuleFactory); this.modules = props.modules || []; this._cache = props.cache || {}; this._loader = props.loader || ClassFactory; this.logger = props.logger; } BoardModuleFactory.prototype.make = function make(type) { var _this = this; var module = this.getModuleByType(type); if (!module) { throw new Error('Module with type "' + type + '" not found'); } var promise; if (this.isCached(module)) { promise = Promise.resolve(this.getFromCache(module)); } else { promise = this.loadModule(module).then(function (ModuleClass) { _this.setToCache(module, ModuleClass); return ModuleClass; }); } return promise.then(function (ModuleClass) { return new ModuleClass({ logger: _this.logger }); }); }; BoardModuleFactory.prototype.loadModule = function loadModule(module) { var _this2 = this; return this._loader.loadModule(module.path).catch(function (error) { _this2.logger.warn('[BoardModuleFactory]: attempted to load module "' + module.path + ' and failed"', error); throw error; }); }; BoardModuleFactory.prototype.getModuleByType = function getModuleByType(type) { var found; // TODO: update to find when everything is babelified for (var key in this.modules) { if (this.modules.hasOwnProperty(key) && this.modules[key].type === type) { found = this.modules[key]; } } return found; }; BoardModuleFactory.prototype.getDefaultModuleType = function getDefaultModuleType() { return BoardModuleFactory.defaultModuleType; }; /* cache methods */ BoardModuleFactory.prototype.isCached = function isCached(module) { return !!this._cache[module.type]; }; BoardModuleFactory.prototype.getFromCache = function getFromCache(module) { return this._cache[module.type]; }; BoardModuleFactory.prototype.setToCache = function setToCache(module, value) { this._cache[module.type] = value; return this; }; return BoardModuleFactory; }(); BoardModuleFactory.defaultModuleType = 'legacy'; return BoardModuleFactory; }); //# sourceMappingURL=BoardModuleFactory.js.map