UNPKG

@inversifyjs/container

Version:

InversifyJs container

3 lines (2 loc) 15.2 kB
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,resolveBindingsDeactivations as d,resolveModuleDeactivations as l,plan as u,resolve as h,ActivationsService as v,BindingService as g,DeactivationsService as f,PlanResultCacheService as b}from"@inversifyjs/core";import{LazyServiceIdentifier as p,stringifyServiceIdentifier as S,isPromise as M}from"@inversifyjs/common";import{isPlugin as y}from"@inversifyjs/plugin";const R="@inversifyjs/container/bindingId";class w{#e;#n;constructor(t){this.#e=function(){const t=e(Object,R)??0;return t===Number.MAX_SAFE_INTEGER?n(Object,R,Number.MIN_SAFE_INTEGER):i(Object,R,(()=>t),(e=>e+1)),t}(),this.#n=t}get id(){return this.#e}load(e){return this.#n(e)}}const m=Symbol.for("@inversifyjs/container/bindingIdentifier");function I(e){return"object"==typeof e&&null!==e&&!0===e[m]}class A{static always=e=>!0}function P(e){return{[m]:!0,id:e.id}}function C(e){return n=>{for(let i=n.getAncestor();void 0!==i;i=i.getAncestor())if(e(i))return!0;return!1}}function B(e){return n=>n.name===e}function x(e){return n=>n.serviceIdentifier===e}function O(e,n){return i=>i.tags.has(e)&&i.tags.get(e)===n}function N(e){return void 0===e.name&&0===e.tags.size}function U(e){const n=C(e);return e=>!n(e)}function F(e){return n=>{const i=n.getAncestor();return void 0===i||!e(i)}}function k(e){return n=>{const i=n.getAncestor();return void 0!==i&&e(i)}}class j{#i;constructor(e){this.#i=e}getIdentifier(){return P(this.#i)}inRequestScope(){return this.#i.scope=s.Request,new E(this.#i)}inSingletonScope(){return this.#i.scope=s.Singleton,new E(this.#i)}inTransientScope(){return this.#i.scope=s.Transient,new E(this.#i)}}class D{#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 L(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 E(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 L(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 L(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 E(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 E(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&&!p.is(e)}(e)?{kind:!0===e.isMultiple?o.multipleInjection: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 T{#i;constructor(e){this.#i=e}getIdentifier(){return P(this.#i)}onActivation(e){return this.#i.onActivation=e,new V(this.#i)}onDeactivation(e){return this.#i.onDeactivation=e,new V(this.#i)}}class V{#i;constructor(e){this.#i=e}getIdentifier(){return P(this.#i)}when(e){return this.#i.isSatisfiedBy=e,new T(this.#i)}whenAnyAncestor(e){return this.when(C(e))}whenAnyAncestorIs(e){return this.when(C(x(e)))}whenAnyAncestorNamed(e){return this.when(function(e){return C(B(e))}(e))}whenAnyAncestorTagged(e,n){return this.when(function(e,n){return C(O(e,n))}(e,n))}whenDefault(){return this.when(N)}whenNamed(e){return this.when(B(e))}whenNoParent(e){return this.when(F(e))}whenNoParentIs(e){return this.when(F(x(e)))}whenNoParentNamed(e){return this.when(function(e){return F(B(e))}(e))}whenNoParentTagged(e,n){return this.when(function(e,n){return F(O(e,n))}(e,n))}whenParent(e){return this.when(k(e))}whenParentIs(e){return this.when(k(x(e)))}whenParentNamed(e){return this.when(function(e){return k(B(e))}(e))}whenParentTagged(e,n){return this.when(function(e,n){return k(O(e,n))}(e,n))}whenTagged(e,n){return this.when(O(e,n))}whenNoAncestor(e){return this.when(U(e))}whenNoAncestorIs(e){return this.when(U(x(e)))}whenNoAncestorNamed(e){return this.when(function(e){return U(B(e))}(e))}whenNoAncestorTagged(e,n){return this.when(function(e,n){return U(O(e,n))}(e,n))}}class E extends V{#c;constructor(e){super(e),this.#c=new T(e)}onActivation(e){return this.#c.onActivation(e)}onDeactivation(e){return this.#c.onDeactivation(e)}}class L extends E{#d;constructor(e){super(e),this.#d=new j(e)}inRequestScope(){return this.#d.inRequestScope()}inSingletonScope(){return this.#d.inSingletonScope()}inTransientScope(){return this.#d.inTransientScope()}}const q=Symbol.for("@inversifyjs/container/InversifyContainerError");class $ extends Error{[q];kind;constructor(e,n,i){super(n,i),this[q]=!0,this.kind=e}static is(e){return"object"==typeof e&&null!==e&&!0===e[q]}static isErrorOfKind(e,n){return $.is(e)&&e.kind===n}}var G;!function(e){e[e.invalidOperation=0]="invalidOperation"}(G||(G={}));class H{#l;#a;#u;constructor(e,n,i){this.#l=e,this.#a=n,this.#u=i}bind(e){return new D((e=>{this.#h(e)}),void 0,this.#a,e)}isBound(e,n){const i=this.#u.bindingService.get(e);return this.#v(e,i,n)}isCurrentBound(e,n){const i=this.#u.bindingService.getNonParentBindings(e);return this.#v(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.#g(e)}async unbindAll(){const e=[...this.#u.bindingService.getNonParentBoundServices()];await Promise.all(e.map((async e=>c(this.#l,e))));for(const n of e)this.#u.activationService.removeAllByServiceId(n),this.#u.bindingService.removeAllByServiceId(n),this.#u.deactivationService.removeAllByServiceId(n);this.#u.planResultCacheService.clearCache()}unbindSync(e){void 0!==this.#g(e)&&this.#f(e)}#h(e){this.#u.bindingService.set(e),this.#u.planResultCacheService.clearCache()}#f(e){let n;if(I(e)){const t=this.#u.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 "${S(r)}" binding. Consider using Container.unbind() instead.`}else n=`Unexpected asynchronous deactivation when unbinding "${S(e)}" service. Consider using Container.unbind() instead.`;var i;throw new $(G.invalidOperation,n)}#g(e){return I(e)?this.#b(e):this.#p(e)}#b(e){const n=this.#u.bindingService.getById(e.id),i=d(this.#l,n);if(void 0!==i)return i.then((()=>{this.#S(e)}));this.#S(e)}#S(e){this.#u.bindingService.removeById(e.id),this.#u.planResultCacheService.clearCache()}#p(e){const n=c(this.#l,e);if(void 0!==n)return n.then((()=>{this.#M(e)}));this.#M(e)}#M(e){this.#u.activationService.removeAllByServiceId(e),this.#u.bindingService.removeAllByServiceId(e),this.#u.deactivationService.removeAllByServiceId(e),this.#u.planResultCacheService.clearCache()}#v(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 _{#y;#l;#a;#u;constructor(e,n,i,t){this.#y=e,this.#l=n,this.#a=i,this.#u=t}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 $(G.invalidOperation,"Unexpected asynchronous module load. Consider using Container.load() instead.")}async unload(...e){await Promise.all(this.#R(...e)),this.#w(e)}unloadSync(...e){const n=this.#R(...e);for(const e of n)if(void 0!==e)throw new $(G.invalidOperation,"Unexpected asynchronous module unload. Consider using Container.unload() instead.");this.#w(e)}#m(e){return{bind:n=>new D((e=>{this.#h(e)}),e,this.#a,n),isBound:this.#y.isBound.bind(this.#y),onActivation:(n,i)=>{this.#u.activationService.add(i,{moduleId:e,serviceId:n})},onDeactivation:(n,i)=>{this.#u.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.#u.activationService.removeAllByModuleId(n.id),this.#u.bindingService.removeAllByModuleId(n.id),this.#u.deactivationService.removeAllByModuleId(n.id);this.#u.planResultCacheService.clearCache()}#n(...e){return e.map((e=>e.load(this.#m(e.id))))}#h(e){this.#u.bindingService.set(e),this.#u.planResultCacheService.clearCache()}#R(...e){return e.map((e=>l(this.#l,e.id)))}}class z{#I;#A;#P;#u;constructor(e,n,i){this.#u=n,this.#P=i,this.#I=this.#C(e),this.#A=this.#B()}register(e,n){const i=new n(e,this.#A);if(!0!==i[y])throw new $(G.invalidOperation,"Invalid plugin. The plugin must extend the Plugin class");i.load(this.#I)}#C(e){return{define:(n,i)=>{if(Object.prototype.hasOwnProperty.call(e,n))throw new $(G.invalidOperation,`Container already has a method named "${String(n)}"`);e[n]=i},onPlan:this.#P.onPlan.bind(this.#P)}}#B(){const e=this.#u;return{get activationService(){return e.activationService},get bindingService(){return e.bindingService},get deactivationService(){return e.deactivationService},get planResultCacheService(){return e.planResultCacheService}}}}class K{activationService;bindingService;deactivationService;planResultCacheService;#x;constructor(e,n,i,t){this.activationService=e,this.bindingService=n,this.deactivationService=i,this.planResultCacheService=t,this.#x=[]}reset(e,n,i){this.activationService=e,this.bindingService=n,this.deactivationService=i,this.planResultCacheService.clearCache();for(const e of this.#x)e()}onReset(e){this.#x.push(e)}}class X{#O;#a;#N;#U;#F;#k;#u;#j;constructor(e,n,i){this.#u=e,this.#F=this.#D(),this.#O=n,this.#a=i,this.#N=e=>this.#u.activationService.get(e),this.#U=this.#u.bindingService.get.bind(this.#u.bindingService),this.#k=[],this.#j=this.#h.bind(this),this.#u.onReset((()=>{this.#T()}))}get(e,n){const i=this.#V(!1,e,n),t=this.#E(i);if(M(t))throw new $(G.invalidOperation,`Unexpected asynchronous service when resolving service "${S(e)}"`);return t}getAll(e,n){const i=this.#V(!0,e,n),t=this.#E(i);if(M(t))throw new $(G.invalidOperation,`Unexpected asynchronous service when resolving service "${S(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.#k.push(e)}#T(){this.#U=this.#u.bindingService.get.bind(this.#u.bindingService),this.#j=this.#h.bind(this),this.#F=this.#D()}#L(e,n,i){return{isMultiple:e,name:i?.name,optional:i?.optional,serviceIdentifier:n,tag:i?.tag}}#q(e,n,i){const r={autobindOptions:i?.autobind??this.#O?{scope:this.#a}:void 0,getBindings:this.#U,getClassMetadata:t,rootConstraints:{isMultiple:n,serviceIdentifier:e},servicesBranch:[],setBinding:this.#j};return this.#$(r,i),r}#V(e,n,i){const t=this.#L(e,n,i),r=this.#u.planResultCacheService.get(t);if(void 0!==r)return r;const a=u(this.#q(n,e,i));this.#u.planResultCacheService.set(t,a);for(const e of this.#k)e(t,a);return a}#D(){return{get:this.get.bind(this),getAll:this.getAll.bind(this),getAllAsync:this.getAllAsync.bind(this),getAsync:this.getAsync.bind(this)}}#E(e){return h({context:this.#F,getActivations:this.#N,planResult:e,requestScopeCache:new Map})}#$(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}))}#h(e){this.#u.bindingService.set(e),this.#u.planResultCacheService.clearCache()}}class J{#u;#G;constructor(e){this.#u=e,this.#G=[]}restore(){const e=this.#G.pop();if(void 0===e)throw new $(G.invalidOperation,"No snapshot available to restore");this.#u.reset(e.activationService,e.bindingService,e.deactivationService)}snapshot(){this.#G.push({activationService:this.#u.activationService.clone(),bindingService:this.#u.bindingService.clone(),deactivationService:this.#u.deactivationService.clone()})}}const Q=s.Transient;class W{#y;#H;#_;#u;#P;#z;constructor(e){this.#u=this.#K(e);const n=e?.autobind??!1,i=e?.defaultScope??Q,r={getBindings:(a=this.#u).bindingService.get.bind(a.bindingService),getBindingsFromModule:a.bindingService.getByModuleId.bind(a.bindingService),getClassMetadata:t,getDeactivations:a.deactivationService.get.bind(a.deactivationService)};var a;this.#y=new H(r,i,this.#u),this.#H=new _(this.#y,r,i,this.#u),this.#P=new X(this.#u,n,i),this.#_=new z(this,this.#u,this.#P),this.#z=new J(this.#u)}bind(e){return this.#y.bind(e)}get(e,n){return this.#P.get(e,n)}getAll(e,n){return this.#P.getAll(e,n)}async getAllAsync(e,n){return this.#P.getAllAsync(e,n)}async getAsync(e,n){return this.#P.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.#H.load(...e)}loadSync(...e){this.#H.loadSync(...e)}onActivation(e,n){this.#u.activationService.add(n,{serviceId:e})}onDeactivation(e,n){this.#u.deactivationService.add(n,{serviceId:e})}register(e){this.#_.register(this,e)}restore(){this.#z.restore()}async rebind(e){return this.#y.rebind(e)}rebindSync(e){return this.#y.rebindSync(e)}snapshot(){this.#z.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.#H.unload(...e)}unloadSync(...e){this.#H.unloadSync(...e)}#K(e){if(void 0===e?.parent)return new K(v.build(void 0),g.build(void 0),f.build(void 0),new b);const n=new b;return e.parent.#u.planResultCacheService.subscribe(n),new K(v.build(e.parent.#u.activationService),g.build(e.parent.#u.bindingService),f.build(e.parent.#u.deactivationService),n)}}export{W as Container,w as ContainerModule,$ as InversifyContainerError,G as InversifyContainerErrorKind}; //# sourceMappingURL=index.js.map