UNPKG

modular-core

Version:

JavaScript application modular support

22 lines 2.63 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 Logger from"js-logger";var logger=Logger.get("modular.core.ExtensionPoint");export var Type=Object.freeze({Single:"SINGLE",Mixin:"MIXIN",Multiple:"MULTIPLE"});var DefaultExtensionPoint=function(){function e(e,t){this.extensions=[],this.extension=null,this.preprocessors=[],this.processed=!1,this.type=e.type,this.module=e.module,this.modular=t}return e.prototype.addExtension=function(e){for(var t,s=[],o=1;o<arguments.length;o++)s[o-1]=arguments[o];this.processed=!1,s.forEach((function(t){return t["@module"]=e})),(t=this.extensions).push.apply(t,s)},e.prototype.getExtension=function(){return this.preprocess(),this.extension},e.prototype.getExtensions=function(){return this.extensions},e.prototype.addPreprocessors=function(){for(var e,t=[],s=0;s<arguments.length;s++)t[s]=arguments[s];this.processed=!1,(e=this.preprocessors).push.apply(e,t)},e.prototype.preprocess=function(){if(!this.processed){var e=null,t=this.extensions;if(t&&t.length&&t.length>0)switch(this.type){case Type.Single:e=t[t.length-1],t=this.processExtensions([e]),e=t[0];break;case Type.Mixin:t=this.processExtensions(t),e={},t.forEach((function(t){return Object.assign(e,t)})),delete e["@module"];break;case Type.Multiple:default:e=t,e=this.processExtensions(e)}this.extension=e,this.processed=!0}},e.prototype.processExtensions=function(e){var t=this;return this.preprocessors.forEach((function(s){var o=null;try{o=s.process(e,t,t.modular)}catch(e){logger.error(e)}null!==o&&(e=o)})),e},e}();export{DefaultExtensionPoint};