@inversifyjs/container
Version:
InversifyJs container
3 lines (2 loc) • 17.5 kB
JavaScript
import"reflect-metadata";import{getOwnReflectMetadata as e,setReflectMetadata as n,updateOwnReflectMetadata as i}from"@inversifyjs/reflect-metadata-utils";import{getClassMetadata as t,bindingTypeValues as r,getBindingId as a,bindingScopeValues as s,ResolvedValueElementMetadataKind as o,resolveServiceDeactivations as c,CacheBindingInvalidationKind as d,resolveBindingsDeactivations as l,resolveModuleDeactivations as u,plan as h,resolve as v,ActivationsService as g,BindingService as f,DeactivationsService as b,PlanResultCacheService as p}from"@inversifyjs/core";import{LazyServiceIdentifier as S,stringifyServiceIdentifier as M,isPromise as R}from"@inversifyjs/common";import{isPlugin as y}from"@inversifyjs/plugin";const m="@inversifyjs/container/bindingId";class w{#e;#n;constructor(t){this.#e=function(){const t=e(Object,m)??0;return t===Number.MAX_SAFE_INTEGER?n(Object,m,Number.MIN_SAFE_INTEGER):i(Object,m,()=>t,e=>e+1),t}(),this.#n=t}get id(){return this.#e}load(e){return this.#n(e)}}const I=Symbol.for("@inversifyjs/container/bindingIdentifier");function P(e){return"object"==typeof e&&null!==e&&!0===e[I]}class A{static always=e=>!0}const C=Symbol.for("@inversifyjs/container/InversifyContainerError");class B extends Error{[C];kind;constructor(e,n,i){super(n,i),this[C]=!0,this.kind=e}static is(e){return"object"==typeof e&&null!==e&&!0===e[C]}static isErrorOfKind(e,n){return B.is(e)&&e.kind===n}}var O;function x(e){return{[I]:!0,id:e.id}}function k(e){return n=>{for(let i=n.getAncestor();void 0!==i;i=i.getAncestor())if(e(i))return!0;return!1}}function N(e){return n=>n.name===e}function F(e){return n=>n.serviceIdentifier===e}function U(e,n){return i=>i.tags.has(e)&&i.tags.get(e)===n}function D(e){return void 0===e.name&&0===e.tags.size}function j(e){const n=k(e);return e=>!n(e)}function T(e){return n=>{const i=n.getAncestor();return void 0===i||!e(i)}}function V(e){return n=>{const i=n.getAncestor();return void 0!==i&&e(i)}}!function(e){e[e.invalidOperation=0]="invalidOperation"}(O||(O={}));class E{#i;constructor(e){this.#i=e}getIdentifier(){return x(this.#i)}inRequestScope(){return this.#i.scope=s.Request,new G(this.#i)}inSingletonScope(){return this.#i.scope=s.Singleton,new G(this.#i)}inTransientScope(){return this.#i.scope=s.Transient,new G(this.#i)}}class L{#t;#r;#a;#s;constructor(e,n,i,t){this.#t=e,this.#r=n,this.#a=i,this.#s=t}to(e){const n=t(e),i={cache:{isRight:!1,value:void 0},id:a(),implementationType:e,isSatisfiedBy:A.always,moduleId:this.#r,onActivation:void 0,onDeactivation:void 0,scope:n.scope??this.#a,serviceIdentifier:this.#s,type:r.Instance};return this.#t(i),new H(i)}toSelf(){if("function"!=typeof this.#s)throw new Error('"toSelf" function can only be applied when a newable function is used as service identifier');return this.to(this.#s)}toConstantValue(e){const n={cache:{isRight:!1,value:void 0},id:a(),isSatisfiedBy:A.always,moduleId:this.#r,onActivation:void 0,onDeactivation:void 0,scope:s.Singleton,serviceIdentifier:this.#s,type:r.ConstantValue,value:e};return this.#t(n),new G(n)}toDynamicValue(e){const n={cache:{isRight:!1,value:void 0},id:a(),isSatisfiedBy:A.always,moduleId:this.#r,onActivation:void 0,onDeactivation:void 0,scope:this.#a,serviceIdentifier:this.#s,type:r.DynamicValue,value:e};return this.#t(n),new H(n)}toResolvedValue(e,n){const i={cache:{isRight:!1,value:void 0},factory:e,id:a(),isSatisfiedBy:A.always,metadata:this.#o(n),moduleId:this.#r,onActivation:void 0,onDeactivation:void 0,scope:this.#a,serviceIdentifier:this.#s,type:r.ResolvedValue};return this.#t(i),new H(i)}toFactory(e){const n={cache:{isRight:!1,value:void 0},factory:e,id:a(),isSatisfiedBy:A.always,moduleId:this.#r,onActivation:void 0,onDeactivation:void 0,scope:s.Singleton,serviceIdentifier:this.#s,type:r.Factory};return this.#t(n),new G(n)}toProvider(e){const n={cache:{isRight:!1,value:void 0},id:a(),isSatisfiedBy:A.always,moduleId:this.#r,onActivation:void 0,onDeactivation:void 0,provider:e,scope:s.Singleton,serviceIdentifier:this.#s,type:r.Provider};return this.#t(n),new G(n)}toService(e){const n={id:a(),isSatisfiedBy:A.always,moduleId:this.#r,serviceIdentifier:this.#s,targetServiceIdentifier:e,type:r.ServiceRedirection};this.#t(n)}#o(e){return{arguments:(e??[]).map(e=>function(e){return"object"==typeof e&&!S.is(e)}(e)?function(e){return!0===e.isMultiple}(e)?{chained:e.chained??!1,kind:o.multipleInjection,name:e.name,optional:e.optional??!1,tags:new Map((e.tags??[]).map(e=>[e.key,e.value])),value:e.serviceIdentifier}:{kind:o.singleInjection,name:e.name,optional:e.optional??!1,tags:new Map((e.tags??[]).map(e=>[e.key,e.value])),value:e.serviceIdentifier}:{kind:o.singleInjection,name:void 0,optional:!1,tags:new Map,value:e})}}}class ${#i;constructor(e){this.#i=e}getIdentifier(){return x(this.#i)}onActivation(e){return this.#i.onActivation=e,new q(this.#i)}onDeactivation(e){if(this.#i.onDeactivation=e,this.#i.scope!==s.Singleton)throw new B(O.invalidOperation,`Binding for service "${M(this.#i.serviceIdentifier)}" has a deactivation function, but its scope is not singleton. Deactivation functions can only be used with singleton bindings.`);return new q(this.#i)}}class q{#i;constructor(e){this.#i=e}getIdentifier(){return x(this.#i)}when(e){return this.#i.isSatisfiedBy=e,new $(this.#i)}whenAnyAncestor(e){return this.when(k(e))}whenAnyAncestorIs(e){return this.when(k(F(e)))}whenAnyAncestorNamed(e){return this.when(function(e){return k(N(e))}(e))}whenAnyAncestorTagged(e,n){return this.when(function(e,n){return k(U(e,n))}(e,n))}whenDefault(){return this.when(D)}whenNamed(e){return this.when(N(e))}whenNoParent(e){return this.when(T(e))}whenNoParentIs(e){return this.when(T(F(e)))}whenNoParentNamed(e){return this.when(function(e){return T(N(e))}(e))}whenNoParentTagged(e,n){return this.when(function(e,n){return T(U(e,n))}(e,n))}whenParent(e){return this.when(V(e))}whenParentIs(e){return this.when(V(F(e)))}whenParentNamed(e){return this.when(function(e){return V(N(e))}(e))}whenParentTagged(e,n){return this.when(function(e,n){return V(U(e,n))}(e,n))}whenTagged(e,n){return this.when(U(e,n))}whenNoAncestor(e){return this.when(j(e))}whenNoAncestorIs(e){return this.when(j(F(e)))}whenNoAncestorNamed(e){return this.when(function(e){return j(N(e))}(e))}whenNoAncestorTagged(e,n){return this.when(function(e,n){return j(U(e,n))}(e,n))}}class G extends q{#c;constructor(e){super(e),this.#c=new $(e)}onActivation(e){return this.#c.onActivation(e)}onDeactivation(e){return this.#c.onDeactivation(e)}}class H extends G{#d;constructor(e){super(e),this.#d=new E(e)}inRequestScope(){return this.#d.inRequestScope()}inSingletonScope(){return this.#d.inSingletonScope()}inTransientScope(){return this.#d.inTransientScope()}}class _{#l;#a;#u;#h;constructor(e,n,i,t){this.#l=e,this.#a=n,this.#u=i,this.#h=t}bind(e){return new L(e=>{this.#v(e)},void 0,this.#a,e)}isBound(e,n){const i=this.#h.bindingService.get(e);return this.#g(e,i,n)}isCurrentBound(e,n){const i=this.#h.bindingService.getNonParentBindings(e);return this.#g(e,i,n)}async rebind(e){return await this.unbind(e),this.bind(e)}rebindSync(e){return this.unbindSync(e),this.bind(e)}async unbind(e){await this.#f(e)}async unbindAll(){const e=[...this.#h.bindingService.getNonParentBoundServices()];await Promise.all(e.map(async e=>c(this.#l,e)));for(const n of e)this.#h.activationService.removeAllByServiceId(n),this.#h.bindingService.removeAllByServiceId(n),this.#h.deactivationService.removeAllByServiceId(n);this.#h.planResultCacheService.clearCache()}unbindSync(e){void 0!==this.#f(e)&&this.#b(e)}#v(e){this.#h.bindingService.set(e),this.#u.invalidateService({binding:e,kind:d.bindingAdded})}#b(e){let n;if(P(e)){const t=this.#h.bindingService.getById(e.id),r=(i=t,function(e){if(void 0===e)return;const n=e.next();return!0!==n.done?n.value:void 0}(i?.[Symbol.iterator]()))?.serviceIdentifier;n=void 0===r?"Unexpected asynchronous deactivation when unbinding binding identifier. Consider using Container.unbind() instead.":`Unexpected asynchronous deactivation when unbinding "${M(r)}" binding. Consider using Container.unbind() instead.`}else n=`Unexpected asynchronous deactivation when unbinding "${M(e)}" service. Consider using Container.unbind() instead.`;var i;throw new B(O.invalidOperation,n)}#f(e){return P(e)?this.#p(e):this.#S(e)}#p(e){const n=this.#h.bindingService.getById(e.id),i=void 0===n?void 0:[...n],t=l(this.#l,n);if(void 0!==t)return t.then(()=>{this.#M(i,e)});this.#M(i,e)}#M(e,n){if(this.#h.bindingService.removeById(n.id),void 0!==e)for(const n of e)this.#u.invalidateService({binding:n,kind:d.bindingRemoved})}#S(e){const n=this.#h.bindingService.get(e),i=void 0===n?void 0:[...n],t=l(this.#l,n);if(void 0!==t)return t.then(()=>{this.#R(e,i)});this.#R(e,i)}#R(e,n){if(this.#h.activationService.removeAllByServiceId(e),this.#h.bindingService.removeAllByServiceId(e),this.#h.deactivationService.removeAllByServiceId(e),void 0!==n)for(const e of n)this.#u.invalidateService({binding:e,kind:d.bindingRemoved})}#g(e,n,i){if(void 0===n)return!1;const t={getAncestor:()=>{},name:i?.name,serviceIdentifier:e,tags:new Map};void 0!==i?.tag&&t.tags.set(i.tag.key,i.tag.value);for(const e of n)if(e.isSatisfiedBy(t))return!0;return!1}}class z{#y;#l;#a;#u;#h;constructor(e,n,i,t,r){this.#y=e,this.#l=n,this.#a=i,this.#u=t,this.#h=r}async load(...e){await Promise.all(this.#n(...e))}loadSync(...e){const n=this.#n(...e);for(const e of n)if(void 0!==e)throw new B(O.invalidOperation,"Unexpected asynchronous module load. Consider using Container.load() instead.")}async unload(...e){await Promise.all(this.#m(...e)),this.#w(e)}unloadSync(...e){const n=this.#m(...e);for(const e of n)if(void 0!==e)throw new B(O.invalidOperation,"Unexpected asynchronous module unload. Consider using Container.unload() instead.");this.#w(e)}#I(e){return{bind:n=>new L(e=>{this.#v(e)},e,this.#a,n),isBound:this.#y.isBound.bind(this.#y),onActivation:(n,i)=>{this.#h.activationService.add(i,{moduleId:e,serviceId:n})},onDeactivation:(n,i)=>{this.#h.deactivationService.add(i,{moduleId:e,serviceId:n})},rebind:this.#y.rebind.bind(this.#y),rebindSync:this.#y.rebindSync.bind(this.#y),unbind:this.#y.unbind.bind(this.#y),unbindSync:this.#y.unbindSync.bind(this.#y)}}#w(e){for(const n of e)this.#h.activationService.removeAllByModuleId(n.id),this.#h.bindingService.removeAllByModuleId(n.id),this.#h.deactivationService.removeAllByModuleId(n.id);this.#h.planResultCacheService.clearCache()}#n(...e){return e.map(e=>e.load(this.#I(e.id)))}#v(e){this.#h.bindingService.set(e),this.#u.invalidateService({binding:e,kind:d.bindingAdded})}#m(...e){return e.map(e=>u(this.#l,e.id))}}class K{deactivationParams;constructor(e){this.deactivationParams=function(e){return{getBindings:e.bindingService.get.bind(e.bindingService),getBindingsFromModule:e.bindingService.getByModuleId.bind(e.bindingService),getClassMetadata:t,getDeactivations:e.deactivationService.get.bind(e.deactivationService)}}(e),e.onReset(()=>{!function(e,n){n.getBindings=e.bindingService.get.bind(e.bindingService),n.getBindingsFromModule=e.bindingService.getByModuleId.bind(e.bindingService),n.getDeactivations=e.deactivationService.get.bind(e.deactivationService)}(e,this.deactivationParams)})}}class X{planParamsOperations;#h;constructor(e){this.#h=e,this.planParamsOperations={getBindings:this.#h.bindingService.get.bind(this.#h.bindingService),getBindingsChained:this.#h.bindingService.getChained.bind(this.#h.bindingService),getClassMetadata:t,getPlan:this.#h.planResultCacheService.get.bind(this.#h.planResultCacheService),setBinding:this.#v.bind(this),setNonCachedServiceNode:this.#h.planResultCacheService.setNonCachedServiceNode.bind(this.#h.planResultCacheService),setPlan:this.#h.planResultCacheService.set.bind(this.#h.planResultCacheService)},this.#h.onReset(()=>{this.#P()})}#P(){this.planParamsOperations.getBindings=this.#h.bindingService.get.bind(this.#h.bindingService),this.planParamsOperations.getBindingsChained=this.#h.bindingService.getChained.bind(this.#h.bindingService),this.planParamsOperations.setBinding=this.#v.bind(this)}#v(e){this.#h.bindingService.set(e),this.#h.planResultCacheService.invalidateServiceBinding({binding:e,kind:d.bindingAdded,operations:this.planParamsOperations})}}class J{#A;#h;constructor(e,n){this.#A=e,this.#h=n}invalidateService(e){this.#h.planResultCacheService.invalidateServiceBinding({...e,operations:this.#A.planParamsOperations})}}class Q{#C;#B;#O;#h;constructor(e,n,i){this.#h=n,this.#O=i,this.#C=this.#x(e),this.#B=this.#k()}register(e,n){const i=new n(e,this.#B);if(!0!==i[y])throw new B(O.invalidOperation,"Invalid plugin. The plugin must extend the Plugin class");i.load(this.#C)}#x(e){return{define:(n,i)=>{if(Object.prototype.hasOwnProperty.call(e,n))throw new B(O.invalidOperation,`Container already has a method named "${String(n)}"`);e[n]=i},onPlan:this.#O.onPlan.bind(this.#O)}}#k(){const e=this.#h;return{get activationService(){return e.activationService},get bindingService(){return e.bindingService},get deactivationService(){return e.deactivationService},get planResultCacheService(){return e.planResultCacheService}}}}class W{activationService;bindingService;deactivationService;planResultCacheService;#N;constructor(e,n,i,t){this.activationService=e,this.bindingService=n,this.deactivationService=i,this.planResultCacheService=t,this.#N=[]}reset(e,n,i){this.activationService=e,this.bindingService=n,this.deactivationService=i,this.planResultCacheService.clearCache();for(const e of this.#N)e()}onReset(e){this.#N.push(e)}}class Y{#F;#a;#U;#D;#j;#A;#h;constructor(e,n,i,t){this.#A=e,this.#h=n,this.#D=this.#T(),this.#F=i,this.#a=t,this.#U=e=>this.#h.activationService.get(e),this.#j=[],this.#h.onReset(()=>{this.#P()})}get(e,n){const i=this.#V(!1,e,n),t=this.#E(i);if(R(t))throw new B(O.invalidOperation,`Unexpected asynchronous service when resolving service "${M(e)}"`);return t}getAll(e,n){const i=this.#V(!0,e,n),t=this.#E(i);if(R(t))throw new B(O.invalidOperation,`Unexpected asynchronous service when resolving service "${M(e)}"`);return t}async getAllAsync(e,n){const i=this.#V(!0,e,n);return this.#E(i)}async getAsync(e,n){const i=this.#V(!1,e,n);return this.#E(i)}onPlan(e){this.#j.push(e)}#P(){this.#D=this.#T()}#L(e,n,i){const t=i?.name,r=i?.optional??!1,a=i?.tag;return e?{chained:i?.chained??!1,isMultiple:e,name:t,optional:r,serviceIdentifier:n,tag:a}:{isMultiple:e,name:t,optional:r,serviceIdentifier:n,tag:a}}#$(e,n,i){const t={autobindOptions:i?.autobind??this.#F?{scope:this.#a}:void 0,operations:this.#A.planParamsOperations,rootConstraints:this.#q(e,n,i),servicesBranch:[]};return this.#G(t,i),t}#q(e,n,i){return n?{chained:i?.chained??!1,isMultiple:n,serviceIdentifier:e}:{isMultiple:n,serviceIdentifier:e}}#V(e,n,i){const t=this.#L(e,n,i),r=this.#h.planResultCacheService.get(t);if(void 0!==r)return r;const a=h(this.#$(n,e,i));for(const e of this.#j)e(t,a);return a}#T(){return{get:this.get.bind(this),getAll:this.getAll.bind(this),getAllAsync:this.getAllAsync.bind(this),getAsync:this.getAsync.bind(this)}}#E(e){return v({context:this.#D,getActivations:this.#U,planResult:e,requestScopeCache:new Map})}#G(e,n){void 0!==n&&(void 0!==n.name&&(e.rootConstraints.name=n.name),!0===n.optional&&(e.rootConstraints.isOptional=!0),void 0!==n.tag&&(e.rootConstraints.tag={key:n.tag.key,value:n.tag.value}),e.rootConstraints.isMultiple&&(e.rootConstraints.chained=n?.chained??!1))}}class Z{#h;#H;constructor(e){this.#h=e,this.#H=[]}restore(){const e=this.#H.pop();if(void 0===e)throw new B(O.invalidOperation,"No snapshot available to restore");this.#h.reset(e.activationService,e.bindingService,e.deactivationService)}snapshot(){this.#H.push({activationService:this.#h.activationService.clone(),bindingService:this.#h.bindingService.clone(),deactivationService:this.#h.deactivationService.clone()})}}const ee=s.Transient;class ne{#y;#_;#z;#h;#O;#K;constructor(e){this.#h=this.#X(e);const n=e?.autobind??!1,i=e?.defaultScope??ee,t=new X(this.#h),r=new J(t,this.#h),a=new K(this.#h);this.#y=new _(a.deactivationParams,i,r,this.#h),this.#_=new z(this.#y,a.deactivationParams,i,r,this.#h),this.#O=new Y(t,this.#h,n,i),this.#z=new Q(this,this.#h,this.#O),this.#K=new Z(this.#h)}bind(e){return this.#y.bind(e)}get(e,n){return this.#O.get(e,n)}getAll(e,n){return this.#O.getAll(e,n)}async getAllAsync(e,n){return this.#O.getAllAsync(e,n)}async getAsync(e,n){return this.#O.getAsync(e,n)}isBound(e,n){return this.#y.isBound(e,n)}isCurrentBound(e,n){return this.#y.isCurrentBound(e,n)}async load(...e){return this.#_.load(...e)}loadSync(...e){this.#_.loadSync(...e)}onActivation(e,n){this.#h.activationService.add(n,{serviceId:e})}onDeactivation(e,n){this.#h.deactivationService.add(n,{serviceId:e})}register(e){this.#z.register(this,e)}restore(){this.#K.restore()}async rebind(e){return this.#y.rebind(e)}rebindSync(e){return this.#y.rebindSync(e)}snapshot(){this.#K.snapshot()}async unbind(e){await this.#y.unbind(e)}async unbindAll(){return this.#y.unbindAll()}unbindSync(e){this.#y.unbindSync(e)}async unload(...e){return this.#_.unload(...e)}unloadSync(...e){this.#_.unloadSync(...e)}#X(e){if(void 0===e?.parent)return new W(g.build(()=>{}),f.build(()=>{}),b.build(()=>{}),new p);const n=new p,i=e.parent;return i.#h.planResultCacheService.subscribe(n),new W(g.build(()=>i.#h.activationService),f.build(()=>i.#h.bindingService),b.build(()=>i.#h.deactivationService),n)}}export{ne as Container,w as ContainerModule,B as InversifyContainerError,O as InversifyContainerErrorKind};
//# sourceMappingURL=index.js.map