UNPKG

modular-core

Version:

JavaScript application modular support

22 lines 4.58 kB
/*! * Copyright (c) 2021 han_feng@foxmail.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE * OR OTHER DEALINGS IN THE SOFTWARE. */ import{__spreadArray}from"tslib";import Logger from"js-logger";import LogInfo from"./LogInfo";import ModulesLoader from"./ModulesLoader";import{DefaultExtensionPoint,Type}from"./ExtensionPoint";var logger=Logger.get("modular.core.Modular"),Modular=function(){function t(t){this.inited=!1,this.logs=[],this.extensionPoints={},this.attributes={},t=t||{modules:[]},this.strict=!!t.strict,this.modules=t.modules||[],this.application=t.application||{name:"Application"},this.init()}return t.prototype.getApplication=function(){return this.application},t.prototype.getModule=function(t){return this.modules.find((function(e){return e.name===t}))},t.prototype.getModules=function(){return this.modules},t.prototype.getExtension=function(t){var e=this.getExtensionPoint(t);return null!==e?e.getExtension():null},t.prototype.getExtensions=function(t){var e=this.getExtensionPoint(t);return null!==e?e.getExtensions():null},t.prototype.getExtensionPoint=function(t){var e=this.extensionPoints[t];return void 0===e?null:e},t.prototype.getExtensionPoints=function(){return this.extensionPoints},t.prototype.getAttributeNames=function(){return Object.keys(this.attributes)},t.prototype.setAttribute=function(t,e){this.attributes[t]=e},t.prototype.getAttribute=function(t){return this.attributes[t]},t.prototype.start=function(){var t=this;logger.debug("Modular starting ..."),this.modules.forEach((function(e){e.activator&&e.activator.start&&e.activator.start(t,e)})),logger.debug("Modular started")},t.prototype.getLogs=function(){return Object.freeze(this.logs)},t.prototype.log=function(t){this.logs.push(t),logger.error(t)},t.prototype.createNameMapping=function(t){var e=this,o={};return t.forEach((function(t){if(void 0!==t.name&&""!==t.name){var n=t.name;o[n]?e.log(new LogInfo("E02","error",{m1:o[n],m2:t})):o[n]=t}else e.log(new LogInfo("E01","error",{m:t}))})),o},t.prototype.init=function(){var t,e=this;if(this.inited)this.log(new LogInfo("E00","error"));else{logger.debug("Modular init");var o=this.application,n=this.modules,r=this.createNameMapping(n),i=new ModulesLoader;n.forEach((function(t){e.loadDepens(t,i,r)})),this.loadDepens(o,i,r);for(var s={},a=(n=i.getModules()).length,u=0;u<a;u++){var p=n[u];if(p.extensionPoints){var l=p.extensionPoints;for(var f in l)if(s[f])this.log(new LogInfo("E05","error",{m:p,ep:f}));else{var g=new DefaultExtensionPoint(l[f],this);g.module=p.name,s[f]=g}}if(p.preprocessors){var d=p.preprocessors;for(var c in d)s[c]?s[c].addPreprocessors(d[c]):this.log(new LogInfo("E06","error",{m:p,ep:c}))}if(p.extensions){var h=p.extensions;for(var m in h)s[m]?s[m].type===Type.Multiple&&Array.isArray(h[m])?(t=s[m]).addExtension.apply(t,__spreadArray([p.name],h[m],!1)):s[m].addExtension(p.name,h[m]):this.log(new LogInfo("E06","error",{m:p,ep:m}))}n[u]=Object.freeze(p)}this.application=Object.freeze(o),this.modules=Object.freeze(n),this.extensionPoints=Object.freeze(s),this.inited=!0}},t.prototype.loadDepens=function(t,e,o,n){if(void 0===n&&(n={}),void 0===t.name||""===t.name)return!1;if(e.contains(t))return!0;if(t.dependencies&&t.dependencies.length){t.dependencies=Object.freeze(t.dependencies);for(var r=t.dependencies,i=r.length,s=0;s<i;s++){var a=r[s];if(o[a]){if(n[a])return!0;if(n[a]=o[a],this.loadDepens(o[a],e,o,n)){delete n[a];continue}return this.log(new LogInfo("E03","error",{m1:t,m2:a})),!1}return this.log(new LogInfo("E04","error",{m1:t,m2:a})),!1}}return e.add(t),!0},t}();export default Modular;