servicelocatorjs
Version:
Dependency injection approach using Service Locator pattern
2 lines • 3.26 kB
JavaScript
!function(t,n){"function"==typeof define&&define.amd?define(function(){return t.ServiceLocator=n()}):"object"==typeof module&&module.exports?module.exports=t.ServiceLocator=n():t.ServiceLocator=n()}(this,function(){"use strict";function t(){function t(t){var n,e,i=Array.prototype.slice.call(arguments,1);t.__mixins=[];for(e=0;e<i.length;++e)for(n in i[e])if(void 0===t[n]){t[n]=i[e][n];t.__mixins.push(n)}}function n(t,n,e){function i(t){var e,i;if(!t)return this;this.__mixins=[];for(e=0;e<t.length;++e)for(i in t[e]){this[i]=n[e][i];this.__mixins.push(i)}}var r;i.prototype=t.prototype;t.prototype=new i(n);r=new t(e);t.prototype=i.prototype;return r}function e(t,n){var i;if(t&&!(n.recursion>1e3)){n.recursion++;if(t.hasOwnProperty("__mixins")){for(i=0;i<n.length;i+=1)delete t[n[i]];delete t.__mixins}else e(Object.getPrototypeOf(t),n)}}function i(t){t.__mixins.recursion=0;e(t,t.__mixins);return t}function r(t){g&&console.log("Instantiate: "+t);f[t].instance=n(f[t].creator,[{id:t},l],f[t].args||[]);return f[t].instance}function s(t){return Object.prototype.toString.call(t).slice(8,-1).toLowerCase()}function o(t){return"object"===s(t)}function u(t){return"string"==typeof t}function c(t){return"function"==typeof t}function a(t){return d.isRegistered(t)?"creator"in f[t]:!1}var f={},l={},g=!1,d={printLog:function(t){g=!!t;return this},setMixin:function(t){o(t)&&(l=t);return this},getMixin:function(){return l},mixin:function(t){this.setMixin(t);return this.getMixin()},register:function(e,i,r,s){if(!u(e)||!e.length){g&&console.warn("serviceName must be type of string: ["+e+"]");return!1}if(!c(i)&&!o(i)){g&&console.warn("serviceObject argument is empty or have wrong type: ["+i+"]");return!1}if(this.isRegistered(e)){g&&console.warn("You try to register already registered module: ["+e+"]");return!1}r=arguments.length<3?!0:!!r;switch(typeof i){case"function":f[e]={creator:i};arguments.length>3&&(f[e].args=s);if(r){var a;a="args"in f[e]?n(i,{},f[e].args):n(i,{});t(a,{id:e},l);f[e].instance=a}break;case"object":t(i,{id:e},l);f[e]={instance:i};break;default:return!1}return!0},isRegistered:function(t){return t in f},isInstantiated:function(t){return this.isRegistered(t)&&"instance"in f[t]?!!f[t].instance:!1},get:function(t){if(!this.isRegistered(t)){g&&console.warn("Service is not registered: "+t);return null}if(this.isInstantiated(t)){g&&console.warn("Already instantiated: "+t);return f[t].instance}return a(t)?r(t):null},instantiate:function(t){return this.isRegistered(t)?this.isInstantiated(t)?!0:a(t)?!!r(t):!1:!1},instantiateAll:function(t){var n,e=[];"function"!=typeof t&&(t=function(){return!0});for(n in f)!this.isInstantiated(n)&&a(n)&&t(n)&&e.push(r(n));return e},getAllInstantiate:function(){var t,n=[];for(t in f)this.isInstantiated(t)&&n.push(t);return n},removeInstance:function(t){if(!f[t]||!f[t].instance)return!1;delete f[t].instance;return!0},unRegister:function(t,n){if(!this.isRegistered(t))return!1;if(!this.isInstantiated(t)){delete f[t];return null}var e=null;e=n?i(f[t].instance):f[t].instance;delete f[t];return e},unRegisterAll:function(t){var n,e,i={};for(n in f){e=this.unRegister(n,t);e&&(i[n]=e)}return i}};return d}var n;n=new t;n.Constructor=t;return n});
//# sourceMappingURL=servicelocator.min.js.map