UNPKG

@soundstep/soma

Version:

Soma is a javascript framework created to build scalable and maintainable applications.

3 lines (2 loc) 6.68 kB
/* soma - v3.0.4 - 7/31/2021 - https://github.com/soundstep/soma */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@soundstep/infuse"),require("signals")):"function"==typeof define&&define.amd?define(["exports","@soundstep/infuse","signals"],e):e((t=t||self).soma={},t.infuse,t.signals)}(this,(function(t,e,i){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e,i=i&&i.hasOwnProperty("default")?i.default:i;const o={};o.is={object:t=>"object"==typeof t&&null!==t,array:Array.isArray||(t=>"[object Array]"===Object.prototype.toString.call(t)),func:t=>"function"==typeof t},o.applyProperties=(t,e,i,o)=>{if("[object Array]"===Object.prototype.toString.apply(o))for(const n=0,s=o.length;n<s;n++)void 0!==t[o[n]]&&null!==t[o[n]]||(i&&"function"==typeof e[o[n]]?t[o[n]]=e[o[n]].bind(e):t[o[n]]=e[o[n]]);else for(const o in e)i&&"function"==typeof e[o]?t[o]=e[o].bind(e):t[o]=e[o]},o.augment=(t,e,i)=>{if(e.prototype&&t.prototype)if("[object Array]"===Object.prototype.toString.apply(i))for(const o=0,n=i.length;o<n;o++)t.prototype[i[o]]||(t.prototype[i[o]]=e.prototype[i[o]]);else for(const i in e.prototype)t.prototype[i]||(t.prototype[i]=e.prototype[i])},o.inherit=(t,e)=>{let i;i=e&&e.hasOwnProperty("constructor")?e.constructor:function(){return t.apply(this,arguments)};const n=function(){};return n.prototype=t.prototype,i.prototype=new n,e&&o.applyProperties(i.prototype,e),i.prototype.constructor=i,i.parent=t.prototype,i.extend=function(t){return o.inherit(i,t)},i},o.extend=t=>o.inherit((function(){}),t);const n=function(){this.signals={}};function s(t,e,i,o,n){const s=Array.prototype.slice.call(arguments,5),r=t.createChild();r.mapValue("id",e),r.mapValue("signal",o),r.mapValue("binding",n);const c=r.createInstance(i);"function"==typeof c.execute&&c.execute.apply(c,s),r.dispose()}n.prototype.addListener=function(t,e,o,n){return this.signals[t]||(this.signals[t]=new i),this.signals[t].add(e,o,n)},n.prototype.addListenerOnce=function(t,e,o,n){return this.signals[t]||(this.signals[t]=new i),this.signals[t].addOnce(e,o,n)},n.prototype.removeListener=function(t,e,i){const o=this.signals[t];o&&o.remove(e,i)},n.prototype.getSignal=function(t){return this.signals[t]},n.prototype.dispatch=function(t,e){const i=this.signals[t];i&&(e?i.dispatch.apply(i,e):i.dispatch())},n.prototype.dispose=function(){for(const t in this.signals)this.signals[t].removeAll(),this.signals[t]=void 0,delete this.signals[t];this.signals={}},n.extend=function(t){return o.inherit(n,t)};const r=function(t,e){this.list={},this.emitter=t,this.injector=e};r.prototype.add=function(t,e){if(this.list[t])throw new Error('[Commands] Error: a command with the id: "'+t+'" has already been registered');return this.list[t]=e,function t(e){return{setInjector:function(i){return e&&i&&(e.params[0]=i),t(e)}}}(function(t,e,i){const o=t.emitter.addListener(e,s,t);return o.params=[t.injector,e,i,t.emitter.getSignal(e),o],o}(this,t,e))},r.prototype.get=function(t){return this.list[t]},r.prototype.remove=function(t){this.list[t]&&(this.list[t]=void 0,delete this.list[t],function(t,e){const i=t.emitter.getSignal(e);i&&i.removeAll()}(this,t))},r.prototype.dispose=function(){for(const t in this.list)this.remove(t);this.list={},this.emitter=void 0,this.injector=void 0},r.extend=function(t){return o.inherit(r,t)};const c=function(t,e){this.emitter=t,this.injector=e};c.prototype.create=function(t,e){if(null==t)throw new Error("Error creating a mediator, the first parameter cannot be undefined or null.");if(!e||"function"!=typeof e)throw new Error("[Mediators] Error creating a mediator, the second parameter must be a function.");let i=[];const o=[];Array.isArray(t)&&t.length>0?i=[].concat(t):i.push(t);for(let t=0,n=i.length;t<n;t++){const n=this.injector.createChild();n.mapValue("target",i[t]);const s=n.createInstance(e);if(1===i.length)return s;o.push(s)}return o},c.prototype.dispose=function(){this.emitter=void 0,this.injector=void 0};const p=function(t){this.injector=t,this.list={}};p.prototype.create=function(t,i,n,s){let r,c;const p=!1!==n,a=!0===s;if(o.is.func(t))c=t;else if(o.is.object(t)&&o.is.func(t.module))c=t.module;else{if(!o.is.object(t)||!o.is.func(t.Module))throw new Error("[Modules] Error: Could not create module. The module must be a function or an object containing a module property referencing a function.");c=t.Module}if(!function(t){let e=!0;return null==t?e=!1:"string"!=typeof t.id&&(e=!1),e}(c))throw new Error('[Modules] Error: Could not create module. The module function must contain a static "id" property, ex: function Module(){}; Module.id = "module-name"; ');if(c)if(this.has(c.id))r=this.get(c.id);else{let t=this.injector;a&&(t=this.injector.createChild()).mapValue("injector",t),r=function(t,i,o){const n=e.getDependencies(i);let s=[i];for(let e=0,i=n.length;e<i;e++)t.hasMapping(n[e])||t.hasInheritedMapping(n[e])?s.push(t.getValue(n[e])):s.push(void 0);for(let t=s.length-1;t>=0&&void 0===s[t];t--)s.splice(t,1);return s=s.concat(o),t.createInstance.apply(t,s)}(t,c,i),u=this.list,l=c.id,h=r,!u[l]&&p&&(u[l]=h),"function"==typeof r.init&&r.init()}var u,l,h;return r},p.prototype.has=function(t){return void 0!==this.list[t]},p.prototype.get=function(t){return this.list[t]},p.prototype.remove=function(t){this.list[t]&&("function"==typeof this.list[t].dispose&&this.list[t].dispose(),this.list[t]=void 0,delete this.list[t])},p.prototype.dispose=function(){for(const t in this.list)this.remove(t);this.list={}};var a=function(){this.injector=void 0,this.emitter=void 0,this.commands=void 0,this.mediators=void 0,this.setup(),this.init()};a.prototype.setup=function(){this.injector=new e.Injector,this.injector.throwOnMissing=!1,this.injector.mapValue("injector",this.injector),this.injector.mapValue("instance",this),this.injector.mapClass("emitter",n,!0),this.emitter=this.injector.getValue("emitter"),this.injector.mapClass("commands",r,!0),this.commands=this.injector.getValue("commands"),this.injector.mapClass("mediators",c,!0),this.mediators=this.injector.getValue("mediators"),this.injector.mapClass("modules",p,!0),this.modules=this.injector.getValue("modules")},a.prototype.init=function(){},a.prototype.dispose=function(){this.injector&&this.injector.dispose(),this.emitter&&this.emitter.dispose(),this.commands&&this.commands.dispose(),this.mediators&&this.mediators.dispose(),this.modules&&this.modules.dispose(),this.injector=void 0,this.emitter=void 0,this.commands=void 0,this.mediators=void 0,this.modules=void 0},a.extend=function(t){return o.inherit(a,t)},t.Application=a,t.Commands=a,t.Emitter=a,t.Mediators=a,t.Modules=a,t.utils=o,Object.defineProperty(t,"__esModule",{value:!0})}));