@kaokei/di 5.0.2 → 5.0.3
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 +137 -128
- 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 o=(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"},f={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},p={INVALID:"Invalid",INSTANCE:"Instance",CONSTANT:"ConstantValue",DYNAMIC:"DynamicValue"},N={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 _(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function Y(n){return n!==null&&typeof n=="object"}const y=new WeakMap;function O(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(O(n))return V(Object.getPrototypeOf(n))}function j(n){const t=y.get(n);if(t)return t[u.PRE_DESTROY];if(O(n))return j(Object.getPrototypeOf(n))}function w(n){const t=y.get(n),e=t&&_(t,u.INJECTED_PROPS)?t[u.INJECTED_PROPS]:void 0;if(!O(n))return e;const i=w(Object.getPrototypeOf(n));if(i||e)return Object.assign({},i,e)}class J{constructor(t){o(this,"name");this.name=t}}class R{constructor(t){o(this,"_callback");this._callback=t}resolve(){return this._callback()}}function b(n){if(!n)throw new Error(N.INVALID_TOKEN);return n instanceof R?n.resolve():n}class v extends Error{constructor(e,i){const s=(i==null?void 0:i.name)||"<unknown token>";super(`${e}${s}`);o(this,"token");this.name=this.constructor.name,this.token=i}}class m 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(r=>r.name||"<anonymous>").join(" --> ");this.message=`Circular dependency found: ${s}`}}class L extends v{constructor(t){super("Invalid binding: ",t)}}class k extends m{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const E=class E{constructor(t,e){o(this,"container");o(this,"context");o(this,"token");o(this,"type",p.INVALID);o(this,"status",f.DEFAULT);o(this,"classValue");o(this,"constantValue");o(this,"dynamicValue");o(this,"cache");o(this,"postConstructResult",D);o(this,"transient",!1);o(this,"onActivationHandler");o(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 m(t);if(f.ACTIVATED===this.status)if(this.transient)this.status=f.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(p.INSTANCE===this.type){const{key:i,value:s}=V(this.classValue)||{};if(i)if(s){const r=e.filter(c=>p.INSTANCE===(c==null?void 0:c.type)),a=this._getAwaitBindings(r,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(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=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=w(this.classValue)||{},i=Object.keys(e),s=Object.create(null),r=[];for(let a=0;a<i.length;a++){const l=i[a],c=e[l],h=Object.assign({},c);h.parent=t;const d=this.container.get(b(h.inject),h);d===void 0&&h.optional||(s[l]=d),r.push(h.binding)}return{properties:s,bindings:r}}};o(E,"_resolvers",{[p.INSTANCE]:"_resolveInstanceValue",[p.CONSTANT]:"_resolveConstantValue",[p.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(){o(this,"parent");o(this,"children");o(this,"_bindings",new Map);o(this,"_onActivationHandler");o(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)}getAsync(t,e={}){let i;try{i=this.get(t,e)}catch(r){return Promise.reject(r)}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 C(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new B(t)}};o(T,"_instanceContainerMap",new WeakMap);let I=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 r=s.name,a=s.metadata;_(a,u.INJECTED_PROPS)||(a[u.INJECTED_PROPS]={});const l=a[u.INJECTED_PROPS];l[r]||(l[r]={}),l[r][n]=e===void 0?t:e}}}function F(n,t){return e=>(i,s)=>{const r=s.name,a=s.metadata;if(_(a,n))throw new Error(t);a[n]={key:r,value:e}}}const K=A(u.INJECT),G=A(u.SELF,!0),$=A(u.SKIP_SELF,!0),Z=A(u.OPTIONAL,!0),q=F(u.POST_CONSTRUCT,N.POST_CONSTRUCT),W=F(u.PRE_DESTROY,N.PRE_DESTROY);function Q(){return function(n,t){const e=t.metadata;P(n,e)}}function X(n,t,e,i){if(e==null)throw new Error(N.LAZY_INJECT_INVALID_TOKEN);const s=Symbol.for(t);Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get(){if(!_(n,s)){const r=i||I.getContainerOf(n),a=n.constructor;if(!r)throw new M(b(e),a);n[s]=r.get(b(e),{parent:{token:a}})}return n[s]},set(r){n[s]=r}})}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 S=Symbol("decorate.metadata");function nt(n,t,e){const i=Array.isArray(n)?n:[n],s=t.prototype,r=typeof s[e]=="function",a=[];_(t,S)||(t[S]={});const l=t[S],c={kind:r?"method":"field",name:e,static:!1,private:!1,addInitializer(d){a.push(d)},metadata:l};let h=r?s[e]:void 0;for(let d=i.length-1;d>=0;d--){const g=i[d](h,c);r&&typeof g=="function"&&(h=g)}if(r&&h!==s[e]&&(s[e]=h),P(t,l),a.length>0){const d=Object.create(s);for(const g of a)g.call(d)}}exports.BaseError=v;exports.Binding=C;exports.BindingNotFoundError=B;exports.BindingNotValidError=L;exports.CircularDependencyError=m;exports.Container=I;exports.ContainerNotFoundError=M;exports.DuplicateBindingError=H;exports.ERRORS=N;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=_;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,6 +1,6 @@
|
|
|
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
|
-
var
|
|
3
|
+
var o = (n, t, e) => k(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
4
|
const u = {
|
|
5
5
|
// 记录实例属性装饰器对应的数据的键
|
|
6
6
|
INJECTED_PROPS: "injected:props",
|
|
@@ -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,52 +33,52 @@ 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
|
+
}, A = Symbol("UNINITIALIZED");
|
|
37
|
+
function v(n, t) {
|
|
38
38
|
return Object.prototype.hasOwnProperty.call(n, t);
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
function K(n) {
|
|
41
|
+
return n !== null && typeof n == "object";
|
|
42
|
+
}
|
|
43
|
+
const E = /* @__PURE__ */ new WeakMap();
|
|
44
|
+
function b(n) {
|
|
42
45
|
return typeof n == "function" && Object.getPrototypeOf(n) !== Function.prototype;
|
|
43
46
|
}
|
|
44
|
-
function
|
|
47
|
+
function O(n, t) {
|
|
45
48
|
E.set(n, t);
|
|
46
49
|
}
|
|
47
50
|
function P(n) {
|
|
48
51
|
const t = E.get(n);
|
|
49
52
|
if (t)
|
|
50
53
|
return t[u.POST_CONSTRUCT];
|
|
51
|
-
if (
|
|
54
|
+
if (b(n))
|
|
52
55
|
return P(Object.getPrototypeOf(n));
|
|
53
56
|
}
|
|
54
57
|
function V(n) {
|
|
55
58
|
const t = E.get(n);
|
|
56
59
|
if (t)
|
|
57
60
|
return t[u.PRE_DESTROY];
|
|
58
|
-
if (
|
|
61
|
+
if (b(n))
|
|
59
62
|
return V(Object.getPrototypeOf(n));
|
|
60
63
|
}
|
|
61
64
|
function w(n) {
|
|
62
|
-
const t = E.get(n), e = t &&
|
|
63
|
-
if (!
|
|
65
|
+
const t = E.get(n), e = t && v(t, u.INJECTED_PROPS) ? t[u.INJECTED_PROPS] : void 0;
|
|
66
|
+
if (!b(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
|
+
o(this, "name");
|
|
76
76
|
this.name = t;
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
class B {
|
|
80
80
|
constructor(t) {
|
|
81
|
-
|
|
81
|
+
o(this, "_callback");
|
|
82
82
|
this._callback = t;
|
|
83
83
|
}
|
|
84
84
|
resolve() {
|
|
@@ -90,26 +90,26 @@ function S(n) {
|
|
|
90
90
|
throw new Error(g.INVALID_TOKEN);
|
|
91
91
|
return n instanceof B ? n.resolve() : n;
|
|
92
92
|
}
|
|
93
|
-
class
|
|
93
|
+
class T extends Error {
|
|
94
94
|
constructor(e, i) {
|
|
95
95
|
const s = (i == null ? void 0 : i.name) || "<unknown token>";
|
|
96
96
|
super(`${e}${s}`);
|
|
97
|
-
|
|
97
|
+
o(this, "token");
|
|
98
98
|
this.name = this.constructor.name, this.token = i;
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
class R extends
|
|
101
|
+
class R extends T {
|
|
102
102
|
constructor(t) {
|
|
103
103
|
super("");
|
|
104
104
|
const e = [];
|
|
105
105
|
let i = t;
|
|
106
106
|
for (; i && i.token; )
|
|
107
107
|
e.push(i.token), i = i.parent;
|
|
108
|
-
const s = e.reverse().map((
|
|
108
|
+
const s = e.reverse().map((r) => r.name || "<anonymous>").join(" --> ");
|
|
109
109
|
this.message = `Circular dependency found: ${s}`;
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
|
-
class H extends
|
|
112
|
+
class H extends T {
|
|
113
113
|
constructor(t) {
|
|
114
114
|
super("Invalid binding: ", t);
|
|
115
115
|
}
|
|
@@ -121,20 +121,20 @@ class M extends R {
|
|
|
121
121
|
}
|
|
122
122
|
const C = class C {
|
|
123
123
|
constructor(t, e) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
124
|
+
o(this, "container");
|
|
125
|
+
o(this, "context");
|
|
126
|
+
o(this, "token");
|
|
127
|
+
o(this, "type", p.INVALID);
|
|
128
|
+
o(this, "status", f.DEFAULT);
|
|
129
|
+
o(this, "classValue");
|
|
130
|
+
o(this, "constantValue");
|
|
131
|
+
o(this, "dynamicValue");
|
|
132
|
+
o(this, "cache");
|
|
133
|
+
o(this, "postConstructResult", A);
|
|
134
134
|
// 是否为瞬态作用域,默认 false(单例)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
o(this, "transient", !1);
|
|
136
|
+
o(this, "onActivationHandler");
|
|
137
|
+
o(this, "onDeactivationHandler");
|
|
138
138
|
this.container = e, this.context = { container: this.container }, this.token = t;
|
|
139
139
|
}
|
|
140
140
|
onActivation(t) {
|
|
@@ -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,11 +171,11 @@ 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
181
|
const e = C._resolvers[this.type];
|
|
@@ -200,15 +200,15 @@ 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
|
-
const
|
|
208
|
-
(c) =>
|
|
209
|
-
), a = this._getAwaitBindings(
|
|
207
|
+
const r = e.filter(
|
|
208
|
+
(c) => p.INSTANCE === (c == null ? void 0 : c.type)
|
|
209
|
+
), a = this._getAwaitBindings(r, s);
|
|
210
210
|
for (const c of a)
|
|
211
|
-
if (c && c.postConstructResult ===
|
|
211
|
+
if (c && c.postConstructResult === A)
|
|
212
212
|
throw new M({
|
|
213
213
|
token: c.token,
|
|
214
214
|
parent: t
|
|
@@ -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 = A, 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,62 +248,62 @@ 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
|
-
const e = w(this.classValue) || {}, i = Object.keys(e), s = /* @__PURE__ */ Object.create(null),
|
|
266
|
+
const e = w(this.classValue) || {}, i = Object.keys(e), s = /* @__PURE__ */ Object.create(null), r = [];
|
|
267
267
|
for (let a = 0; a < i.length; a++) {
|
|
268
|
-
const l = i[a], c = e[l],
|
|
268
|
+
const l = i[a], c = e[l], h = Object.assign({}, c);
|
|
269
269
|
h.parent = t;
|
|
270
|
-
const
|
|
271
|
-
S(
|
|
270
|
+
const d = this.container.get(
|
|
271
|
+
S(h.inject),
|
|
272
272
|
h
|
|
273
273
|
);
|
|
274
|
-
|
|
274
|
+
d === void 0 && h.optional || (s[l] = d), r.push(h.binding);
|
|
275
275
|
}
|
|
276
|
-
return { properties: s, bindings:
|
|
276
|
+
return { properties: s, bindings: r };
|
|
277
277
|
}
|
|
278
278
|
};
|
|
279
279
|
// 类型到解析方法的静态映射表,用于替代 get 方法中的 if-else 链
|
|
280
|
-
|
|
281
|
-
[
|
|
282
|
-
[
|
|
283
|
-
[
|
|
280
|
+
o(C, "_resolvers", {
|
|
281
|
+
[p.INSTANCE]: "_resolveInstanceValue",
|
|
282
|
+
[p.CONSTANT]: "_resolveConstantValue",
|
|
283
|
+
[p.DYNAMIC]: "_resolveDynamicValue"
|
|
284
284
|
});
|
|
285
285
|
let m = C;
|
|
286
|
-
class x extends
|
|
286
|
+
class x extends T {
|
|
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 T {
|
|
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
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
298
|
+
o(this, "parent");
|
|
299
|
+
o(this, "children");
|
|
300
|
+
o(this, "_bindings", /* @__PURE__ */ new Map());
|
|
301
|
+
o(this, "_onActivationHandler");
|
|
302
|
+
o(this, "_onDeactivationHandler");
|
|
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 (r) {
|
|
352
|
+
return Promise.reject(r);
|
|
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) {
|
|
@@ -391,9 +397,9 @@ const I = class I {
|
|
|
391
397
|
// 同一对象可能通过 toConstantValue 被绑定到多个容器,WeakMap 只能保留最后一次映射,
|
|
392
398
|
// 会导致 @LazyInject 从错误的容器解析依赖。
|
|
393
399
|
// 由于 Instance 类型每次都通过 new ClassName() 创建新实例,不存在同一实例被多个容器注册的覆盖风险
|
|
394
|
-
|
|
395
|
-
let
|
|
396
|
-
class z extends
|
|
400
|
+
o(_, "_instanceContainerMap", /* @__PURE__ */ new WeakMap());
|
|
401
|
+
let I = _;
|
|
402
|
+
class z extends T {
|
|
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,35 +407,35 @@ class z extends N {
|
|
|
401
407
|
);
|
|
402
408
|
}
|
|
403
409
|
}
|
|
404
|
-
function
|
|
410
|
+
function y(n, t) {
|
|
405
411
|
return function(e) {
|
|
406
412
|
return function(i, s) {
|
|
407
|
-
const
|
|
408
|
-
|
|
413
|
+
const r = s.name, a = s.metadata;
|
|
414
|
+
v(a, u.INJECTED_PROPS) || (a[u.INJECTED_PROPS] = {});
|
|
409
415
|
const l = a[u.INJECTED_PROPS];
|
|
410
|
-
l[
|
|
416
|
+
l[r] || (l[r] = {}), l[r][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
|
-
const
|
|
417
|
-
if (
|
|
422
|
+
const r = s.name, a = s.metadata;
|
|
423
|
+
if (v(a, n))
|
|
418
424
|
throw new Error(t);
|
|
419
|
-
a[n] = { key:
|
|
425
|
+
a[n] = { key: r, value: e };
|
|
420
426
|
};
|
|
421
427
|
}
|
|
422
|
-
const
|
|
428
|
+
const $ = y(u.INJECT), Z = y(u.SELF, !0), q = y(u.SKIP_SELF, !0), W = y(u.OPTIONAL, !0), Q = j(
|
|
423
429
|
u.POST_CONSTRUCT,
|
|
424
430
|
g.POST_CONSTRUCT
|
|
425
|
-
),
|
|
431
|
+
), X = j(
|
|
426
432
|
u.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
|
+
O(n, e);
|
|
433
439
|
};
|
|
434
440
|
}
|
|
435
441
|
function U(n, t, e, i) {
|
|
@@ -440,18 +446,18 @@ function U(n, t, e, i) {
|
|
|
440
446
|
configurable: !0,
|
|
441
447
|
enumerable: !0,
|
|
442
448
|
get() {
|
|
443
|
-
if (!
|
|
444
|
-
const
|
|
445
|
-
if (!
|
|
449
|
+
if (!v(n, s)) {
|
|
450
|
+
const r = i || I.getContainerOf(n), a = n.constructor;
|
|
451
|
+
if (!r)
|
|
446
452
|
throw new z(S(e), a);
|
|
447
|
-
n[s] =
|
|
453
|
+
n[s] = r.get(S(e), {
|
|
448
454
|
parent: { token: a }
|
|
449
455
|
});
|
|
450
456
|
}
|
|
451
457
|
return n[s];
|
|
452
458
|
},
|
|
453
|
-
set(
|
|
454
|
-
n[s] =
|
|
459
|
+
set(r) {
|
|
460
|
+
n[s] = r;
|
|
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
483
|
const D = Symbol("decorate.metadata");
|
|
478
|
-
function
|
|
479
|
-
const i = Array.isArray(n) ? n : [n], s = t.prototype,
|
|
480
|
-
|
|
484
|
+
function it(n, t, e) {
|
|
485
|
+
const i = Array.isArray(n) ? n : [n], s = t.prototype, r = typeof s[e] == "function", a = [];
|
|
486
|
+
v(t, D) || (t[D] = {});
|
|
481
487
|
const l = t[D], c = {
|
|
482
|
-
kind:
|
|
488
|
+
kind: r ? "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
495
|
metadata: l
|
|
490
496
|
};
|
|
491
|
-
let
|
|
492
|
-
for (let
|
|
493
|
-
const
|
|
494
|
-
|
|
497
|
+
let h = r ? s[e] : void 0;
|
|
498
|
+
for (let d = i.length - 1; d >= 0; d--) {
|
|
499
|
+
const N = i[d](h, c);
|
|
500
|
+
r && typeof N == "function" && (h = N);
|
|
495
501
|
}
|
|
496
|
-
if (
|
|
497
|
-
const
|
|
498
|
-
for (const
|
|
499
|
-
|
|
502
|
+
if (r && h !== s[e] && (s[e] = h), O(t, l), 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
|
-
|
|
509
|
+
T as BaseError,
|
|
504
510
|
m 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
|
+
v 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.3",
|
|
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",
|