@kaokei/di 5.0.2 → 5.0.4
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.
- package/README.md +5 -0
- package/dist/constants.d.cts +4 -3
- package/dist/constants.d.ts +4 -3
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +128 -119
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/constants.d.cts
CHANGED
|
@@ -30,12 +30,13 @@ export declare const ERRORS: {
|
|
|
30
30
|
readonly INVALID_TOKEN: "@Inject or @LazyInject 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
|
+
export declare const UNINITIALIZED: unique symbol;
|
|
34
|
+
export type BindingType = (typeof BINDING)[keyof typeof BINDING];
|
|
35
|
+
export type StatusType = (typeof STATUS)[keyof typeof STATUS];
|
|
33
36
|
/**
|
|
34
37
|
* Object.hasOwn 的兼容性替代函数
|
|
35
38
|
* Object.hasOwn 在部分旧版浏览器和运行时中不可用(ES2022+),
|
|
36
39
|
* 此函数使用 Object.prototype.hasOwnProperty.call 实现相同语义。
|
|
37
40
|
*/
|
|
38
41
|
export declare function hasOwn(obj: object, key: PropertyKey): boolean;
|
|
39
|
-
export declare
|
|
40
|
-
export type BindingType = (typeof BINDING)[keyof typeof BINDING];
|
|
41
|
-
export type StatusType = (typeof STATUS)[keyof typeof STATUS];
|
|
42
|
+
export declare function isObject(val: unknown): val is object;
|
package/dist/constants.d.ts
CHANGED
|
@@ -30,12 +30,13 @@ export declare const ERRORS: {
|
|
|
30
30
|
readonly INVALID_TOKEN: "@Inject or @LazyInject 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
|
+
export declare const UNINITIALIZED: unique symbol;
|
|
34
|
+
export type BindingType = (typeof BINDING)[keyof typeof BINDING];
|
|
35
|
+
export type StatusType = (typeof STATUS)[keyof typeof STATUS];
|
|
33
36
|
/**
|
|
34
37
|
* Object.hasOwn 的兼容性替代函数
|
|
35
38
|
* Object.hasOwn 在部分旧版浏览器和运行时中不可用(ES2022+),
|
|
36
39
|
* 此函数使用 Object.prototype.hasOwnProperty.call 实现相同语义。
|
|
37
40
|
*/
|
|
38
41
|
export declare function hasOwn(obj: object, key: PropertyKey): boolean;
|
|
39
|
-
export declare
|
|
40
|
-
export type BindingType = (typeof BINDING)[keyof typeof BINDING];
|
|
41
|
-
export type StatusType = (typeof STATUS)[keyof typeof STATUS];
|
|
42
|
+
export declare function isObject(val: unknown): val is object;
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var z=Object.defineProperty;var U=(n,t,e)=>t in n?z(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var r=(n,t,e)=>U(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u={INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},d={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},f={INVALID:"Invalid",INSTANCE:"Instance",CONSTANT:"ConstantValue",DYNAMIC:"DynamicValue"},g={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."};function N(n,t){return Object.prototype.hasOwnProperty.call(n,t)}const D=Symbol("UNINITIALIZED"),y=new WeakMap;function m(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function P(n,t){y.set(n,t)}function V(n){const t=y.get(n);if(t)return t[u.POST_CONSTRUCT];if(m(n))return V(Object.getPrototypeOf(n))}function w(n){const t=y.get(n);if(t)return t[u.PRE_DESTROY];if(m(n))return w(Object.getPrototypeOf(n))}function R(n){const t=y.get(n),e=t&&N(t,u.INJECTED_PROPS)?t[u.INJECTED_PROPS]:void 0;if(!m(n))return e;const i=R(Object.getPrototypeOf(n));if(i||e)return{...i||{},...e||{}}}class Y{constructor(t){r(this,"name");this.name=t}}class j{constructor(t){r(this,"_callback");this._callback=t}resolve(){return this._callback()}}function b(n){if(!n)throw new Error(g.INVALID_TOKEN);return n instanceof j?n.resolve():n}class v 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}}class O extends v{constructor(t){super("");const e=[];let i=t;for(;i&&i.token;)e.push(i.token),i=i.parent;const s=e.reverse().map(o=>o.name||"<anonymous>").join(" --> ");this.message=`Circular dependency found: ${s}`}}class L extends v{constructor(t){super("Invalid binding: ",t)}}class k extends O{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const E=class E{constructor(t,e){r(this,"container");r(this,"context");r(this,"token");r(this,"type",f.INVALID);r(this,"status",d.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=f.INSTANCE,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=f.CONSTANT,this.constantValue=t,this}toDynamicValue(t){return this.type=f.DYNAMIC,this.dynamicValue=t,this}inTransientScope(){return this.transient=!0,this}toService(t){return this.toDynamicValue(e=>e.container.get(t,{parent:{token:this.token}}))}get(t){if(d.INITING===this.status)throw new O(t);if(d.ACTIVATED===this.status)if(this.transient)this.status=d.DEFAULT;else return this.cache;const e=E._resolvers[this.type];if(e)return this[e](t);throw new L(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(f.INSTANCE===this.type){const{key:i,value:s}=V(this.classValue)||{};if(i)if(s){const o=e.filter(c=>f.INSTANCE===(c==null?void 0:c.type)),a=this._getAwaitBindings(o,s);for(const c of a)if(c&&c.postConstructResult===D)throw new k({token:c.token,parent:t});const l=a.map(c=>c.postConstructResult);this.postConstructResult=Promise.all(l).then(()=>this._execute(i))}else this.postConstructResult=this._execute(i);else this.postConstructResult=void 0}}preDestroy(){if(f.INSTANCE===this.type){const{key:t}=w(this.classValue)||{};t&&this._execute(t)}_._instanceContainerMap.delete(this.cache),this.container=null,this.context=null,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=d.INITING;const e=this._createInstance();this.cache=this.activate(e),this.status=d.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(){_._instanceContainerMap.set(this.cache,this.container)}_injectProperties(t){Object.assign(this.cache,t)}_resolveConstantValue(){return this.status=d.INITING,this.cache=this.activate(this.constantValue),this.status=d.ACTIVATED,this.cache}_resolveDynamicValue(){this.status=d.INITING;const t=this.dynamicValue(this.context);return this.cache=this.activate(t),this.status=d.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 l=i[a],c=e[l],{inject:I,...h}=c;h.parent=t;const p=this.container.get(b(I),h);p===void 0&&c.optional||(s[l]=p),o.push(h.binding)}return{properties:s,bindings:o}}};r(E,"_resolvers",{[f.INSTANCE]:"_resolveInstanceValue",[f.CONSTANT]:"_resolveConstantValue",[f.DYNAMIC]:"_resolveDynamicValue"});let C=E;class B extends v{constructor(t){super("No matching binding found for token: ",t)}}class H extends v{constructor(t){super("Cannot bind token multiple times: ",t)}}const T=class T{constructor(){r(this,"parent");r(this,"children");r(this,"_bindings",new Map);r(this,"_onActivationHandler");r(this,"_onDeactivationHandler")}static getContainerOf(t){return T._instanceContainerMap.get(t)}bind(t){if(this._bindings.has(t))throw new H(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)}}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 T;return t.parent=this,this.children||(this.children=new Set),this.children.add(t),t}destroy(){var t,e;if(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={}){return e.skipSelf?this._resolveSkipSelf(t,e):e.self?this._resolveSelf(t,e):this._resolveDefault(t,e)}async getAsync(t,e={}){const i=this.get(t,e),s=e.binding;return(s==null?void 0:s.postConstructResult)instanceof Promise&&await s.postConstructResult,i}_resolveSkipSelf(t,e){return this.parent?(e.skipSelf=!1,this.parent.get(t,e)):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.get(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 C(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new B(t)}};r(T,"_instanceContainerMap",new WeakMap);let _=T;class M extends v{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 A(n,t){return function(e){return function(i,s){const o=s.name,a=s.metadata;N(a,u.INJECTED_PROPS)||(a[u.INJECTED_PROPS]={});const l=a[u.INJECTED_PROPS];l[o]||(l[o]={}),l[o][n]=e===void 0?t:e}}}function F(n,t){return e=>(i,s)=>{const o=s.name,a=s.metadata;if(N(a,n))throw new Error(t);a[n]={key:o,value:e}}}const J=A(u.INJECT),K=A(u.SELF,!0),G=A(u.SKIP_SELF,!0),$=A(u.OPTIONAL,!0),Z=F(u.POST_CONSTRUCT,g.POST_CONSTRUCT),q=F(u.PRE_DESTROY,g.PRE_DESTROY);function W(){return function(n,t){const e=t.metadata;P(n,e)}}function Q(n,t,e,i){if(e==null)throw new Error(g.LAZY_INJECT_INVALID_TOKEN);const s=Symbol.for(t);Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get(){if(!N(n,s)){const o=i||_.getContainerOf(n),a=n.constructor;if(!o)throw new M(b(e),a);n[s]=o.get(b(e),{parent:{token:a}})}return n[s]},set(o){n[s]=o}})}function x(n,t){return function(e,i){const s=i.name;i.addInitializer(function(){Q(this,s,n,t)})}}function X(n){return function(t){return x(t,n)}}function tt(n,t){const e=t.name;t.addInitializer(function(){this[e]=n.bind(this)})}const S=Symbol("decorate.metadata");function et(n,t,e){const i=Array.isArray(n)?n:[n],s=t.prototype,o=typeof s[e]=="function",a=[];N(t,S)||(t[S]={});const l=t[S],c={kind:o?"method":"field",name:e,static:!1,private:!1,addInitializer(h){a.push(h)},metadata:l};let I=o?s[e]:void 0;for(let h=i.length-1;h>=0;h--){const p=i[h](I,c);o&&typeof p=="function"&&(I=p)}if(o&&I!==s[e]&&(s[e]=I),P(t,l),a.length>0){const h=Object.create(s);for(const p of a)p.call(h)}}exports.BaseError=v;exports.Binding=C;exports.BindingNotFoundError=B;exports.BindingNotValidError=L;exports.CircularDependencyError=O;exports.Container=_;exports.ContainerNotFoundError=M;exports.DuplicateBindingError=H;exports.Inject=J;exports.Injectable=W;exports.LazyInject=x;exports.LazyToken=j;exports.Optional=$;exports.PostConstruct=Z;exports.PostConstructError=k;exports.PreDestroy=q;exports.Self=K;exports.SkipSelf=G;exports.Token=Y;exports.autobind=tt;exports.createLazyInject=X;exports.decorate=et;
|
|
1
|
+
"use strict";var z=Object.defineProperty;var U=(n,t,e)=>t in n?z(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var r=(n,t,e)=>U(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"},g={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."},S=Symbol("UNINITIALIZED");function E(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function Y(n){return n!==null&&typeof n=="object"}const y=new WeakMap;function b(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function m(n,t){y.set(n,t)}function V(n){const t=y.get(n);if(t)return t[l.POST_CONSTRUCT];if(b(n))return V(Object.getPrototypeOf(n))}function j(n){const t=y.get(n);if(t)return t[l.PRE_DESTROY];if(b(n))return j(Object.getPrototypeOf(n))}function w(n){const t=y.get(n),e=t&&E(t,l.INJECTED_PROPS)?t[l.INJECTED_PROPS]:void 0;if(!b(n))return e;const i=w(Object.getPrototypeOf(n));if(i||e)return Object.assign({},i,e)}class J{constructor(t){r(this,"name");this.name=t}}class R{constructor(t){r(this,"_callback");this._callback=t}resolve(){return this._callback()}}function O(n){if(!n)throw new Error(g.INVALID_TOKEN);return n instanceof R?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}}class P extends _{constructor(t){super("");const e=[];let i=t;for(;i&&i.token;)e.push(i.token),i=i.parent;const s=e.reverse().map(o=>o.name||"<anonymous>").join(" --> ");this.message=`Circular dependency found: ${s}`}}class L extends _{constructor(t){super("Invalid binding: ",t)}}class k extends P{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const C=class C{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",S);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.get(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=C._resolvers[this.type];if(e)return this[e](t);throw new L(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===S)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){const{key:t}=j(this.classValue)||{};t&&this._execute(t)}I._instanceContainerMap.delete(this.cache),this.container=null,this.context=null,this.classValue=void 0,this.constantValue=void 0,this.dynamicValue=void 0,this.cache=void 0,this.postConstructResult=S,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=w(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.get(O(h.inject),h);d===void 0&&h.optional||(s[c]=d),o.push(h.binding)}return{properties:s,bindings:o}}};r(C,"_resolvers",{[p.INSTANCE]:"_resolveInstanceValue",[p.CONSTANT]:"_resolveConstantValue",[p.DYNAMIC]:"_resolveDynamicValue"});let T=C;class B extends _{constructor(t){super("No matching binding found for token: ",t)}}class H extends _{constructor(t){super("Cannot bind token multiple times: ",t)}}const v=class v{constructor(){r(this,"parent");r(this,"children");r(this,"_bindings",new Map);r(this,"_onActivationHandler");r(this,"_onDeactivationHandler")}static getContainerOf(t){return v._instanceContainerMap.get(t)}bind(t){if(this._bindings.has(t))throw new H(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)}}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.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={}){return e.skipSelf?this._resolveSkipSelf(t,e):e.self?this._resolveSelf(t,e):this._resolveDefault(t,e)}getAsync(t,e={}){let i;try{i=this.get(t,e)}catch(o){return Promise.reject(o)}const s=e.binding;return(s==null?void 0:s.postConstructResult)instanceof Promise?s.postConstructResult.then(()=>i):Promise.resolve(i)}_resolveSkipSelf(t,e){return this.parent?(e.skipSelf=!1,this.parent.get(t,e)):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.get(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 T(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new B(t)}};r(v,"_instanceContainerMap",new WeakMap);let I=v;class M 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 D(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 F(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 K=D(l.INJECT),G=D(l.SELF,!0),$=D(l.SKIP_SELF,!0),Z=D(l.OPTIONAL,!0),q=F(l.POST_CONSTRUCT,g.POST_CONSTRUCT),W=F(l.PRE_DESTROY,g.PRE_DESTROY);function Q(){return function(n,t){const e=t.metadata;m(n,e)}}function X(n,t,e,i){if(e==null)throw new Error(g.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 M(O(e),c);s=a.get(O(e),{parent:{token:c}}),o=!0}return s},set(a){s=a,o=!0}})}function x(n,t){return function(e,i){const s=i.name;i.addInitializer(function(){X(this,s,n,t)})}}function tt(n){return function(t){return x(t,n)}}function et(n,t){const e=t.name;t.addInitializer(function(){this[e]=n.bind(this)})}const A=new WeakMap;function nt(n,t,e){const i=Array.isArray(n)?n:[n],s=t.prototype,o=typeof s[e]=="function",a=[];A.has(t)||A.set(t,{});const c=A.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 N=i[d](h,u);o&&typeof N=="function"&&(h=N)}if(o&&h!==s[e]&&(s[e]=h),m(t,c),a.length>0){const d=Object.create(s);for(const N of a)N.call(d)}}exports.BaseError=_;exports.Binding=T;exports.BindingNotFoundError=B;exports.BindingNotValidError=L;exports.CircularDependencyError=P;exports.Container=I;exports.ContainerNotFoundError=M;exports.DuplicateBindingError=H;exports.ERRORS=g;exports.Inject=K;exports.Injectable=Q;exports.LazyInject=x;exports.LazyToken=R;exports.Optional=Z;exports.PostConstruct=q;exports.PostConstructError=k;exports.PreDestroy=W;exports.Self=G;exports.SkipSelf=$;exports.Token=J;exports.autobind=et;exports.createLazyInject=tt;exports.decorate=nt;exports.hasOwn=E;exports.isObject=Y;
|
package/dist/index.d.cts
CHANGED
|
@@ -10,3 +10,4 @@ export { CircularDependencyError } from './errors/CircularDependencyError';
|
|
|
10
10
|
export { DuplicateBindingError } from './errors/DuplicateBindingError';
|
|
11
11
|
export { PostConstructError } from './errors/PostConstructError';
|
|
12
12
|
export { ContainerNotFoundError } from './errors/ContainerNotFoundError';
|
|
13
|
+
export { hasOwn, isObject, ERRORS } from './constants';
|
package/dist/index.d.ts
CHANGED
|
@@ -10,3 +10,4 @@ export { CircularDependencyError } from './errors/CircularDependencyError';
|
|
|
10
10
|
export { DuplicateBindingError } from './errors/DuplicateBindingError';
|
|
11
11
|
export { PostConstructError } from './errors/PostConstructError';
|
|
12
12
|
export { ContainerNotFoundError } from './errors/ContainerNotFoundError';
|
|
13
|
+
export { hasOwn, isObject, ERRORS } from './constants';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var L = Object.defineProperty;
|
|
2
2
|
var k = (n, t, e) => t in n ? L(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
3
|
var r = (n, t, e) => k(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
-
const
|
|
4
|
+
const l = {
|
|
5
5
|
// 记录实例属性装饰器对应的数据的键
|
|
6
6
|
INJECTED_PROPS: "injected:props",
|
|
7
7
|
// Inject 装饰器的键
|
|
@@ -16,11 +16,11 @@ const u = {
|
|
|
16
16
|
POST_CONSTRUCT: "postConstruct",
|
|
17
17
|
// PreDestroy 装饰器的键
|
|
18
18
|
PRE_DESTROY: "preDestroy"
|
|
19
|
-
},
|
|
19
|
+
}, f = {
|
|
20
20
|
DEFAULT: "default",
|
|
21
21
|
INITING: "initing",
|
|
22
22
|
ACTIVATED: "activated"
|
|
23
|
-
},
|
|
23
|
+
}, p = {
|
|
24
24
|
INVALID: "Invalid",
|
|
25
25
|
INSTANCE: "Instance",
|
|
26
26
|
CONSTANT: "ConstantValue",
|
|
@@ -33,43 +33,43 @@ const u = {
|
|
|
33
33
|
INVALID_TOKEN: "@Inject or @LazyInject 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
|
-
};
|
|
37
|
-
function
|
|
36
|
+
}, y = Symbol("UNINITIALIZED");
|
|
37
|
+
function m(n, t) {
|
|
38
38
|
return Object.prototype.hasOwnProperty.call(n, t);
|
|
39
39
|
}
|
|
40
|
-
|
|
40
|
+
function K(n) {
|
|
41
|
+
return n !== null && typeof n == "object";
|
|
42
|
+
}
|
|
43
|
+
const C = /* @__PURE__ */ new WeakMap();
|
|
41
44
|
function O(n) {
|
|
42
45
|
return typeof n == "function" && Object.getPrototypeOf(n) !== Function.prototype;
|
|
43
46
|
}
|
|
44
47
|
function b(n, t) {
|
|
45
|
-
|
|
48
|
+
C.set(n, t);
|
|
46
49
|
}
|
|
47
50
|
function P(n) {
|
|
48
|
-
const t =
|
|
51
|
+
const t = C.get(n);
|
|
49
52
|
if (t)
|
|
50
|
-
return t[
|
|
53
|
+
return t[l.POST_CONSTRUCT];
|
|
51
54
|
if (O(n))
|
|
52
55
|
return P(Object.getPrototypeOf(n));
|
|
53
56
|
}
|
|
54
57
|
function V(n) {
|
|
55
|
-
const t =
|
|
58
|
+
const t = C.get(n);
|
|
56
59
|
if (t)
|
|
57
|
-
return t[
|
|
60
|
+
return t[l.PRE_DESTROY];
|
|
58
61
|
if (O(n))
|
|
59
62
|
return V(Object.getPrototypeOf(n));
|
|
60
63
|
}
|
|
61
64
|
function w(n) {
|
|
62
|
-
const t =
|
|
65
|
+
const t = C.get(n), e = t && m(t, l.INJECTED_PROPS) ? t[l.INJECTED_PROPS] : void 0;
|
|
63
66
|
if (!O(n))
|
|
64
67
|
return e;
|
|
65
68
|
const i = w(Object.getPrototypeOf(n));
|
|
66
69
|
if (i || e)
|
|
67
|
-
return {
|
|
68
|
-
...i || {},
|
|
69
|
-
...e || {}
|
|
70
|
-
};
|
|
70
|
+
return Object.assign({}, i, e);
|
|
71
71
|
}
|
|
72
|
-
class
|
|
72
|
+
class G {
|
|
73
73
|
constructor(t) {
|
|
74
74
|
// 仅类型层面存在,无运行时开销
|
|
75
75
|
r(this, "name");
|
|
@@ -85,12 +85,12 @@ class B {
|
|
|
85
85
|
return this._callback();
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
|
-
function
|
|
88
|
+
function D(n) {
|
|
89
89
|
if (!n)
|
|
90
90
|
throw new Error(g.INVALID_TOKEN);
|
|
91
91
|
return n instanceof B ? n.resolve() : n;
|
|
92
92
|
}
|
|
93
|
-
class
|
|
93
|
+
class v extends Error {
|
|
94
94
|
constructor(e, i) {
|
|
95
95
|
const s = (i == null ? void 0 : i.name) || "<unknown token>";
|
|
96
96
|
super(`${e}${s}`);
|
|
@@ -98,7 +98,7 @@ class N extends Error {
|
|
|
98
98
|
this.name = this.constructor.name, this.token = i;
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
class R extends
|
|
101
|
+
class R extends v {
|
|
102
102
|
constructor(t) {
|
|
103
103
|
super("");
|
|
104
104
|
const e = [];
|
|
@@ -109,7 +109,7 @@ class R extends N {
|
|
|
109
109
|
this.message = `Circular dependency found: ${s}`;
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
|
-
class H extends
|
|
112
|
+
class H extends v {
|
|
113
113
|
constructor(t) {
|
|
114
114
|
super("Invalid binding: ", t);
|
|
115
115
|
}
|
|
@@ -119,13 +119,13 @@ class M extends R {
|
|
|
119
119
|
super(t), this.name = "CircularDependencyError inside @PostConstruct";
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
const
|
|
122
|
+
const T = class T {
|
|
123
123
|
constructor(t, e) {
|
|
124
124
|
r(this, "container");
|
|
125
125
|
r(this, "context");
|
|
126
126
|
r(this, "token");
|
|
127
|
-
r(this, "type",
|
|
128
|
-
r(this, "status",
|
|
127
|
+
r(this, "type", p.INVALID);
|
|
128
|
+
r(this, "status", f.DEFAULT);
|
|
129
129
|
r(this, "classValue");
|
|
130
130
|
r(this, "constantValue");
|
|
131
131
|
r(this, "dynamicValue");
|
|
@@ -151,16 +151,16 @@ const C = class C {
|
|
|
151
151
|
this.onDeactivationHandler && this.onDeactivationHandler(this.cache);
|
|
152
152
|
}
|
|
153
153
|
to(t) {
|
|
154
|
-
return this.type =
|
|
154
|
+
return this.type = p.INSTANCE, this.classValue = t, this;
|
|
155
155
|
}
|
|
156
156
|
toSelf() {
|
|
157
157
|
return this.to(this.token);
|
|
158
158
|
}
|
|
159
159
|
toConstantValue(t) {
|
|
160
|
-
return this.type =
|
|
160
|
+
return this.type = p.CONSTANT, this.constantValue = t, this;
|
|
161
161
|
}
|
|
162
162
|
toDynamicValue(t) {
|
|
163
|
-
return this.type =
|
|
163
|
+
return this.type = p.DYNAMIC, this.dynamicValue = t, this;
|
|
164
164
|
}
|
|
165
165
|
inTransientScope() {
|
|
166
166
|
return this.transient = !0, this;
|
|
@@ -171,14 +171,14 @@ const C = class C {
|
|
|
171
171
|
);
|
|
172
172
|
}
|
|
173
173
|
get(t) {
|
|
174
|
-
if (
|
|
174
|
+
if (f.INITING === this.status)
|
|
175
175
|
throw new R(t);
|
|
176
|
-
if (
|
|
176
|
+
if (f.ACTIVATED === this.status)
|
|
177
177
|
if (this.transient)
|
|
178
|
-
this.status =
|
|
178
|
+
this.status = f.DEFAULT;
|
|
179
179
|
else
|
|
180
180
|
return this.cache;
|
|
181
|
-
const e =
|
|
181
|
+
const e = T._resolvers[this.type];
|
|
182
182
|
if (e)
|
|
183
183
|
return this[e](t);
|
|
184
184
|
throw new H(this.token);
|
|
@@ -200,21 +200,21 @@ const C = class C {
|
|
|
200
200
|
* - 如果前置服务初始化失败,rejected promise 自然传播,当前服务的 PostConstruct 不执行
|
|
201
201
|
*/
|
|
202
202
|
_postConstruct(t, e) {
|
|
203
|
-
if (
|
|
203
|
+
if (p.INSTANCE === this.type) {
|
|
204
204
|
const { key: i, value: s } = P(this.classValue) || {};
|
|
205
205
|
if (i)
|
|
206
206
|
if (s) {
|
|
207
207
|
const o = e.filter(
|
|
208
|
-
(
|
|
208
|
+
(u) => p.INSTANCE === (u == null ? void 0 : u.type)
|
|
209
209
|
), a = this._getAwaitBindings(o, s);
|
|
210
|
-
for (const
|
|
211
|
-
if (
|
|
210
|
+
for (const u of a)
|
|
211
|
+
if (u && u.postConstructResult === y)
|
|
212
212
|
throw new M({
|
|
213
|
-
token:
|
|
213
|
+
token: u.token,
|
|
214
214
|
parent: t
|
|
215
215
|
});
|
|
216
|
-
const
|
|
217
|
-
this.postConstructResult = Promise.all(
|
|
216
|
+
const c = a.map((u) => u.postConstructResult);
|
|
217
|
+
this.postConstructResult = Promise.all(c).then(
|
|
218
218
|
() => this._execute(i)
|
|
219
219
|
);
|
|
220
220
|
} else
|
|
@@ -224,20 +224,20 @@ const C = class C {
|
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
preDestroy() {
|
|
227
|
-
if (
|
|
227
|
+
if (p.INSTANCE === this.type) {
|
|
228
228
|
const { key: t } = V(this.classValue) || {};
|
|
229
229
|
t && this._execute(t);
|
|
230
230
|
}
|
|
231
|
-
|
|
231
|
+
I._instanceContainerMap.delete(this.cache), this.container = null, this.context = null, this.classValue = void 0, this.constantValue = void 0, this.dynamicValue = void 0, this.cache = void 0, this.postConstructResult = y, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
|
|
232
232
|
}
|
|
233
233
|
_execute(t) {
|
|
234
234
|
const e = this.cache[t];
|
|
235
235
|
return e == null ? void 0 : e.call(this.cache);
|
|
236
236
|
}
|
|
237
237
|
_resolveInstanceValue(t) {
|
|
238
|
-
this.status =
|
|
238
|
+
this.status = f.INITING;
|
|
239
239
|
const e = this._createInstance();
|
|
240
|
-
this.cache = this.activate(e), this.status =
|
|
240
|
+
this.cache = this.activate(e), this.status = f.ACTIVATED, this._registerInstance();
|
|
241
241
|
const { properties: i, bindings: s } = this._getInjectProperties(t);
|
|
242
242
|
return this._injectProperties(i), this._postConstruct(t, s), this.cache;
|
|
243
243
|
}
|
|
@@ -248,52 +248,52 @@ const C = class C {
|
|
|
248
248
|
}
|
|
249
249
|
// 注册实例与容器的映射关系
|
|
250
250
|
_registerInstance() {
|
|
251
|
-
|
|
251
|
+
I._instanceContainerMap.set(this.cache, this.container);
|
|
252
252
|
}
|
|
253
253
|
// 将解析后的属性注入到实例上
|
|
254
254
|
_injectProperties(t) {
|
|
255
255
|
Object.assign(this.cache, t);
|
|
256
256
|
}
|
|
257
257
|
_resolveConstantValue() {
|
|
258
|
-
return this.status =
|
|
258
|
+
return this.status = f.INITING, this.cache = this.activate(this.constantValue), this.status = f.ACTIVATED, this.cache;
|
|
259
259
|
}
|
|
260
260
|
_resolveDynamicValue() {
|
|
261
|
-
this.status =
|
|
261
|
+
this.status = f.INITING;
|
|
262
262
|
const t = this.dynamicValue(this.context);
|
|
263
|
-
return this.cache = this.activate(t), this.status =
|
|
263
|
+
return this.cache = this.activate(t), this.status = f.ACTIVATED, this.cache;
|
|
264
264
|
}
|
|
265
265
|
_getInjectProperties(t) {
|
|
266
266
|
const e = w(this.classValue) || {}, i = Object.keys(e), s = /* @__PURE__ */ Object.create(null), o = [];
|
|
267
267
|
for (let a = 0; a < i.length; a++) {
|
|
268
|
-
const
|
|
268
|
+
const c = i[a], u = e[c], h = Object.assign({}, u);
|
|
269
269
|
h.parent = t;
|
|
270
|
-
const
|
|
271
|
-
|
|
270
|
+
const d = this.container.get(
|
|
271
|
+
D(h.inject),
|
|
272
272
|
h
|
|
273
273
|
);
|
|
274
|
-
|
|
274
|
+
d === void 0 && h.optional || (s[c] = d), o.push(h.binding);
|
|
275
275
|
}
|
|
276
276
|
return { properties: s, bindings: o };
|
|
277
277
|
}
|
|
278
278
|
};
|
|
279
279
|
// 类型到解析方法的静态映射表,用于替代 get 方法中的 if-else 链
|
|
280
|
-
r(
|
|
281
|
-
[
|
|
282
|
-
[
|
|
283
|
-
[
|
|
280
|
+
r(T, "_resolvers", {
|
|
281
|
+
[p.INSTANCE]: "_resolveInstanceValue",
|
|
282
|
+
[p.CONSTANT]: "_resolveConstantValue",
|
|
283
|
+
[p.DYNAMIC]: "_resolveDynamicValue"
|
|
284
284
|
});
|
|
285
|
-
let
|
|
286
|
-
class x extends
|
|
285
|
+
let S = T;
|
|
286
|
+
class x extends v {
|
|
287
287
|
constructor(t) {
|
|
288
288
|
super("No matching binding found for token: ", t);
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
|
-
class F extends
|
|
291
|
+
class F extends v {
|
|
292
292
|
constructor(t) {
|
|
293
293
|
super("Cannot bind token multiple times: ", t);
|
|
294
294
|
}
|
|
295
295
|
}
|
|
296
|
-
const
|
|
296
|
+
const _ = class _ {
|
|
297
297
|
constructor() {
|
|
298
298
|
r(this, "parent");
|
|
299
299
|
r(this, "children");
|
|
@@ -303,7 +303,7 @@ const I = class I {
|
|
|
303
303
|
}
|
|
304
304
|
// 查询实例所属的容器
|
|
305
305
|
static getContainerOf(t) {
|
|
306
|
-
return
|
|
306
|
+
return _._instanceContainerMap.get(t);
|
|
307
307
|
}
|
|
308
308
|
bind(t) {
|
|
309
309
|
if (this._bindings.has(t))
|
|
@@ -329,7 +329,7 @@ const I = class I {
|
|
|
329
329
|
return this.isCurrentBound(t) || !!this.parent && this.parent.isBound(t);
|
|
330
330
|
}
|
|
331
331
|
createChild() {
|
|
332
|
-
const t = new
|
|
332
|
+
const t = new _();
|
|
333
333
|
return t.parent = this, this.children || (this.children = /* @__PURE__ */ new Set()), this.children.add(t), t;
|
|
334
334
|
}
|
|
335
335
|
destroy() {
|
|
@@ -344,9 +344,15 @@ const I = class I {
|
|
|
344
344
|
get(t, e = {}) {
|
|
345
345
|
return e.skipSelf ? this._resolveSkipSelf(t, e) : e.self ? this._resolveSelf(t, e) : this._resolveDefault(t, e);
|
|
346
346
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
347
|
+
getAsync(t, e = {}) {
|
|
348
|
+
let i;
|
|
349
|
+
try {
|
|
350
|
+
i = this.get(t, e);
|
|
351
|
+
} catch (o) {
|
|
352
|
+
return Promise.reject(o);
|
|
353
|
+
}
|
|
354
|
+
const s = e.binding;
|
|
355
|
+
return (s == null ? void 0 : s.postConstructResult) instanceof Promise ? s.postConstructResult.then(() => i) : Promise.resolve(i);
|
|
350
356
|
}
|
|
351
357
|
// 处理 skipSelf 选项:跳过当前容器,委托父容器解析
|
|
352
358
|
_resolveSkipSelf(t, e) {
|
|
@@ -375,7 +381,7 @@ const I = class I {
|
|
|
375
381
|
this._onDeactivationHandler && this._onDeactivationHandler(t.cache, t.token);
|
|
376
382
|
}
|
|
377
383
|
_buildBinding(t) {
|
|
378
|
-
return new
|
|
384
|
+
return new S(t, this);
|
|
379
385
|
}
|
|
380
386
|
_getBinding(t) {
|
|
381
387
|
return this._bindings.get(t);
|
|
@@ -391,9 +397,9 @@ const I = class I {
|
|
|
391
397
|
// 同一对象可能通过 toConstantValue 被绑定到多个容器,WeakMap 只能保留最后一次映射,
|
|
392
398
|
// 会导致 @LazyInject 从错误的容器解析依赖。
|
|
393
399
|
// 由于 Instance 类型每次都通过 new ClassName() 创建新实例,不存在同一实例被多个容器注册的覆盖风险
|
|
394
|
-
r(
|
|
395
|
-
let
|
|
396
|
-
class z extends
|
|
400
|
+
r(_, "_instanceContainerMap", /* @__PURE__ */ new WeakMap());
|
|
401
|
+
let I = _;
|
|
402
|
+
class z extends v {
|
|
397
403
|
constructor(t, e) {
|
|
398
404
|
super(
|
|
399
405
|
`@LazyInject(${t == null ? void 0 : t.name}) in class ${e.name} requires a registered container but none was found. Token: `,
|
|
@@ -401,32 +407,32 @@ class z extends N {
|
|
|
401
407
|
);
|
|
402
408
|
}
|
|
403
409
|
}
|
|
404
|
-
function
|
|
410
|
+
function E(n, t) {
|
|
405
411
|
return function(e) {
|
|
406
412
|
return function(i, s) {
|
|
407
413
|
const o = s.name, a = s.metadata;
|
|
408
|
-
|
|
409
|
-
const
|
|
410
|
-
|
|
414
|
+
m(a, l.INJECTED_PROPS) || (a[l.INJECTED_PROPS] = {});
|
|
415
|
+
const c = a[l.INJECTED_PROPS];
|
|
416
|
+
c[o] || (c[o] = {}), c[o][n] = e === void 0 ? t : e;
|
|
411
417
|
};
|
|
412
418
|
};
|
|
413
419
|
}
|
|
414
420
|
function j(n, t) {
|
|
415
421
|
return (e) => (i, s) => {
|
|
416
422
|
const o = s.name, a = s.metadata;
|
|
417
|
-
if (
|
|
423
|
+
if (m(a, n))
|
|
418
424
|
throw new Error(t);
|
|
419
425
|
a[n] = { key: o, value: e };
|
|
420
426
|
};
|
|
421
427
|
}
|
|
422
|
-
const
|
|
423
|
-
|
|
428
|
+
const $ = E(l.INJECT), Z = E(l.SELF, !0), q = E(l.SKIP_SELF, !0), W = E(l.OPTIONAL, !0), Q = j(
|
|
429
|
+
l.POST_CONSTRUCT,
|
|
424
430
|
g.POST_CONSTRUCT
|
|
425
|
-
),
|
|
426
|
-
|
|
431
|
+
), X = j(
|
|
432
|
+
l.PRE_DESTROY,
|
|
427
433
|
g.PRE_DESTROY
|
|
428
434
|
);
|
|
429
|
-
function
|
|
435
|
+
function tt() {
|
|
430
436
|
return function(n, t) {
|
|
431
437
|
const e = t.metadata;
|
|
432
438
|
b(n, e);
|
|
@@ -435,23 +441,23 @@ function X() {
|
|
|
435
441
|
function U(n, t, e, i) {
|
|
436
442
|
if (e == null)
|
|
437
443
|
throw new Error(g.LAZY_INJECT_INVALID_TOKEN);
|
|
438
|
-
|
|
444
|
+
let s, o = !1;
|
|
439
445
|
Object.defineProperty(n, t, {
|
|
440
446
|
configurable: !0,
|
|
441
447
|
enumerable: !0,
|
|
442
448
|
get() {
|
|
443
|
-
if (!
|
|
444
|
-
const
|
|
445
|
-
if (!
|
|
446
|
-
throw new z(
|
|
447
|
-
|
|
448
|
-
parent: { token:
|
|
449
|
-
});
|
|
449
|
+
if (!o) {
|
|
450
|
+
const a = i || I.getContainerOf(n), c = n.constructor;
|
|
451
|
+
if (!a)
|
|
452
|
+
throw new z(D(e), c);
|
|
453
|
+
s = a.get(D(e), {
|
|
454
|
+
parent: { token: c }
|
|
455
|
+
}), o = !0;
|
|
450
456
|
}
|
|
451
|
-
return
|
|
457
|
+
return s;
|
|
452
458
|
},
|
|
453
|
-
set(
|
|
454
|
-
|
|
459
|
+
set(a) {
|
|
460
|
+
s = a, o = !0;
|
|
455
461
|
}
|
|
456
462
|
});
|
|
457
463
|
}
|
|
@@ -463,63 +469,66 @@ function Y(n, t) {
|
|
|
463
469
|
});
|
|
464
470
|
};
|
|
465
471
|
}
|
|
466
|
-
function
|
|
472
|
+
function et(n) {
|
|
467
473
|
return function(t) {
|
|
468
474
|
return Y(t, n);
|
|
469
475
|
};
|
|
470
476
|
}
|
|
471
|
-
function
|
|
477
|
+
function nt(n, t) {
|
|
472
478
|
const e = t.name;
|
|
473
479
|
t.addInitializer(function() {
|
|
474
480
|
this[e] = n.bind(this);
|
|
475
481
|
});
|
|
476
482
|
}
|
|
477
|
-
const
|
|
478
|
-
function
|
|
483
|
+
const A = /* @__PURE__ */ new WeakMap();
|
|
484
|
+
function it(n, t, e) {
|
|
479
485
|
const i = Array.isArray(n) ? n : [n], s = t.prototype, o = typeof s[e] == "function", a = [];
|
|
480
|
-
|
|
481
|
-
const
|
|
486
|
+
A.has(t) || A.set(t, {});
|
|
487
|
+
const c = A.get(t), u = {
|
|
482
488
|
kind: o ? "method" : "field",
|
|
483
489
|
name: e,
|
|
484
490
|
static: !1,
|
|
485
491
|
private: !1,
|
|
486
|
-
addInitializer(
|
|
487
|
-
a.push(
|
|
492
|
+
addInitializer(d) {
|
|
493
|
+
a.push(d);
|
|
488
494
|
},
|
|
489
|
-
metadata:
|
|
495
|
+
metadata: c
|
|
490
496
|
};
|
|
491
|
-
let
|
|
492
|
-
for (let
|
|
493
|
-
const
|
|
494
|
-
o && typeof
|
|
497
|
+
let h = o ? s[e] : void 0;
|
|
498
|
+
for (let d = i.length - 1; d >= 0; d--) {
|
|
499
|
+
const N = i[d](h, u);
|
|
500
|
+
o && typeof N == "function" && (h = N);
|
|
495
501
|
}
|
|
496
|
-
if (o &&
|
|
497
|
-
const
|
|
498
|
-
for (const
|
|
499
|
-
|
|
502
|
+
if (o && h !== s[e] && (s[e] = h), b(t, c), a.length > 0) {
|
|
503
|
+
const d = Object.create(s);
|
|
504
|
+
for (const N of a)
|
|
505
|
+
N.call(d);
|
|
500
506
|
}
|
|
501
507
|
}
|
|
502
508
|
export {
|
|
503
|
-
|
|
504
|
-
|
|
509
|
+
v as BaseError,
|
|
510
|
+
S as Binding,
|
|
505
511
|
x as BindingNotFoundError,
|
|
506
512
|
H as BindingNotValidError,
|
|
507
513
|
R as CircularDependencyError,
|
|
508
|
-
|
|
514
|
+
I as Container,
|
|
509
515
|
z as ContainerNotFoundError,
|
|
510
516
|
F as DuplicateBindingError,
|
|
511
|
-
|
|
512
|
-
|
|
517
|
+
g as ERRORS,
|
|
518
|
+
$ as Inject,
|
|
519
|
+
tt as Injectable,
|
|
513
520
|
Y as LazyInject,
|
|
514
521
|
B as LazyToken,
|
|
515
|
-
|
|
516
|
-
|
|
522
|
+
W as Optional,
|
|
523
|
+
Q as PostConstruct,
|
|
517
524
|
M as PostConstructError,
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
+
X as PreDestroy,
|
|
526
|
+
Z as Self,
|
|
527
|
+
q as SkipSelf,
|
|
528
|
+
G as Token,
|
|
529
|
+
nt as autobind,
|
|
530
|
+
et as createLazyInject,
|
|
531
|
+
it as decorate,
|
|
532
|
+
m as hasOwn,
|
|
533
|
+
K as isObject
|
|
525
534
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaokei/di",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Tiny di library depends on typescript decorator.",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"standard-version": "^9.5.0",
|
|
66
66
|
"typescript": "~5.6.3",
|
|
67
67
|
"unplugin-swc": "^1.5.9",
|
|
68
|
-
"vite": "
|
|
68
|
+
"vite": "^6.0.0",
|
|
69
69
|
"vite-plugin-dts": "^4.5.0",
|
|
70
70
|
"vitepress": "^1.6.3",
|
|
71
71
|
"vitest": "^3.1.1",
|