UNPKG

@allgemein/moduls

Version:

Commons-moduls handles and manages contextual moduls for complex and modular applications.

75 lines 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractModuleLoader = void 0; const lodash_1 = require("lodash"); class AbstractModuleLoader { constructor(registry, options) { this._handles = []; this.registry = registry; this._options = options || {}; } handles() { return this._handles; } add(handle) { if (handle) { let exists = (0, lodash_1.find)(this._handles, (x) => { return x.module.name === handle.module.name; }); if (!exists) { this._handles.push(handle); // correct order if necessary this._handles = (0, lodash_1.orderBy)(this._handles, [ x => x.module.weight, x => x.module.child_modules.length, x => x.module.id ], ['asc', 'asc', 'asc']); } else { throw new Error('handle for module ' + handle.module.name + ' already loaded'); } } return handle; } load(modules) { if ((0, lodash_1.isArray)(modules)) { const promises = []; for (let x of modules) { if (this._options.filter && !this._options.filter(x)) { continue; } promises.push(this._loadOne(x)); } return Promise.all(promises); } else { return this._loadOne(modules); } } async _loadOne(modules) { let res = null; let y = await this.loadOne(modules); try { res = this.add(y); } catch (err) { if (this.registry.options().handleErrorOnDuplicate) { switch (this.registry.options().handleErrorOnDuplicate) { case 'skip': break; case 'log': console.error(err); break; default: throw err; } } else { throw err; } } return res; } } exports.AbstractModuleLoader = AbstractModuleLoader; //# sourceMappingURL=AbstractModuleLoader.js.map