UNPKG

@inversifyjs/core

Version:

InversifyJs core package

3 lines (2 loc) 30.3 kB
import{getOwnReflectMetadata as e,setReflectMetadata as t,updateOwnReflectMetadata as n}from"@inversifyjs/reflect-metadata-utils";import{getBaseType as i}from"@inversifyjs/prototype-utils";import{stringifyServiceIdentifier as o,LazyServiceIdentifier as r,isPromise as a}from"@inversifyjs/common";const s="@inversifyjs/container/bindingId";function c(){const i=e(Object,s)??0;return i===Number.MAX_SAFE_INTEGER?t(Object,s,Number.MIN_SAFE_INTEGER):n(Object,s,(()=>i),(e=>e+1)),i}const u={Request:"Request",Singleton:"Singleton",Transient:"Transient"},d={ConstantValue:"ConstantValue",DynamicValue:"DynamicValue",Factory:"Factory",Instance:"Instance",Provider:"Provider",ResolvedValue:"ResolvedValue",ServiceRedirection:"ServiceRedirection"};function*l(...e){for(const t of e)yield*t}class p{#e;#t;#n;constructor(e){this.#e=new Map,this.#t={};for(const t of Reflect.ownKeys(e))this.#t[t]=new Map;this.#n=e}add(e,t){this.#i(e).push(t);for(const n of Reflect.ownKeys(t))this.#o(n,t[n]).push(e)}clone(){const e=this.#r(),t=this.#a(),n=Reflect.ownKeys(this.#n),i=this._buildNewInstance(this.#n);this.#s(this.#e,i.#e,e,t);for(const t of n)this.#c(this.#t[t],i.#t[t],e);return i}get(e,t){return this.#t[e].get(t)}getAllKeys(e){return this.#t[e].keys()}removeByRelation(e,t){const n=this.get(e,t);if(void 0===n)return;const i=new Set(n);for(const n of i){const i=this.#e.get(n);if(void 0===i)throw new Error("Expecting model relation, none found");for(const o of i)o[e]===t&&this.#u(n,o);this.#e.delete(n)}}_buildNewInstance(e){return new p(e)}_cloneModel(e){return e}_cloneRelation(e){return e}#r(){const e=new Map;for(const t of this.#e.keys()){const n=this._cloneModel(t);e.set(t,n)}return e}#a(){const e=new Map;for(const t of this.#e.values())for(const n of t){const t=this._cloneRelation(n);e.set(n,t)}return e}#i(e){let t=this.#e.get(e);return void 0===t&&(t=[],this.#e.set(e,t)),t}#o(e,t){let n=this.#t[e].get(t);return void 0===n&&(n=[],this.#t[e].set(t,n)),n}#d(e,t){const n=t.get(e);if(void 0===n)throw new Error("Expecting model to be cloned, none found");return n}#l(e,t){const n=t.get(e);if(void 0===n)throw new Error("Expecting relation to be cloned, none found");return n}#c(e,t,n){for(const[i,o]of e){const e=new Array;for(const t of o)e.push(this.#d(t,n));t.set(i,e)}}#s(e,t,n,i){for(const[o,r]of e){const e=new Array;for(const t of r)e.push(this.#l(t,i));t.set(this.#d(o,n),e)}}#u(e,t){for(const n of Reflect.ownKeys(t))this.#p(e,n,t[n])}#p(e,t,n){const i=this.#t[t].get(n);if(void 0!==i){const o=i.indexOf(e);-1!==o&&i.splice(o,1),0===i.length&&this.#t[t].delete(n)}}}var f,g,h;!function(e){e.moduleId="moduleId",e.serviceId="serviceId"}(f||(f={}));class m{#f;#g;constructor(e,t){this.#f=t??new p({moduleId:{isOptional:!0},serviceId:{isOptional:!1}}),this.#g=e}static build(e){return new m(e)}add(e,t){this.#f.add(e,t)}clone(){return new m(this.#g,this.#f.clone())}get(e){const t=[],n=this.#f.get(f.serviceId,e);void 0!==n&&t.push(n);const i=this.#g?.get(e);if(void 0!==i&&t.push(i),0!==t.length)return l(...t)}removeAllByModuleId(e){this.#f.removeByRelation(f.moduleId,e)}removeAllByServiceId(e){this.#f.removeByRelation(f.serviceId,e)}}function v(e){return e.isRight?{isRight:!0,value:e.value}:e}function y(e){switch(e.type){case d.ConstantValue:case d.DynamicValue:return function(e){return{cache:v(e.cache),id:e.id,isSatisfiedBy:e.isSatisfiedBy,moduleId:e.moduleId,onActivation:e.onActivation,onDeactivation:e.onDeactivation,scope:e.scope,serviceIdentifier:e.serviceIdentifier,type:e.type,value:e.value}}(e);case d.Factory:return function(e){return{cache:v(e.cache),factory:e.factory,id:e.id,isSatisfiedBy:e.isSatisfiedBy,moduleId:e.moduleId,onActivation:e.onActivation,onDeactivation:e.onDeactivation,scope:e.scope,serviceIdentifier:e.serviceIdentifier,type:e.type}}(e);case d.Instance:return function(e){return{cache:v(e.cache),id:e.id,implementationType:e.implementationType,isSatisfiedBy:e.isSatisfiedBy,moduleId:e.moduleId,onActivation:e.onActivation,onDeactivation:e.onDeactivation,scope:e.scope,serviceIdentifier:e.serviceIdentifier,type:e.type}}(e);case d.Provider:return function(e){return{cache:v(e.cache),id:e.id,isSatisfiedBy:e.isSatisfiedBy,moduleId:e.moduleId,onActivation:e.onActivation,onDeactivation:e.onDeactivation,provider:e.provider,scope:e.scope,serviceIdentifier:e.serviceIdentifier,type:e.type}}(e);case d.ResolvedValue:return function(e){return{cache:v(e.cache),factory:e.factory,id:e.id,isSatisfiedBy:e.isSatisfiedBy,metadata:e.metadata,moduleId:e.moduleId,onActivation:e.onActivation,onDeactivation:e.onDeactivation,scope:e.scope,serviceIdentifier:e.serviceIdentifier,type:e.type}}(e);case d.ServiceRedirection:return function(e){return{id:e.id,isSatisfiedBy:e.isSatisfiedBy,moduleId:e.moduleId,serviceIdentifier:e.serviceIdentifier,targetServiceIdentifier:e.targetServiceIdentifier,type:e.type}}(e)}}!function(e){e.id="id",e.moduleId="moduleId",e.serviceId="serviceId"}(g||(g={}));class M extends p{_buildNewInstance(e){return new M(e)}_cloneModel(e){return y(e)}}class I{#h;#g;constructor(e,t){this.#h=t??new M({id:{isOptional:!1},moduleId:{isOptional:!0},serviceId:{isOptional:!1}}),this.#g=e}static build(e){return new I(e)}clone(){return new I(this.#g,this.#h.clone())}get(e){return this.getNonParentBindings(e)??this.#g?.get(e)}getBoundServices(){const e=new Set(this.#h.getAllKeys(g.serviceId));if(void 0!==this.#g)for(const t of this.#g.getBoundServices())e.add(t);return e}getById(e){return this.#h.get(g.id,e)??this.#g?.getById(e)}getByModuleId(e){return this.#h.get(g.moduleId,e)??this.#g?.getByModuleId(e)}getNonParentBindings(e){return this.#h.get(g.serviceId,e)}getNonParentBoundServices(){return this.#h.getAllKeys(g.serviceId)}removeById(e){this.#h.removeByRelation(g.id,e)}removeAllByModuleId(e){this.#h.removeByRelation(g.moduleId,e)}removeAllByServiceId(e){this.#h.removeByRelation(g.serviceId,e)}set(e){const t={[g.id]:e.id,[g.serviceId]:e.serviceIdentifier};void 0!==e.moduleId&&(t[g.moduleId]=e.moduleId),this.#h.add(e,t)}}!function(e){e.moduleId="moduleId",e.serviceId="serviceId"}(h||(h={}));class w{#m;#g;constructor(e,t){this.#m=t??new p({moduleId:{isOptional:!0},serviceId:{isOptional:!1}}),this.#g=e}static build(e){return new w(e)}add(e,t){this.#m.add(e,t)}clone(){return new w(this.#g,this.#m.clone())}get(e){const t=[],n=this.#m.get(h.serviceId,e);void 0!==n&&t.push(n);const i=this.#g?.get(e);if(void 0!==i&&t.push(i),0!==t.length)return l(...t)}removeAllByModuleId(e){this.#m.removeByRelation(h.moduleId,e)}removeAllByServiceId(e){this.#m.removeByRelation(h.serviceId,e)}}function b(e,t,n){const i=Array.isArray(e)?e:[e];if(void 0!==n)if("number"!=typeof n)Reflect.decorate(i,t.prototype,n);else for(const e of i)e(t,void 0,n);else Reflect.decorate(i,t)}const S="@inversifyjs/core/classMetadataReflectKey";function j(){return{constructorArguments:[],lifecycle:{postConstructMethodName:void 0,preDestroyMethodName:void 0},properties:new Map,scope:void 0}}const R="@inversifyjs/core/pendingClassMetadataCountReflectKey";const T=Symbol.for("@inversifyjs/core/InversifyCoreError");class A extends Error{[T];kind;constructor(e,t,n){super(t,n),this[T]=!0,this.kind=e}static is(e){return"object"==typeof e&&null!==e&&!0===e[T]}static isErrorOfKind(e,t){return A.is(e)&&e.kind===t}}var C,B,$;function x(t){const n=e(t,S)??j();if(!function(t){const n=e(t,R);return void 0!==n&&0!==n}(t))return function(e,t){const n=[];if(t.length<e.length)throw new A(C.missingInjectionDecorator,`Found unexpected missing metadata on type "${e.name}". "${e.name}" constructor requires at least ${e.length.toString()} arguments, found ${t.length.toString()} instead.\nAre you using @inject, @multiInject or @unmanaged decorators in every non optional constructor argument?\n\nIf you're using typescript and want to rely on auto injection, set "emitDecoratorMetadata" compiler option to true`);for(let e=0;e<t.length;++e)void 0===t[e]&&n.push(e);if(n.length>0)throw new A(C.missingInjectionDecorator,`Found unexpected missing metadata on type "${e.name}" at constructor indexes "${n.join('", "')}".\n\nAre you using @inject, @multiInject or @unmanaged decorators at those indexes?\n\nIf you're using typescript and want to rely on auto injection, set "emitDecoratorMetadata" compiler option to true`)}(t,n.constructorArguments),n;!function(e,t){const n=[];for(let i=0;i<t.constructorArguments.length;++i){const o=t.constructorArguments[i];void 0!==o&&o.kind!==B.unknown||n.push(` - Missing or incomplete metadata for type "${e.name}" at constructor argument with index ${i.toString()}.\nEvery constructor parameter must be decorated either with @inject, @multiInject or @unmanaged decorator.`)}for(const[i,o]of t.properties)o.kind===B.unknown&&n.push(` - Missing or incomplete metadata for type "${e.name}" at property "${i.toString()}".\nThis property must be decorated either with @inject or @multiInject decorator.`);if(0===n.length)throw new A(C.unknown,`Unexpected class metadata for type "${e.name}" with uncompletion traces.\nThis might be caused by one of the following reasons:\n\n1. A third party library is targeting inversify reflection metadata.\n2. A bug is causing the issue. Consider submiting an issue to fix it.`);throw new A(C.missingInjectionDecorator,`Invalid class metadata at type ${e.name}:\n\n${n.join("\n\n")}`)}(t,n)}function D(){return 0}function k(e){return t=>{void 0!==t&&t.kind===B.unknown&&n(e,R,D,(e=>e-1))}}function P(e,t){return(...n)=>i=>{if(void 0===i)return e(...n);if(i.kind===$.unmanaged)throw new A(C.injectionDecoratorConflict,"Unexpected injection found. Multiple @inject, @multiInject or @unmanaged decorators found");return t(i,...n)}}function O(e){if(e.kind!==B.unknown&&!0!==e.isFromTypescriptParamType)throw new A(C.injectionDecoratorConflict,"Unexpected injection found. Multiple @inject, @multiInject or @unmanaged decorators found")}!function(e){e[e.injectionDecoratorConflict=0]="injectionDecoratorConflict",e[e.missingInjectionDecorator=1]="missingInjectionDecorator",e[e.planning=2]="planning",e[e.resolution=3]="resolution",e[e.unknown=4]="unknown"}(C||(C={})),function(e){e[e.unknown=32]="unknown"}(B||(B={})),function(e){e[e.multipleInjection=0]="multipleInjection",e[e.singleInjection=1]="singleInjection",e[e.unmanaged=2]="unmanaged"}($||($={}));const V=P((function(e,t){return{kind:e,name:void 0,optional:!1,tags:new Map,value:t}}),(function(e,t,n){return O(e),{...e,kind:t,value:n}}));function F(e,t){return n=>{const i=n.properties.get(t);return n.properties.set(t,e(i)),n}}var E;function N(e,t,n,i){if(A.isErrorOfKind(i,C.injectionDecoratorConflict)){const o=function(e,t,n){if(void 0===n){if(void 0===t)throw new A(C.unknown,"Unexpected undefined property and index values");return{kind:E.property,property:t,targetClass:e.constructor}}return"number"==typeof n?{index:n,kind:E.parameter,targetClass:e}:{kind:E.method,method:t,targetClass:e}}(e,t,n);throw new A(C.injectionDecoratorConflict,`Unexpected injection error.\n\nCause:\n\n${i.message}\n\nDetails\n\n${function(e){switch(e.kind){case E.method:return`[class: "${e.targetClass.name}", method: "${e.method.toString()}"]`;case E.parameter:return`[class: "${e.targetClass.name}", index: "${e.index.toString()}"]`;case E.property:return`[class: "${e.targetClass.name}", property: "${e.property.toString()}"]`}}(o)}`,{cause:i})}throw i}function U(e,t){return(i,o,r)=>{try{void 0===r?function(e,t){const i=K(e,t);return(e,t)=>{n(e.constructor,S,j,F(i(e),t))}}(e,t)(i,o):"number"==typeof r?function(e,t){const i=K(e,t);return(e,t,o)=>{if(!function(e,t){return"function"==typeof e&&void 0===t}(e,t))throw new A(C.injectionDecoratorConflict,`Found an @inject decorator in a non constructor parameter.\nFound @inject decorator at method "${t?.toString()??""}" at class "${e.constructor.name}"`);n(e,S,j,function(e,t){return n=>{const i=n.constructorArguments[t];return n.constructorArguments[t]=e(i),n}}(i(e),o))}}(e,t)(i,o,r):function(e,t){const i=K(e,t);return(e,t,o)=>{if(!function(e){return void 0!==e.set}(o))throw new A(C.injectionDecoratorConflict,`Found an @inject decorator in a non setter property method.\nFound @inject decorator at method "${t.toString()}" at class "${e.constructor.name}"`);n(e.constructor,S,j,F(i(e),t))}}(e,t)(i,o,r)}catch(e){N(i,o,r,e)}}}function K(e,t){return n=>{const i=t(n);return t=>(i(t),e(t))}}function _(e){return U(V($.singleInjection,e),k)}!function(e){e[e.method=0]="method",e[e.parameter=1]="parameter",e[e.property=2]="property"}(E||(E={}));const q="@inversifyjs/core/classIsInjectableFlagReflectKey";const z=[Array,BigInt,Boolean,Function,Number,Object,String];function G(t){const i=e(t,"design:paramtypes");void 0!==i&&n(t,S,j,function(e){return t=>(e.forEach(((e,n)=>{var i;void 0!==t.constructorArguments[n]||(i=e,z.includes(i))||(t.constructorArguments[n]=function(e){return{isFromTypescriptParamType:!0,kind:$.singleInjection,name:void 0,optional:!1,tags:new Map,value:e}}(e))})),t)}(i))}function L(i){return o=>{!function(n){if(void 0!==e(n,q))throw new A(C.injectionDecoratorConflict,`Cannot apply @injectable decorator multiple times at class "${n.name}"`);t(n,q,!0)}(o),G(o),void 0!==i&&n(o,S,j,(e=>({...e,scope:i})))}}function W(e,t,n){let i;return e.extendConstructorArguments??!0?(i=[...t.constructorArguments],n.constructorArguments.map(((e,t)=>{i[t]=e}))):i=n.constructorArguments,i}function X(e,t,n){let i;return i=e.extendProperties??!0?new Map(l(t.properties,n.properties)):n.properties,i}function H(e){return t=>{const i=x(e.type);n(t,S,j,function(e,t){const n=n=>({constructorArguments:W(e,t,n),lifecycle:n.lifecycle,properties:X(e,t,n),scope:n.scope});return n}(e,i))}}function J(e){return t=>{const n=i(t);if(void 0===n)throw new A(C.injectionDecoratorConflict,`Expected base type for type "${t.name}", none found.`);H({...e,type:n})(t)}}function Q(e){return U(V($.multipleInjection,e),k)}function Y(e){return t=>{void 0===t&&n(e,R,D,(e=>e+1))}}function Z(e){return t=>{const n=t??{kind:B.unknown,name:void 0,optional:!1,tags:new Map};if(n.kind===$.unmanaged)throw new A(C.injectionDecoratorConflict,"Unexpected injection found. Found @unmanaged injection with additional @named, @optional, @tagged or @targetName injections");return e(n)}}function ee(e){const t=Z(function(e){return t=>{if(void 0!==t.name)throw new A(C.injectionDecoratorConflict,"Unexpected duplicated named decorator");return t.name=e,t}}(e));return U(t,Y)}function te(e){if(e.optional)throw new A(C.injectionDecoratorConflict,"Unexpected duplicated optional decorator");return e.optional=!0,e}function ne(){return U(Z(te),Y)}function ie(){return(e,t,i)=>{try{n(e.constructor,S,j,(o=t,e=>{if(void 0!==e.lifecycle.postConstructMethodName)throw new A(C.injectionDecoratorConflict,"Unexpected duplicated postConstruct decorator");return e.lifecycle.postConstructMethodName=o,e}))}catch(n){N(e,t,void 0,n)}var o}}function oe(){return(e,t,i)=>{try{n(e.constructor,S,j,(o=t,e=>{if(void 0!==e.lifecycle.preDestroyMethodName)throw new A(C.injectionDecoratorConflict,"Unexpected duplicated preDestroy decorator");return e.lifecycle.preDestroyMethodName=o,e}))}catch(n){N(e,t,void 0,n)}var o}}function re(e,t){const n=Z(function(e,t){return n=>{if(n.tags.has(e))throw new A(C.injectionDecoratorConflict,"Unexpected duplicated tag decorator with existing tag");return n.tags.set(e,t),n}}(e,t));return U(n,Y)}function ae(){return{kind:$.unmanaged}}const se=P(ae,(function(e){if(O(e),function(e){return void 0!==e.name||e.optional||e.tags.size>0}(e))throw new A(C.injectionDecoratorConflict,"Unexpected injection found. Found @unmanaged injection with additional @named, @optional, @tagged or @targetName injections");return ae()}));function ce(){return U(se(),k)}var ue;!function(e){e[e.multipleInjection=0]="multipleInjection",e[e.singleInjection=1]="singleInjection"}(ue||(ue={}));class de{#v;constructor(e){this.#v=e}get name(){return this.#v.elem.name}get serviceIdentifier(){return this.#v.elem.serviceIdentifier}get tags(){return this.#v.elem.tags}getAncestor(){if(void 0!==this.#v.previous)return new de(this.#v.previous)}}class le{last;constructor(e){this.last=e}concat(e){return new le({elem:e,previous:this.last})}[Symbol.iterator](){let e=this.last;return{next:()=>{if(void 0===e)return{done:!0,value:void 0};const t=e.elem;return e=e.previous,{done:!1,value:t}}}}}function pe(e,t,n){const i=n?.customServiceIdentifier??t.serviceIdentifier,o=[...e.getBindings(i)??[]].filter((e=>e.isSatisfiedBy(t)));if(0===o.length&&void 0!==e.autobindOptions&&"function"==typeof i){const t=function(e,t){const n=x(t),i=n.scope??e.scope;return{cache:{isRight:!1,value:void 0},id:c(),implementationType:t,isSatisfiedBy:()=>!0,moduleId:void 0,onActivation:void 0,onDeactivation:void 0,scope:i,serviceIdentifier:t,type:d.Instance}}(e.autobindOptions,i);e.setBinding(t),o.push(t)}return o}function fe(e){return void 0!==e.redirections}function ge(e,t,n,i){let r,a;fe(n)?(r=n.binding.targetServiceIdentifier,a=n.binding.serviceIdentifier):(r=n.serviceIdentifier,a=n.parent?.binding.serviceIdentifier),Array.isArray(e)?function(e,t,n,i,r){if(0!==e.length){const t=`Ambiguous bindings found for service: "${o(n)}".\n\nRegistered bindings:\n\n${e.map((e=>function(e){switch(e.type){case d.Instance:return`[ type: "${e.type}", serviceIdentifier: "${o(e.serviceIdentifier)}", scope: "${e.scope}", implementationType: "${e.implementationType.name}" ]`;case d.ServiceRedirection:return`[ type: "${e.type}", serviceIdentifier: "${o(e.serviceIdentifier)}", redirection: "${o(e.targetServiceIdentifier)}" ]`;default:return`[ type: "${e.type}", serviceIdentifier: "${o(e.serviceIdentifier)}", scope: "${e.scope}" ]`}}(e.binding))).join("\n")}\n\nTrying to resolve bindings for "${me(n,i)}".\n\n${ve(r)}`;throw new A(C.planning,t)}t||he(n,i,r)}(e,t,r,a,i):function(e,t,n,i,o){if(void 0!==e||t)return;he(n,i,o)}(e,t,r,a,i)}function he(e,t,n){const i=`No bindings found for service: "${o(e)}".\n\nTrying to resolve bindings for "${me(e,t)}".\n\n${ve(n)}`;throw new A(C.planning,i)}function me(e,t){return void 0===t?`${o(e)} (Root service)`:o(t)}function ve(e){const t=0===e.tags.size?"":`\n- tags:\n - ${[...e.tags.keys()].map((e=>e.toString())).join("\n - ")}`;return`Binding constraints:\n- service identifier: ${o(e.serviceIdentifier)}\n- name: ${e.name?.toString()??"-"}${t}`}function ye(e,t,n){if(1!==e.redirections.length)ge(e.redirections,t,e,n);else{const[i]=e.redirections;fe(i)&&ye(i,t,n)}}function Me(e,t,n){if(Array.isArray(e.bindings)&&1===e.bindings.length){const[i]=e.bindings;fe(i)&&ye(i,t,n)}else ge(e.bindings,t,e,n)}function Ie(e,t){if(function(e){return e instanceof Error&&(e instanceof RangeError&&/stack space|call stack|too much recursion/i.test(e.message)||"InternalError"===e.name&&/too much recursion/.test(e.message))}(t)){const n=function(e){const t=[...e];if(0===t.length)return"(No dependency trace)";return t.map(o).join(" -> ")}(function(e){const t=new Set;for(const n of e.servicesBranch){if(t.has(n))return[...t,n];t.add(n)}return[...t]}(e));throw new A(C.planning,`Circular dependency found: ${n}`,{cause:t})}throw t}function we(e){try{const t=new Map;void 0!==e.rootConstraints.tag&&t.set(e.rootConstraints.tag.key,e.rootConstraints.tag.value);const n=new le({elem:{name:e.rootConstraints.name,serviceIdentifier:e.rootConstraints.serviceIdentifier,tags:t},previous:void 0}),i=new de(n.last),o=pe(e,i),r=[],a={bindings:r,parent:void 0,serviceIdentifier:e.rootConstraints.serviceIdentifier};if(r.push(...Te(e,n,o,a)),!e.rootConstraints.isMultiple){Me(a,e.rootConstraints.isOptional??!1,i);const[t]=r;a.bindings=t}return{tree:{root:a}}}catch(t){Ie(e,t)}}function be(e,t,n,i){const o={binding:t,classMetadata:e.getClassMetadata(t.implementationType),constructorParams:[],parent:i,propertyParams:new Map};return Ce({autobindOptions:e.autobindOptions,getBindings:e.getBindings,getClassMetadata:e.getClassMetadata,node:o,servicesBranch:e.servicesBranch,setBinding:e.setBinding},n)}function Se(e,t,n){if(n.kind===$.unmanaged)return;const i=r.is(n.value)?n.value.unwrap():n.value,o=t.concat({name:n.name,serviceIdentifier:i,tags:n.tags}),a=new de(o.last),s=pe(e,a),c=[],u={bindings:c,parent:e.node,serviceIdentifier:i};if(c.push(...Te(e,o,s,u)),n.kind===$.singleInjection){Me(u,n.optional,a);const[e]=c;u.bindings=e}return u}function je(e,t,n){const i=r.is(n.value)?n.value.unwrap():n.value,o=t.concat({name:n.name,serviceIdentifier:i,tags:n.tags}),a=new de(o.last),s=pe(e,a),c=[],u={bindings:c,parent:e.node,serviceIdentifier:i};if(c.push(...Te(e,o,s,u)),n.kind===ue.singleInjection){Me(u,n.optional,a);const[e]=c;u.bindings=e}return u}function Re(e,t,n,i){const o={binding:t,params:[],parent:i};return Ce({autobindOptions:e.autobindOptions,getBindings:e.getBindings,getClassMetadata:e.getClassMetadata,node:o,servicesBranch:e.servicesBranch,setBinding:e.setBinding},n)}function Te(e,t,n,i){const o=fe(i)?i.binding.targetServiceIdentifier:i.serviceIdentifier;e.servicesBranch.push(o);const r=[];for(const o of n)switch(o.type){case d.Instance:r.push(be(e,o,t,i));break;case d.ResolvedValue:r.push(Re(e,o,t,i));break;case d.ServiceRedirection:{const n=Ae(e,t,o,i);r.push(n);break}default:r.push({binding:o,parent:i})}return e.servicesBranch.pop(),r}function Ae(e,t,n,i){const o={binding:n,parent:i,redirections:[]},r=pe(e,new de(t.last),{customServiceIdentifier:n.targetServiceIdentifier});return o.redirections.push(...Te(e,t,r,o)),o}function Ce(e,t){return e.node.binding.type===d.Instance?function(e,t,n){const i=t.classMetadata;for(const[o,r]of i.constructorArguments.entries())t.constructorParams[o]=Se(e,n,r);for(const[o,r]of i.properties){const i=Se(e,n,r);void 0!==i&&t.propertyParams.set(o,i)}return e.node}(e,e.node,t):function(e,t,n){const i=t.binding.metadata;for(const[o,r]of i.arguments.entries())t.params[o]=je(e,n,r);return e.node}(e,e.node,t)}class Be{#y;#M;#I;constructor(){this.#y=[],this.#M=8,this.#I=1024}*[Symbol.iterator](){let e=0;for(const t of this.#y){const n=t.deref();void 0===n?++e:yield n}this.#y.length>=this.#M&&this.#w(e)&&this.#b(e)}push(e){const t=new WeakRef(e);if(this.#y.push(t),this.#y.length>=this.#M&&this.#y.length%this.#I===0){let e=0;for(const t of this.#y)void 0===t.deref()&&++e;this.#w(e)&&this.#b(e)}}#b(e){const t=new Array(this.#y.length-e);let n=0;for(const e of this.#y)e.deref()&&(t[n++]=e);this.#y=t}#w(e){return e>=.5*this.#y.length}}var $e;!function(e){e[e.singleMandatory=0]="singleMandatory",e[e.singleOptional=1]="singleOptional",e[e.multipleMandatory=2]="multipleMandatory",e[e.multipleOptional=3]="multipleOptional",e[e.length=4]="length"}($e||($e={}));class xe{#S;#j;#R;#T;#A;constructor(){this.#S=this.#C(),this.#j=this.#C(),this.#R=this.#C(),this.#T=this.#C(),this.#A=new Be}clearCache(){for(const e of this.#B())e.clear();for(const e of this.#A)e.clearCache()}get(e){return void 0===e.name?void 0===e.tag?this.#$(this.#S,e).get(e.serviceIdentifier):this.#$(this.#T,e).get(e.serviceIdentifier)?.get(e.tag.key)?.get(e.tag.value):void 0===e.tag?this.#$(this.#j,e).get(e.serviceIdentifier)?.get(e.name):this.#$(this.#R,e).get(e.serviceIdentifier)?.get(e.name)?.get(e.tag.key)?.get(e.tag.value)}set(e,t){void 0===e.name?void 0===e.tag?this.#$(this.#S,e).set(e.serviceIdentifier,t):this.#x(this.#x(this.#$(this.#T,e),e.serviceIdentifier),e.tag.key).set(e.tag.value,t):void 0===e.tag?this.#x(this.#$(this.#j,e),e.serviceIdentifier).set(e.name,t):this.#x(this.#x(this.#x(this.#$(this.#R,e),e.serviceIdentifier),e.name),e.tag.key).set(e.tag.value,t)}subscribe(e){this.#A.push(e)}#C(){const e=new Array($e.length);for(let t=0;t<e.length;++t)e[t]=new Map;return e}#x(e,t){let n=e.get(t);return void 0===n&&(n=new Map,e.set(t,n)),n}#$(e,t){return e[this.#D(t)]}#B(){return[...this.#S,...this.#j,...this.#R,...this.#T]}#D(e){return e.isMultiple?!0===e.optional?$e.multipleOptional:$e.multipleMandatory:!0===e.optional?$e.singleOptional:$e.singleMandatory}}function De(e,t){return a(t)?(e.cache={isRight:!0,value:t},t.then((t=>ke(e,t)))):ke(e,t)}function ke(e,t){return e.cache={isRight:!0,value:t},t}function Pe(e,t,n){const i=e.getActivations(t);return void 0===i?n:a(n)?Oe(e,n,i[Symbol.iterator]()):function(e,t,n){let i=t,o=n.next();for(;!0!==o.done;){const t=o.value(e.context,i);if(a(t))return Oe(e,t,n);i=t,o=n.next()}return i}(e,n,i[Symbol.iterator]())}async function Oe(e,t,n){let i=await t,o=n.next();for(;!0!==o.done;)i=await o.value(e.context,i),o=n.next();return i}function Ve(e,t,n){let i=n;if(void 0!==t.onActivation){const n=t.onActivation;i=a(i)?i.then((t=>n(e.context,t))):n(e.context,i)}return Pe(e,t.serviceIdentifier,i)}function Fe(e){return(t,n)=>{if(n.cache.isRight)return n.cache.value;return De(n,Ve(t,n,e(t,n)))}}const Ee=Fe((function(e,t){return t.value}));function Ne(e){return e}function Ue(e,t){return(n,i)=>{const o=e(i);switch(o.scope){case u.Singleton:if(o.cache.isRight)return o.cache.value;return De(o,Ve(n,o,t(n,i)));case u.Request:{if(n.requestScopeCache.has(o.id))return n.requestScopeCache.get(o.id);const e=Ve(n,o,t(n,i));return n.requestScopeCache.set(o.id,e),e}case u.Transient:return Ve(n,o,t(n,i))}}}const Ke=(e=>Ue(Ne,e))((function(e,t){return t.value(e.context)}));const _e=Fe((function(e,t){return t.factory(e.context)}));function qe(e,t,n){const i=function(e,t,n){if(void 0===n)return;if(!(n in e))throw new A(C.resolution,`Expecting a "${n.toString()}" property when resolving "${t.implementationType.name}" class @postConstruct decorated method, none found.`);if("function"!=typeof e[n])throw new A(C.resolution,`Expecting a "${n.toString()}" method when resolving "${t.implementationType.name}" class @postConstruct decorated method, a non function property was found instead.`);{let i;try{i=e[n]()}catch(e){throw new A(C.resolution,`Unexpected error found when calling "${n.toString()}" @postConstruct decorated method on class "${t.implementationType.name}"`,{cause:e})}if(a(i))return async function(e,t,n){try{await n}catch(n){throw new A(C.resolution,`Unexpected error found when calling "${t.toString()}" @postConstruct decorated method on class "${e.implementationType.name}"`,{cause:n})}}(t,n,i)}}(e,t,n);return a(i)?i.then((()=>e)):e}function ze(e){return(t,n,i)=>{const o=new i.binding.implementationType(...t),r=e(n,o,i);return a(r)?r.then((()=>qe(o,i.binding,i.classMetadata.lifecycle.postConstructMethodName))):qe(o,i.binding,i.classMetadata.lifecycle.postConstructMethodName)}}const Ge=Fe((function(e,t){return t.provider(e.context)}));function Le(e){return e.binding}function We(e){return e.binding}const Xe=function(e){return(t,n,i)=>{const o=[];for(const[r,s]of i.propertyParams){const c=i.classMetadata.properties.get(r);if(void 0===c)throw new A(C.resolution,`Expecting metadata at property "${r.toString()}", none found`);c.kind!==$.unmanaged&&void 0!==s.bindings&&(n[r]=e(t,s),a(n[r])&&o.push((async()=>{n[r]=await n[r]})()))}if(o.length>0)return Promise.all(o).then((()=>{}))}}(nt),He=function(e){return function t(n,i){const o=[];for(const r of i.redirections)fe(r)?o.push(...t(n,r)):o.push(e(n,r));return o}}(tt),Je=function(e,t,n){return(i,o)=>{const r=e(i,o);return a(r)?t(r,i,o):n(r,i,o)}}(function(e){return(t,n)=>{const i=[];for(const o of n.constructorParams)void 0===o?i.push(void 0):i.push(e(t,o));return i.some(a)?Promise.all(i):i}}(nt),function(e){return async(t,n,i)=>{const o=await t;return e(o,n,i)}}(ze(Xe)),ze(Xe)),Qe=function(e){return(t,n)=>{const i=e(t,n);return a(i)?i.then((e=>n.binding.factory(...e))):n.binding.factory(...i)}}(function(e){return(t,n)=>{const i=[];for(const o of n.params)i.push(e(t,o));return i.some(a)?Promise.all(i):i}}(nt)),Ye=(e=>Ue(Le,e))(Je),Ze=(e=>Ue(We,e))(Qe);function et(e){return nt(e,e.planResult.tree.root)}function tt(e,t){switch(t.binding.type){case d.ConstantValue:return Ee(e,t.binding);case d.DynamicValue:return Ke(e,t.binding);case d.Factory:return _e(e,t.binding);case d.Instance:return Ye(e,t);case d.Provider:return Ge(e,t.binding);case d.ResolvedValue:return Ze(e,t)}}function nt(e,t){if(void 0!==t.bindings)return Array.isArray(t.bindings)?function(e,t){const n=[];for(const i of t)fe(i)?n.push(...He(e,i)):n.push(tt(e,i));if(n.some(a))return Promise.all(n);return n}(e,t.bindings):function(e,t){if(fe(t)){const n=He(e,t);if(1===n.length)return n[0];throw new A(C.resolution,"Unexpected multiple resolved values on single injection")}return tt(e,t)}(e,t.bindings)}function it(e){return void 0!==e.scope}function ot(e,t){if(void 0!==e.lifecycle.preDestroyMethodName&&"function"==typeof t[e.lifecycle.preDestroyMethodName])return t[e.lifecycle.preDestroyMethodName]()}function rt(e,t,n){const i=e.getDeactivations(t);if(void 0!==i)return a(n)?at(n,i[Symbol.iterator]()):function(e,t){let n=t.next();for(;!0!==n.done;){const i=n.value(e);if(a(i))return at(e,t);n=t.next()}}(n,i[Symbol.iterator]())}async function at(e,t){const n=await e;let i=t.next();for(;!0!==i.done;)await i.value(n),i=t.next()}function st(e,t){const n=function(e,t){if(t.type===d.Instance){const n=e.getClassMetadata(t.implementationType),i=t.cache.value;return a(i)?i.then((e=>ot(n,e))):ot(n,i)}}(e,t);return void 0===n?ct(e,t):n.then((()=>ct(e,t)))}function ct(e,t){const n=t.cache;return a(n.value)?n.value.then((n=>ut(e,t,n))):ut(e,t,n.value)}function ut(e,t,n){let i;if(void 0!==t.onDeactivation){i=(0,t.onDeactivation)(n)}return void 0===i?rt(e,t.serviceIdentifier,n):i.then((()=>rt(e,t.serviceIdentifier,n)))}function dt(e,t){if(void 0===t)return;const n=function(e){const t=[];for(const n of e)it(n)&&n.scope===u.Singleton&&n.cache.isRight&&t.push(n);return t}(t),i=[];for(const t of n){const n=st(e,t);void 0!==n&&i.push(n)}return i.length>0?Promise.all(i).then((()=>{})):void 0}function lt(e,t){const n=e.getBindingsFromModule(t);return dt(e,n)}function pt(e,t){const n=e.getBindings(t);return dt(e,n)}export{m as ActivationsService,I as BindingService,$ as ClassElementMetadataKind,w as DeactivationsService,xe as PlanResultCacheService,ue as ResolvedValueElementMetadataKind,u as bindingScopeValues,d as bindingTypeValues,b as decorate,c as getBindingId,x as getClassMetadata,_ as inject,J as injectFromBase,L as injectable,Q as multiInject,ee as named,ne as optional,we as plan,ie as postConstruct,oe as preDestroy,et as resolve,dt as resolveBindingsDeactivations,lt as resolveModuleDeactivations,pt as resolveServiceDeactivations,re as tagged,ce as unmanaged}; //# sourceMappingURL=index.js.map