@kaokei/di 5.0.5 → 5.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,7 @@ export declare const BINDING: {
27
27
  export declare const ERRORS: {
28
28
  readonly POST_CONSTRUCT: "Multiple @PostConstruct decorators are not allowed in a single class.";
29
29
  readonly PRE_DESTROY: "Multiple @PreDestroy decorators are not allowed in a single class.";
30
- readonly INVALID_TOKEN: "@Inject or @LazyInject requires a valid token, but received null or undefined.";
30
+ readonly INVALID_TOKEN: "@Inject requires a valid token, but received null or undefined.";
31
31
  readonly LAZY_INJECT_INVALID_TOKEN: "@LazyInject requires a valid token, but received null or undefined.";
32
32
  };
33
33
  export declare const UNINITIALIZED: unique symbol;
@@ -27,7 +27,7 @@ export declare const BINDING: {
27
27
  export declare const ERRORS: {
28
28
  readonly POST_CONSTRUCT: "Multiple @PostConstruct decorators are not allowed in a single class.";
29
29
  readonly PRE_DESTROY: "Multiple @PreDestroy decorators are not allowed in a single class.";
30
- readonly INVALID_TOKEN: "@Inject or @LazyInject requires a valid token, but received null or undefined.";
30
+ readonly INVALID_TOKEN: "@Inject requires a valid token, but received null or undefined.";
31
31
  readonly LAZY_INJECT_INVALID_TOKEN: "@LazyInject requires a valid token, but received null or undefined.";
32
32
  };
33
33
  export declare const UNINITIALIZED: unique symbol;
package/dist/index.cjs CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";var Y=Object.defineProperty;var J=(n,t,e)=>t in n?Y(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var r=(n,t,e)=>J(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l={INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},f={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},p={INVALID:"Invalid",INSTANCE:"Instance",CONSTANT:"ConstantValue",DYNAMIC:"DynamicValue"},C={POST_CONSTRUCT:"Multiple @PostConstruct decorators are not allowed in a single class.",PRE_DESTROY:"Multiple @PreDestroy decorators are not allowed in a single class.",INVALID_TOKEN:"@Inject or @LazyInject requires a valid token, but received null or undefined.",LAZY_INJECT_INVALID_TOKEN:"@LazyInject requires a valid token, but received null or undefined."},D=Symbol("UNINITIALIZED");function E(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function G(n){return n!==null&&typeof n=="object"}const O=new WeakMap,y=new WeakMap;function m(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function j(n,t){O.set(n,t),y.delete(n)}function V(n){const t=O.get(n);if(t)return t[l.POST_CONSTRUCT];if(m(n))return V(Object.getPrototypeOf(n))}function w(n){const t=O.get(n);if(t)return t[l.PRE_DESTROY];if(m(n))return w(Object.getPrototypeOf(n))}function R(n){if(y.has(n)){const e=y.get(n);return e&&Object.assign({},e)}const t=K(n);return y.set(n,t),t&&Object.assign({},t)}function K(n){const t=O.get(n),e=t&&E(t,l.INJECTED_PROPS)?t[l.INJECTED_PROPS]:void 0;if(!m(n))return e;const i=R(Object.getPrototypeOf(n));if(i||e)return Object.assign({},i,e)}class q{constructor(t){r(this,"name");this.name=t}}class L{constructor(t){r(this,"_callback");this._callback=t}resolve(){return this._callback()}}function A(n){if(!n)throw new Error(C.INVALID_TOKEN);return n instanceof L?n.resolve():n}class _ extends Error{constructor(e,i){const s=(i==null?void 0:i.name)||"<unknown token>";super(`${e}${s}`);r(this,"token");this.name=this.constructor.name,this.token=i}}function B(n){const t=[];let e=n;for(;e&&e.token;)t.push(e.token.name||"<anonymous>"),e=e.parent;return t.reverse()}class P extends _{constructor(t){super(""),this.message="Circular dependency found: "+B(t).join(" --> ")}}class k extends _{constructor(t){super("Binding is not configured (missing .to() / .toSelf() / .toConstantValue() / .toDynamicValue()): ",t)}}class M extends P{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const T=class T{constructor(t,e){r(this,"container");r(this,"context");r(this,"token");r(this,"type",p.INVALID);r(this,"status",f.DEFAULT);r(this,"classValue");r(this,"constantValue");r(this,"dynamicValue");r(this,"cache");r(this,"postConstructResult",D);r(this,"transient",!1);r(this,"onActivationHandler");r(this,"onDeactivationHandler");this.container=e,this.context={container:this.container},this.token=t}onActivation(t){this.onActivationHandler=t}onDeactivation(t){this.onDeactivationHandler=t}activate(t){const e=this.onActivationHandler?this.onActivationHandler(this.context,t):t;return this.container.activate(e,this.token)}deactivate(){this.onDeactivationHandler&&this.onDeactivationHandler(this.cache)}to(t){return this.type=p.INSTANCE,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=p.CONSTANT,this.constantValue=t,this}toDynamicValue(t){return this.type=p.DYNAMIC,this.dynamicValue=t,this}inTransientScope(){return this.transient=!0,this}toService(t){return this.toDynamicValue(e=>e.container._resolveWithInternalOpts(t,{parent:{token:this.token}}))}get(t){if(f.INITING===this.status)throw new P(t);if(f.ACTIVATED===this.status)if(this.transient)this.status=f.DEFAULT;else return this.cache;const e=T._resolvers.get(this.type);if(e)return e.call(this,t);throw new k(this.token)}_getAwaitBindings(t,e){return e===!0?t:Array.isArray(e)?t.filter(i=>e.includes(i.token)):typeof e=="function"?t.filter(e):[]}_postConstruct(t,e){if(p.INSTANCE===this.type){const{key:i,value:s}=V(this.classValue)||{};if(i)if(s){const o=e.filter(u=>p.INSTANCE===(u==null?void 0:u.type)),a=this._getAwaitBindings(o,s);for(const u of a)if(u&&u.postConstructResult===D)throw new M({token:u.token,parent:t});const c=a.map(u=>u.postConstructResult);this.postConstructResult=Promise.all(c).then(()=>this._execute(i))}else this.postConstructResult=this._execute(i);else this.postConstructResult=void 0}}preDestroy(){if(p.INSTANCE===this.type&&this.cache!==void 0){const{key:t}=w(this.classValue)||{};t&&this._execute(t)}this.container&&!this.container._destroyed&&I._instanceContainerMap.delete(this.cache),this.container=void 0,this.context=void 0,this.classValue=void 0,this.constantValue=void 0,this.dynamicValue=void 0,this.cache=void 0,this.postConstructResult=D,this.onActivationHandler=void 0,this.onDeactivationHandler=void 0}_execute(t){const e=this.cache[t];return e==null?void 0:e.call(this.cache)}_resolveInstanceValue(t){this.status=f.INITING;const e=this._createInstance();this.cache=this.activate(e),this.status=f.ACTIVATED,this._registerInstance();const{properties:i,bindings:s}=this._getInjectProperties(t);return this._injectProperties(i),this._postConstruct(t,s),this.cache}_createInstance(){const t=this.classValue;return new t}_registerInstance(){I._instanceContainerMap.set(this.cache,this.container)}_injectProperties(t){Object.assign(this.cache,t)}_resolveConstantValue(){return this.status=f.INITING,this.cache=this.activate(this.constantValue),this.status=f.ACTIVATED,this.cache}_resolveDynamicValue(){this.status=f.INITING;const t=this.dynamicValue(this.context);return this.cache=this.activate(t),this.status=f.ACTIVATED,this.cache}_getInjectProperties(t){const e=R(this.classValue)||{},i=Object.keys(e),s=Object.create(null),o=[];for(let a=0;a<i.length;a++){const c=i[a],u=e[c],h=Object.assign({},u);h.parent=t;const d=this.container._resolveWithInternalOpts(A(h.inject),h);d===void 0&&h.optional||(s[c]=d),o.push(h.binding)}return{properties:s,bindings:o}}};r(T,"_resolvers",new Map([[p.INSTANCE,function(t){return this._resolveInstanceValue(t)}],[p.CONSTANT,function(t){return this._resolveConstantValue()}],[p.DYNAMIC,function(t){return this._resolveDynamicValue()}]]));let N=T;class H extends _{constructor(t,e){if(super("No matching binding found for token: ",t),e!=null&&e.parent){const i=B(e.parent);i.length>0&&(this.message+=`
1
+ "use strict";var Y=Object.defineProperty;var J=(n,t,e)=>t in n?Y(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var r=(n,t,e)=>J(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l={INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},f={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},p={INVALID:"Invalid",INSTANCE:"Instance",CONSTANT:"ConstantValue",DYNAMIC:"DynamicValue"},C={POST_CONSTRUCT:"Multiple @PostConstruct decorators are not allowed in a single class.",PRE_DESTROY:"Multiple @PreDestroy decorators are not allowed in a single class.",INVALID_TOKEN:"@Inject requires a valid token, but received null or undefined.",LAZY_INJECT_INVALID_TOKEN:"@LazyInject requires a valid token, but received null or undefined."},D=Symbol("UNINITIALIZED");function E(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function G(n){return n!==null&&typeof n=="object"}const O=new WeakMap,y=new WeakMap;function A(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function j(n,t){O.set(n,t),y.delete(n)}function w(n){const t=O.get(n);if(t)return t[l.POST_CONSTRUCT];if(A(n))return w(Object.getPrototypeOf(n))}function V(n){const t=O.get(n);if(t)return t[l.PRE_DESTROY];if(A(n))return V(Object.getPrototypeOf(n))}function R(n){if(y.has(n)){const e=y.get(n);return e&&Object.assign({},e)}const t=K(n);return y.set(n,t),t&&Object.assign({},t)}function K(n){const t=O.get(n),e=t&&E(t,l.INJECTED_PROPS)?t[l.INJECTED_PROPS]:void 0;if(!A(n))return e;const i=R(Object.getPrototypeOf(n));if(i||e)return Object.assign({},i,e)}class ${constructor(t){r(this,"name");this.name=t}}class L{constructor(t){r(this,"_callback");this._callback=t}resolve(){return this._callback()}}function m(n){if(!n)throw new Error(C.INVALID_TOKEN);return n instanceof L?n.resolve():n}class _ extends Error{constructor(e,i){const s=(i==null?void 0:i.name)||"<unknown token>";super(`${e}${s}`);r(this,"token");this.name=this.constructor.name,this.token=i}}function B(n){const t=[];let e=n;for(;e&&e.token;)t.push(e.token.name||"<anonymous>"),e=e.parent;return t.reverse()}class P extends _{constructor(t){super(""),this.message="Circular dependency found: "+B(t).join(" --> ")}}class M extends _{constructor(t){super(`Binding is incomplete: container.bind(${t==null?void 0:t.name}) was called but missing to/toSelf/toConstantValue/toDynamicValue method. `,t)}}class k extends P{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const T=class T{constructor(t,e){r(this,"container");r(this,"context");r(this,"token");r(this,"type",p.INVALID);r(this,"status",f.DEFAULT);r(this,"classValue");r(this,"constantValue");r(this,"dynamicValue");r(this,"cache");r(this,"postConstructResult",D);r(this,"transient",!1);r(this,"onActivationHandler");r(this,"onDeactivationHandler");this.container=e,this.context={container:this.container},this.token=t}onActivation(t){this.onActivationHandler=t}onDeactivation(t){this.onDeactivationHandler=t}activate(t){const e=this.onActivationHandler?this.onActivationHandler(this.context,t):t;return this.container.activate(e,this.token)}deactivate(){this.onDeactivationHandler&&this.onDeactivationHandler(this.cache)}to(t){return this.type=p.INSTANCE,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=p.CONSTANT,this.constantValue=t,this}toDynamicValue(t){return this.type=p.DYNAMIC,this.dynamicValue=t,this}inTransientScope(){return this.transient=!0,this}toService(t){return this.toDynamicValue(e=>e.container._resolveWithInternalOpts(t,{parent:{token:this.token}}))}get(t){if(f.INITING===this.status)throw new P(t);if(f.ACTIVATED===this.status)if(this.transient)this.status=f.DEFAULT;else return this.cache;const e=T._resolvers.get(this.type);if(e)return e.call(this,t);throw new M(this.token)}_getAwaitBindings(t,e){return e===!0?t:Array.isArray(e)?t.filter(i=>e.includes(i.token)):typeof e=="function"?t.filter(e):[]}_postConstruct(t,e){if(p.INSTANCE===this.type){const{key:i,value:s}=w(this.classValue)||{};if(i)if(s){const o=e.filter(u=>p.INSTANCE===(u==null?void 0:u.type)),a=this._getAwaitBindings(o,s);for(const u of a)if(u&&u.postConstructResult===D)throw new k({token:u.token,parent:t});const c=a.map(u=>u.postConstructResult);this.postConstructResult=Promise.all(c).then(()=>this._execute(i))}else this.postConstructResult=this._execute(i);else this.postConstructResult=void 0}}preDestroy(){if(p.INSTANCE===this.type&&this.cache!==void 0){const{key:t}=V(this.classValue)||{};t&&this._execute(t)}this.container&&!this.container._destroyed&&I._instanceContainerMap.delete(this.cache),this.container=void 0,this.context=void 0,this.classValue=void 0,this.constantValue=void 0,this.dynamicValue=void 0,this.cache=void 0,this.postConstructResult=D,this.onActivationHandler=void 0,this.onDeactivationHandler=void 0}_execute(t){const e=this.cache[t];return e==null?void 0:e.call(this.cache)}_resolveInstanceValue(t){this.status=f.INITING;const e=this._createInstance();this.cache=this.activate(e),this.status=f.ACTIVATED,this._registerInstance();const{properties:i,bindings:s}=this._getInjectProperties(t);return this._injectProperties(i),this._postConstruct(t,s),this.cache}_createInstance(){const t=this.classValue;return new t}_registerInstance(){I._instanceContainerMap.set(this.cache,this.container)}_injectProperties(t){Object.assign(this.cache,t)}_resolveConstantValue(){return this.status=f.INITING,this.cache=this.activate(this.constantValue),this.status=f.ACTIVATED,this.cache}_resolveDynamicValue(){this.status=f.INITING;const t=this.dynamicValue(this.context);return this.cache=this.activate(t),this.status=f.ACTIVATED,this.cache}_getInjectProperties(t){const e=R(this.classValue)||{},i=Object.keys(e),s=Object.create(null),o=[];for(let a=0;a<i.length;a++){const c=i[a],u=e[c],h=Object.assign({},u);h.parent=t;const d=this.container._resolveWithInternalOpts(m(h.inject),h);d===void 0&&h.optional||(s[c]=d),o.push(h.binding)}return{properties:s,bindings:o}}};r(T,"_resolvers",new Map([[p.INSTANCE,function(t){return this._resolveInstanceValue(t)}],[p.CONSTANT,function(t){return this._resolveConstantValue()}],[p.DYNAMIC,function(t){return this._resolveDynamicValue()}]]));let N=T;class H extends _{constructor(t,e){if(super("No matching binding found for token: ",t),e!=null&&e.parent){const i=B(e.parent);i.length>0&&(this.message+=`
2
2
  `+i.map(s=>" required by: "+s).join(`
3
- `))}}}class F extends _{constructor(t){super("Cannot bind token multiple times: ",t)}}class x extends _{constructor(t){super("Container has been destroyed. Cannot call get() for token: ",t)}}const v=class v{constructor(){r(this,"parent");r(this,"children");r(this,"_bindings",new Map);r(this,"_destroyed",!1);r(this,"_onActivationHandler");r(this,"_onDeactivationHandler")}static getContainerOf(t){return v._instanceContainerMap.get(t)}bind(t){if(this._bindings.has(t))throw new F(t);const e=this._buildBinding(t);return this._bindings.set(t,e),e}unbind(t){if(this._bindings.has(t)){const e=this._getBinding(t);this.deactivate(e),e.deactivate(),e.preDestroy(),this._bindings.delete(t)}}tryGet(t){return this.get(t,{optional:!0})}rebind(t){return this._bindings.has(t)&&this.unbind(t),this.bind(t)}unbindAll(){const t=Array.from(this._bindings.keys());for(const e of t)this.unbind(e)}isCurrentBound(t){return this._bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new v;return t.parent=this,this.children||(this.children=new Set),this.children.add(t),t}destroy(){var t,e;if(this._destroyed=!0,this.children){const i=Array.from(this.children);for(const s of i)s.destroy()}this.unbindAll(),this._bindings.clear(),(e=(t=this.parent)==null?void 0:t.children)==null||e.delete(this),this.parent=void 0,this.children=void 0,this._onActivationHandler=void 0,this._onDeactivationHandler=void 0}get(t,e={}){const i=Object.assign({},e);return this._resolveWithInternalOpts(t,i)}getAsync(t,e={}){const i=Object.assign({},e);let s;try{s=this._resolveWithInternalOpts(t,i)}catch(a){return Promise.reject(a)}const o=i.binding;return(o==null?void 0:o.postConstructResult)instanceof Promise?o.postConstructResult.then(()=>s):Promise.resolve(s)}_resolveWithInternalOpts(t,e){if(this._destroyed)throw new x(t);return e.skipSelf?this._resolveSkipSelf(t,e):e.self?this._resolveSelf(t,e):this._resolveDefault(t,e)}_resolveSkipSelf(t,e){if(this.parent){const i=Object.assign({},e,{skipSelf:!1});return this.parent._resolveWithInternalOpts(t,i)}return this._checkBindingNotFoundError(t,e)}_resolveSelf(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this._checkBindingNotFoundError(t,e)}_resolveDefault(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this.parent?this.parent._resolveWithInternalOpts(t,e):this._checkBindingNotFoundError(t,e)}onActivation(t){this._onActivationHandler=t}onDeactivation(t){this._onDeactivationHandler=t}activate(t,e){return this._onActivationHandler?this._onActivationHandler({container:this},t,e):t}deactivate(t){this._onDeactivationHandler&&this._onDeactivationHandler(t.cache,t.token)}_buildBinding(t){return new N(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new H(t,e)}};r(v,"_instanceContainerMap",new WeakMap);let I=v;class z extends _{constructor(t,e){super(`@LazyInject(${t==null?void 0:t.name}) in class ${e.name} requires a registered container but none was found. Token: `,t)}}function b(n,t){return function(e){return function(i,s){const o=s.name,a=s.metadata;E(a,l.INJECTED_PROPS)||(a[l.INJECTED_PROPS]={});const c=a[l.INJECTED_PROPS];c[o]||(c[o]={}),c[o][n]=e===void 0?t:e}}}function W(n,t){return e=>(i,s)=>{const o=s.name,a=s.metadata;if(E(a,n))throw new Error(t);a[n]={key:o,value:e}}}const Z=b(l.INJECT),$=b(l.SELF,!0),Q=b(l.SKIP_SELF,!0),X=b(l.OPTIONAL,!0),tt=W(l.POST_CONSTRUCT,C.POST_CONSTRUCT),et=W(l.PRE_DESTROY,C.PRE_DESTROY);function nt(){return function(n,t){const e=t.metadata;j(n,e)}}function it(n,t,e,i){if(e==null)throw new Error(C.LAZY_INJECT_INVALID_TOKEN);let s,o=!1;Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get(){if(!o){const a=i||I.getContainerOf(n),c=n.constructor;if(!a)throw new z(A(e),c);s=a._resolveWithInternalOpts(A(e),{parent:{token:c}}),o=!0}return s},set(a){s=a,o=!0}})}function U(n,t){return function(e,i){const s=i.name;i.addInitializer(function(){it(this,s,n,t)})}}function st(n){return function(t){return U(t,n)}}function rt(n,t){const e=t.name;t.addInitializer(function(){this[e]=n.bind(this)})}const S=new WeakMap;function ot(n,t,e){const i=Array.isArray(n)?n:[n],s=t.prototype,o=typeof s[e]=="function",a=[];S.has(t)||S.set(t,{});const c=S.get(t),u={kind:o?"method":"field",name:e,static:!1,private:!1,addInitializer(d){a.push(d)},metadata:c};let h=o?s[e]:void 0;for(let d=i.length-1;d>=0;d--){const g=i[d](h,u);o&&typeof g=="function"&&(h=g)}if(o&&h!==s[e]&&(s[e]=h),j(t,c),a.length>0){const d=Object.create(s);for(const g of a)g.call(d)}}exports.BaseError=_;exports.Binding=N;exports.BindingNotFoundError=H;exports.BindingNotValidError=k;exports.CircularDependencyError=P;exports.Container=I;exports.ContainerDestroyedError=x;exports.ContainerNotFoundError=z;exports.DuplicateBindingError=F;exports.ERRORS=C;exports.Inject=Z;exports.Injectable=nt;exports.LazyInject=U;exports.LazyToken=L;exports.Optional=X;exports.PostConstruct=tt;exports.PostConstructError=M;exports.PreDestroy=et;exports.Self=$;exports.SkipSelf=Q;exports.Token=q;exports.autobind=rt;exports.createLazyInject=st;exports.decorate=ot;exports.hasOwn=E;exports.isObject=G;
3
+ `))}}}class F extends _{constructor(t){super("Cannot bind token multiple times: ",t)}}class x extends _{constructor(t){super("Container has been destroyed. Cannot call get() for token: ",t)}}const v=class v{constructor(){r(this,"parent");r(this,"children");r(this,"_bindings",new Map);r(this,"_destroyed",!1);r(this,"_onActivationHandler");r(this,"_onDeactivationHandler")}static getContainerOf(t){return v._instanceContainerMap.get(t)}bind(t){if(this._bindings.has(t))throw new F(t);const e=this._buildBinding(t);return this._bindings.set(t,e),e}unbind(t){if(this._bindings.has(t)){const e=this._getBinding(t);this.deactivate(e),e.deactivate(),e.preDestroy(),this._bindings.delete(t)}}tryGet(t){return this.get(t,{optional:!0})}rebind(t){return this._bindings.has(t)&&this.unbind(t),this.bind(t)}unbindAll(){const t=Array.from(this._bindings.keys());for(const e of t)this.unbind(e)}isCurrentBound(t){return this._bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new v;return t.parent=this,this.children||(this.children=new Set),this.children.add(t),t}destroy(){var t,e;if(this._destroyed=!0,this.children){const i=Array.from(this.children);for(const s of i)s.destroy()}this.unbindAll(),this._bindings.clear(),(e=(t=this.parent)==null?void 0:t.children)==null||e.delete(this),this.parent=void 0,this.children=void 0,this._onActivationHandler=void 0,this._onDeactivationHandler=void 0}get(t,e={}){const i=Object.assign({},e);return this._resolveWithInternalOpts(t,i)}getAsync(t,e={}){const i=Object.assign({},e);let s;try{s=this._resolveWithInternalOpts(t,i)}catch(a){return Promise.reject(a)}const o=i.binding;return(o==null?void 0:o.postConstructResult)instanceof Promise?o.postConstructResult.then(()=>s):Promise.resolve(s)}_resolveWithInternalOpts(t,e){if(this._destroyed)throw new x(t);return e.skipSelf?this._resolveSkipSelf(t,e):e.self?this._resolveSelf(t,e):this._resolveDefault(t,e)}_resolveSkipSelf(t,e){if(this.parent){const i=Object.assign({},e,{skipSelf:!1});return this.parent._resolveWithInternalOpts(t,i)}return this._checkBindingNotFoundError(t,e)}_resolveSelf(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this._checkBindingNotFoundError(t,e)}_resolveDefault(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this.parent?this.parent._resolveWithInternalOpts(t,e):this._checkBindingNotFoundError(t,e)}onActivation(t){this._onActivationHandler=t}onDeactivation(t){this._onDeactivationHandler=t}activate(t,e){return this._onActivationHandler?this._onActivationHandler({container:this},t,e):t}deactivate(t){this._onDeactivationHandler&&this._onDeactivationHandler(t.cache,t.token)}_buildBinding(t){return new N(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new H(t,e)}};r(v,"_instanceContainerMap",new WeakMap);let I=v;class z extends _{constructor(t,e){super(`@LazyInject(${t==null?void 0:t.name}) in class ${e.name} requires a registered container but none was found. Token: `,t)}}function b(n,t){return function(e){return function(i,s){const o=s.name,a=s.metadata;E(a,l.INJECTED_PROPS)||(a[l.INJECTED_PROPS]={});const c=a[l.INJECTED_PROPS];c[o]||(c[o]={}),c[o][n]=e===void 0?t:e}}}function W(n,t){return e=>(i,s)=>{const o=s.name,a=s.metadata;if(E(a,n))throw new Error(t);a[n]={key:o,value:e}}}const q=b(l.INJECT),Z=b(l.SELF,!0),Q=b(l.SKIP_SELF,!0),X=b(l.OPTIONAL,!0),tt=W(l.POST_CONSTRUCT,C.POST_CONSTRUCT),et=W(l.PRE_DESTROY,C.PRE_DESTROY);function nt(){return function(n,t){const e=t.metadata;j(n,e)}}function it(n,t,e,i){if(e==null)throw new Error(C.LAZY_INJECT_INVALID_TOKEN);let s,o=!1;Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get(){if(!o){const a=i||I.getContainerOf(n),c=n.constructor;if(!a)throw new z(m(e),c);s=a._resolveWithInternalOpts(m(e),{parent:{token:c}}),o=!0}return s},set(a){s=a,o=!0}})}function U(n,t){return function(e,i){const s=i.name;i.addInitializer(function(){it(this,s,n,t)})}}function st(n){return function(t){return U(t,n)}}function rt(n,t){const e=t.name;t.addInitializer(function(){this[e]=n.bind(this)})}const S=new WeakMap;function ot(n,t,e){const i=Array.isArray(n)?n:[n],s=t.prototype,o=typeof s[e]=="function",a=[];S.has(t)||S.set(t,{});const c=S.get(t),u={kind:o?"method":"field",name:e,static:!1,private:!1,addInitializer(d){a.push(d)},metadata:c};let h=o?s[e]:void 0;for(let d=i.length-1;d>=0;d--){const g=i[d](h,u);o&&typeof g=="function"&&(h=g)}if(o&&h!==s[e]&&(s[e]=h),j(t,c),a.length>0){const d=Object.create(s);for(const g of a)g.call(d)}}exports.BaseError=_;exports.Binding=N;exports.BindingNotFoundError=H;exports.BindingNotValidError=M;exports.CircularDependencyError=P;exports.Container=I;exports.ContainerDestroyedError=x;exports.ContainerNotFoundError=z;exports.DuplicateBindingError=F;exports.ERRORS=C;exports.Inject=q;exports.Injectable=nt;exports.LazyInject=U;exports.LazyToken=L;exports.Optional=X;exports.PostConstruct=tt;exports.PostConstructError=k;exports.PreDestroy=et;exports.Self=Z;exports.SkipSelf=Q;exports.Token=$;exports.autobind=rt;exports.createLazyInject=st;exports.decorate=ot;exports.hasOwn=E;exports.isObject=G;
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- var B = Object.defineProperty;
2
- var M = (n, t, e) => t in n ? B(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
- var r = (n, t, e) => M(n, typeof t != "symbol" ? t + "" : t, e);
1
+ var M = Object.defineProperty;
2
+ var H = (n, t, e) => t in n ? M(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
+ var r = (n, t, e) => H(n, typeof t != "symbol" ? t + "" : t, e);
4
4
  const l = {
5
5
  // 记录实例属性装饰器对应的数据的键
6
6
  INJECTED_PROPS: "injected:props",
@@ -30,14 +30,14 @@ const l = {
30
30
  POST_CONSTRUCT: "Multiple @PostConstruct decorators are not allowed in a single class.",
31
31
  PRE_DESTROY: "Multiple @PreDestroy decorators are not allowed in a single class.",
32
32
  // 用于 token.ts 的 resolveToken —— 无效 token
33
- INVALID_TOKEN: "@Inject or @LazyInject requires a valid token, but received null or undefined.",
33
+ INVALID_TOKEN: "@Inject requires a valid token, but received null or undefined.",
34
34
  // 用于 decorator.ts 的 defineLazyProperty —— 无效 token
35
35
  LAZY_INJECT_INVALID_TOKEN: "@LazyInject requires a valid token, but received null or undefined."
36
36
  }, E = Symbol("UNINITIALIZED");
37
- function m(n, t) {
37
+ function A(n, t) {
38
38
  return Object.prototype.hasOwnProperty.call(n, t);
39
39
  }
40
- function Z(n) {
40
+ function q(n) {
41
41
  return n !== null && typeof n == "object";
42
42
  }
43
43
  const y = /* @__PURE__ */ new WeakMap(), g = /* @__PURE__ */ new WeakMap();
@@ -47,37 +47,37 @@ function S(n) {
47
47
  function P(n, t) {
48
48
  y.set(n, t), g.delete(n);
49
49
  }
50
- function V(n) {
50
+ function w(n) {
51
51
  const t = y.get(n);
52
52
  if (t)
53
53
  return t[l.POST_CONSTRUCT];
54
54
  if (S(n))
55
- return V(Object.getPrototypeOf(n));
55
+ return w(Object.getPrototypeOf(n));
56
56
  }
57
- function w(n) {
57
+ function V(n) {
58
58
  const t = y.get(n);
59
59
  if (t)
60
60
  return t[l.PRE_DESTROY];
61
61
  if (S(n))
62
- return w(Object.getPrototypeOf(n));
62
+ return V(Object.getPrototypeOf(n));
63
63
  }
64
64
  function j(n) {
65
65
  if (g.has(n)) {
66
66
  const e = g.get(n);
67
67
  return e && Object.assign({}, e);
68
68
  }
69
- const t = H(n);
69
+ const t = k(n);
70
70
  return g.set(n, t), t && Object.assign({}, t);
71
71
  }
72
- function H(n) {
73
- const t = y.get(n), e = t && m(t, l.INJECTED_PROPS) ? t[l.INJECTED_PROPS] : void 0;
72
+ function k(n) {
73
+ const t = y.get(n), e = t && A(t, l.INJECTED_PROPS) ? t[l.INJECTED_PROPS] : void 0;
74
74
  if (!S(n))
75
75
  return e;
76
76
  const i = j(Object.getPrototypeOf(n));
77
77
  if (i || e)
78
78
  return Object.assign({}, i, e);
79
79
  }
80
- class $ {
80
+ class Z {
81
81
  constructor(t) {
82
82
  // 仅类型层面存在,无运行时开销
83
83
  r(this, "name");
@@ -121,7 +121,7 @@ class L extends _ {
121
121
  class F extends _ {
122
122
  constructor(t) {
123
123
  super(
124
- "Binding is not configured (missing .to() / .toSelf() / .toConstantValue() / .toDynamicValue()): ",
124
+ `Binding is incomplete: container.bind(${t == null ? void 0 : t.name}) was called but missing to/toSelf/toConstantValue/toDynamicValue method. `,
125
125
  t
126
126
  );
127
127
  }
@@ -213,7 +213,7 @@ const N = class N {
213
213
  */
214
214
  _postConstruct(t, e) {
215
215
  if (p.INSTANCE === this.type) {
216
- const { key: i, value: s } = V(this.classValue) || {};
216
+ const { key: i, value: s } = w(this.classValue) || {};
217
217
  if (i)
218
218
  if (s) {
219
219
  const o = e.filter(
@@ -237,7 +237,7 @@ const N = class N {
237
237
  }
238
238
  preDestroy() {
239
239
  if (p.INSTANCE === this.type && this.cache !== void 0) {
240
- const { key: t } = w(this.classValue) || {};
240
+ const { key: t } = V(this.classValue) || {};
241
241
  t && this._execute(t);
242
242
  }
243
243
  this.container && !this.container._destroyed && v._instanceContainerMap.delete(this.cache), this.container = void 0, this.context = void 0, this.classValue = void 0, this.constantValue = void 0, this.dynamicValue = void 0, this.cache = void 0, this.postConstructResult = E, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
@@ -300,8 +300,8 @@ r(N, "_resolvers", /* @__PURE__ */ new Map([
300
300
  return this._resolveDynamicValue();
301
301
  }]
302
302
  ]));
303
- let A = N;
304
- class z extends _ {
303
+ let m = N;
304
+ class U extends _ {
305
305
  constructor(t, e) {
306
306
  if (super("No matching binding found for token: ", t), e != null && e.parent) {
307
307
  const i = R(e.parent);
@@ -311,12 +311,12 @@ class z extends _ {
311
311
  }
312
312
  }
313
313
  }
314
- class U extends _ {
314
+ class Y extends _ {
315
315
  constructor(t) {
316
316
  super("Cannot bind token multiple times: ", t);
317
317
  }
318
318
  }
319
- class Y extends _ {
319
+ class z extends _ {
320
320
  constructor(t) {
321
321
  super("Container has been destroyed. Cannot call get() for token: ", t);
322
322
  }
@@ -336,7 +336,7 @@ const I = class I {
336
336
  }
337
337
  bind(t) {
338
338
  if (this._bindings.has(t))
339
- throw new U(t);
339
+ throw new Y(t);
340
340
  const e = this._buildBinding(t);
341
341
  return this._bindings.set(t, e), e;
342
342
  }
@@ -394,7 +394,7 @@ const I = class I {
394
394
  // 内部解析入口,接受完整 Options;被 getAsync、toService、_getInjectProperties 等内部路径调用
395
395
  _resolveWithInternalOpts(t, e) {
396
396
  if (this._destroyed)
397
- throw new Y(t);
397
+ throw new z(t);
398
398
  return e.skipSelf ? this._resolveSkipSelf(t, e) : e.self ? this._resolveSelf(t, e) : this._resolveDefault(t, e);
399
399
  }
400
400
  // 处理 skipSelf 选项:跳过当前容器,委托父容器解析
@@ -428,14 +428,14 @@ const I = class I {
428
428
  this._onDeactivationHandler && this._onDeactivationHandler(t.cache, t.token);
429
429
  }
430
430
  _buildBinding(t) {
431
- return new A(t, this);
431
+ return new m(t, this);
432
432
  }
433
433
  _getBinding(t) {
434
434
  return this._bindings.get(t);
435
435
  }
436
436
  _checkBindingNotFoundError(t, e) {
437
437
  if (!e.optional)
438
- throw new z(t, e);
438
+ throw new U(t, e);
439
439
  }
440
440
  };
441
441
  // 实例到容器的映射表,用于 @LazyInject 查找实例所属容器
@@ -458,24 +458,24 @@ function O(n, t) {
458
458
  return function(e) {
459
459
  return function(i, s) {
460
460
  const o = s.name, a = s.metadata;
461
- m(a, l.INJECTED_PROPS) || (a[l.INJECTED_PROPS] = {});
461
+ A(a, l.INJECTED_PROPS) || (a[l.INJECTED_PROPS] = {});
462
462
  const c = a[l.INJECTED_PROPS];
463
463
  c[o] || (c[o] = {}), c[o][n] = e === void 0 ? t : e;
464
464
  };
465
465
  };
466
466
  }
467
- function k(n, t) {
467
+ function B(n, t) {
468
468
  return (e) => (i, s) => {
469
469
  const o = s.name, a = s.metadata;
470
- if (m(a, n))
470
+ if (A(a, n))
471
471
  throw new Error(t);
472
472
  a[n] = { key: o, value: e };
473
473
  };
474
474
  }
475
- const Q = O(l.INJECT), X = O(l.SELF, !0), tt = O(l.SKIP_SELF, !0), et = O(l.OPTIONAL, !0), nt = k(
475
+ const Q = O(l.INJECT), X = O(l.SELF, !0), tt = O(l.SKIP_SELF, !0), et = O(l.OPTIONAL, !0), nt = B(
476
476
  l.POST_CONSTRUCT,
477
477
  T.POST_CONSTRUCT
478
- ), it = k(
478
+ ), it = B(
479
479
  l.PRE_DESTROY,
480
480
  T.PRE_DESTROY
481
481
  );
@@ -554,14 +554,14 @@ function at(n, t, e) {
554
554
  }
555
555
  export {
556
556
  _ as BaseError,
557
- A as Binding,
558
- z as BindingNotFoundError,
557
+ m as Binding,
558
+ U as BindingNotFoundError,
559
559
  F as BindingNotValidError,
560
560
  L as CircularDependencyError,
561
561
  v as Container,
562
- Y as ContainerDestroyedError,
562
+ z as ContainerDestroyedError,
563
563
  J as ContainerNotFoundError,
564
- U as DuplicateBindingError,
564
+ Y as DuplicateBindingError,
565
565
  T as ERRORS,
566
566
  Q as Inject,
567
567
  st as Injectable,
@@ -573,10 +573,10 @@ export {
573
573
  it as PreDestroy,
574
574
  X as Self,
575
575
  tt as SkipSelf,
576
- $ as Token,
576
+ Z as Token,
577
577
  ot as autobind,
578
578
  rt as createLazyInject,
579
579
  at as decorate,
580
- m as hasOwn,
581
- Z as isObject
580
+ A as hasOwn,
581
+ q as isObject
582
582
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaokei/di",
3
- "version": "5.0.5",
3
+ "version": "5.0.6",
4
4
  "type": "module",
5
5
  "description": "Tiny di library depends on typescript decorator.",
6
6
  "main": "./dist/index.cjs",