UNPKG

reactant-di

Version:

A dependency injection lib for Reactant

2 lines (1 loc) 8.24 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("reflect-metadata"),require("inversify")):"function"==typeof define&&define.amd?define(["exports","reflect-metadata","inversify"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).reactantDi={},null,e.inversify)}(this,(function(e,t,r){"use strict";var n=function(e,t){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},n(e,t)};function o(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}var i=function(){return i=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},i.apply(this,arguments)};function a(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,o,i=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=i.next()).done;)a.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return a}"function"==typeof SuppressedError&&SuppressedError;var l,u=Symbol("defaultUndefined"),f=function(){function e(e){this.identifier=e}return Object.defineProperty(e.prototype,"key",{get:function(){return u},enumerable:!1,configurable:!0}),e}(),s=function(e){return Reflect.getMetadata(e,Reflect)||new Map},d=function(e,t,r){void 0===r&&(r=t);var n=s(e);n.set(r||t,t),Reflect.defineMetadata(e,n,Reflect)},p=function(e,t,r){var n,o;if(void 0===r)return t;try{for(var i=a(l),c=i.next();!c.done;c=i.next()){var u=c.value;if("object"==typeof u){var s=u.deps;if((u.provide===e||u.useClass===e)&&Array.isArray(s)&&void 0!==s[r])return s[r]instanceof f?s[r].key:s[r]}}}catch(e){n={error:e}}finally{try{c&&!c.done&&(o=i.return)&&o.call(i)}finally{if(n)throw n.error}}return t},y=function(e){var t,r;try{for(var n=a(l),o=n.next();!o.done;o=n.next()){var i=o.value,c=i.deps;if("object"==typeof i&&Array.isArray(c)&&(i.useClass||2===Object.keys(i).length))return!!c.filter((function(t){return t instanceof f&&t.identifier===e})).length}}catch(e){t={error:e}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}return!1};function v(e,t){t.has(e.serviceIdentifier)||t.set(e.serviceIdentifier,[]);var r=e.childRequests.map((function(e){return e.serviceIdentifier}));t.set(e.serviceIdentifier,r),0!==e.childRequests.length&&e.childRequests.forEach((function(e){v(e,t)}))}function h(e){return function(t){return function(r){var n=r.contextInterceptor;return t(i(i({},r),{contextInterceptor:function(t){return v(t.plan.rootRequest,e),n(t)}}))}}}var g={optional:"reactant:optional",provide:"reactant:provide",multiple:"reactant:multiple",lazy:"reactant:lazy",paramtypes:"design:paramtypes",inversifyParamtypes:"inversify:paramtypes",inversifyTagged:"inversify:tagged"},b=function(e){return new r.LazyServiceIdentifer(e)};function m(e){return function(t,n,o){var i,a=Reflect.getMetadata(g.paramtypes,t)[o];i=e instanceof r.LazyServiceIdentifer?e:b(void 0===e?function(){return p(t,a,o)}:function(){return p(t,e,o)}),r.decorate(r.inject(i),t,o)}}function w(e){return function(t,n,o){var i=Reflect.getMetadata(g.paramtypes,t);d(g.optional,i[o],e),r.decorate(m(e),t,o),r.decorate(r.optional(),t,o)}}function M(e){return function(t,n,o){var i=Reflect.getMetadata(g.paramtypes,t);d(g.multiple,null==i?void 0:i[o],e),r.decorate(r.multiInject(e),t,o)}}function R(e){return function(t,n,o){var i=Reflect.getMetadata(g.paramtypes,t);d(g.optional,null==i?void 0:i[o],e),d(g.multiple,null==i?void 0:i[o],e),r.decorate(M(e),t,o),r.decorate(r.optional(),t,o)}}function j(e){return void 0===e&&(e={}),function(t){var n=e.deps;return(void 0===n?[]:n).forEach((function(e,n){if("function"==typeof e)r.decorate(m(e),t,n);else{if("[object Object]"!==toString.call(e))throw new Error("@injectable ".concat(null==e?void 0:e.toString()," option error"));if(void 0===e.provide||null===e.provide)throw new Error("@injectable ".concat(e?JSON.stringify(e):e," option error, please set a valid value for 'provide'"));e.optional&&!e.multi?r.decorate(w(e.provide),t,n):e.multi&&!e.optional?r.decorate(M(e.provide),t,n):e.multi&&e.optional?r.decorate(R(e.provide),t,n):r.decorate(m(e.provide),t,n)}})),d(g.provide,t,t),r.decorate(r.injectable(),t),t}}var C=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t}(r.Container),S=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t.prototype.getConstructorMetadata=function(t){return e.prototype.getConstructorMetadata.call(this,t)},t}(r.MetadataReader);function O(e){Reflect.getMetadata(g.inversifyParamtypes,e).forEach((function(t,r){Reflect.getMetadata(g.inversifyTagged,e)&&Reflect.getMetadata(g.inversifyTagged,e)[r]||m()(e,void 0,r)}))}var x=function(e){function t(t,r){var n=e.call(this,t)||this;return n._serviceIdentifiers=r,n}return o(t,e),t.prototype.got=function(e){try{return this._serviceIdentifiers.has(e)?this.get(e):void 0}catch(e){return}},t.prototype.gotAll=function(e){try{return this._serviceIdentifiers.has(e)?this.getAll(e):void 0}catch(e){return}},t}(r.Container);function I(e,t){var n,o,i=s(g.provide),l=function(t){if("function"==typeof t)i.has(t)||r.decorate(j(),t),O(t),e.bind(t).toSelf();else{if("object"!=typeof t)throw new Error("".concat(t," option error"));if(function(e){return"function"==typeof e.useClass}(t))i.has(t.useClass)||r.decorate(j(),t.useClass),O(t.useClass),e.bind(t.provide).to(t.useClass);else if(Object.hasOwnProperty.call(t,"useValue"))e.bind(t.provide).toConstantValue(t.useValue);else if(function(e){return"function"==typeof e.useFactory}(t))e.bind(t.provide).toFactory((function(e){var r=(t.deps||[]).map((function(t){var r=t.provide||t;if(!t.optional||e.container.isBound(t.provide))return e.container.get(r)}));return t.useFactory.apply(t,function(e,t,r){if(r||2===arguments.length)for(var n,o=0,i=t.length;o<i;o++)!n&&o in t||(n||(n=Array.prototype.slice.call(t,0,o)),n[o]=t[o]);return e.concat(n||Array.prototype.slice.call(t))}([],c(r),!1))}));else{if("function"!=typeof t.provide)throw new Error("".concat(t," option error"));i.has(t.provide)||r.decorate(j(),t.provide),O(t.provide),e.bind(t.provide).toSelf()}}};try{for(var u=a(t),f=u.next();!f.done;f=u.next()){l(f.value)}}catch(e){n={error:e}}finally{try{f&&!f.done&&(o=u.return)&&o.call(u)}finally{if(n)throw n.error}}e.load(new r.ContainerModule((function(e,t,r){var n,o,i=s(g.provide),l=s(g.optional);try{for(var u=a(i),f=u.next();!f.done;f=u.next()){var d=c(f.value,2),p=d[0],v=d[1];l.has(p)&&!y(p)||r(p)||e(p).to(v)}}catch(e){n={error:e}}finally{try{f&&!f.done&&(o=u.return)&&o.call(u)}finally{if(n)throw n.error}}})))}e.METADATA_KEY=g,e.ModuleRef=C,e.Optional=f,e.bindModules=I,e.createContainer=function(e){var t=e.ServiceIdentifiers,r=e.modules,n=void 0===r?[]:r,o=e.options;l=n;var i=new x(o,t);return i.applyCustomMetadataReader(new S),I(i,n),i.applyMiddleware(h(t)),i.isBound(C)&&i.unbind(C),i.bind(C).toConstantValue(i),i.bind(u).toConstantValue(void 0),i},e.forwardRef=b,e.getLazyDecorator=function(e){return function(t,r){return void 0===r&&(r=!0),function(n,o){return{configurable:!0,enumerable:!0,get:function(){if(r&&!Reflect.hasMetadata(g.lazy,this,o)){var n=e(t,this);null!==n&&Reflect.defineMetadata(g.lazy,n,this,o)}return Reflect.hasMetadata(g.lazy,this,o)?Reflect.getMetadata(g.lazy,this,o):e(t,this)},set:function(e){r?Reflect.defineMetadata(g.lazy,e,this,o):console.warn("\n Disable cache and the property ".concat(o.toString(),' in class "').concat(this.constructor.name,'" instance failed to set value.\n '))}}}}},e.getMetadata=s,e.inject=m,e.injectable=j,e.multiInject=M,e.multiOptional=R,e.optional=w}));