@kaokei/di 3.0.4 → 3.0.5

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 CHANGED
@@ -10,41 +10,11 @@
10
10
 
11
11
  </div>
12
12
 
13
- ## 简介
14
-
15
13
  本库是一个轻量级的依赖注入库,类似的其他比较流行的库有`InversifyJS`和`typedi`。
16
14
 
17
- 本库主要特点是参考借鉴了`InversifyJS`的优秀 API 设计,不依赖`reflect-metadata`,支持循环依赖。
15
+ 本库主要特点是参考借鉴了`InversifyJS`和`Angular`的优秀 API 设计,不依赖`reflect-metadata`,支持属性注入的循环依赖。
18
16
 
19
17
  - [入门指南](./docs/guide/README.md)
20
18
  - [API 文档](./docs/api/README.md)
21
19
  - [博客文章](./docs/note/01.什么是Token.md)
22
- - [online playground](https://codesandbox.io/s/di-playground-zjnyv)
23
- - [online demo](https://codesandbox.io/s/di-playground-zjnyv)
24
-
25
- ## todo
26
-
27
- 1. 实现 LazyInject 功能
28
- 关键是怎么实现 container 和 class 的绑定
29
- 正常 inject 时,是可以通过 container.get 获取上下文的,但是 lazyInject 拿不到这个上下文
30
-
31
- LazyInject是否需要@Self/@Optional/@SkipSelf来配合?
32
- 需要协调@LazyInject/@Inject,因为现在是两条并行的路线,也就是@Self/@Optional/@SkipSelf默认是和@Inject配合的,现在如果和@LazyInject就会报找不到@Inject的异常
33
- 可以作为低优先级的需求,后续有需要再考虑。
34
-
35
- 1. 直接使用javascript,不依赖typescript
36
-
37
- 1. getter支持缓存,使用computed支持缓存
38
-
39
- 应该在use-vue-service中实现
40
-
41
- https://yuanbao.tencent.com/bot/app/share/chat/tUbGmhHdY1Ta
42
-
43
- 2. 优化 https://github.com/kaokei/utils/blob/main/src/index.ts 的文档
44
-
45
-
46
- ```ts
47
- function useAppService<T>(token: interfaces.ServiceIdentifier<T>, app: any) {
48
- return app.runWithContext(() => useService(token));
49
- }
50
- ```
20
+ - [codesandbox example](https://codesandbox.io/s/di-playground-zjnyv)
@@ -1,14 +1,14 @@
1
- import { CommonToken, META_KEYS, META_KEY_INJECTED_PARAMS, META_KEY_INJECTED_PROPS, META_KEY_POST_CONSTRUCT, META_KEY_PRE_DESTROY, META_VALUES, META_VALUE_INJECTED_PARAMS, META_VALUE_INJECTED_PROPS, META_VALUE_POST_CONSTRUCT, META_VALUE_PRE_DESTROY } from './interfaces';
1
+ import { CommonToken, META_KEY_INJECTED_PARAMS, META_KEY_INJECTED_PROPS, META_KEY_POST_CONSTRUCT, META_KEY_PRE_DESTROY, META_VALUE_INJECTED_PARAMS, META_VALUE_INJECTED_PROPS, META_VALUE_POST_CONSTRUCT, META_VALUE_PRE_DESTROY } from './interfaces';
2
2
  export declare function defineMetadata(metadataKey: META_KEY_INJECTED_PARAMS, metadataValue: META_VALUE_INJECTED_PARAMS, target: CommonToken): void;
3
3
  export declare function defineMetadata(metadataKey: META_KEY_INJECTED_PROPS, metadataValue: META_VALUE_INJECTED_PROPS, target: CommonToken): void;
4
4
  export declare function defineMetadata(metadataKey: META_KEY_POST_CONSTRUCT, metadataValue: META_VALUE_POST_CONSTRUCT, target: CommonToken): void;
5
5
  export declare function defineMetadata(metadataKey: META_KEY_PRE_DESTROY, metadataValue: META_VALUE_PRE_DESTROY, target: CommonToken): void;
6
- export declare function defineMetadata(metadataKey: META_KEYS, metadataValue: META_VALUES, target: CommonToken): void;
6
+ export declare function defineMetadata(metadataKey: string, metadataValue: any, target: CommonToken): void;
7
7
  export declare function getOwnMetadata(metadataKey: META_KEY_INJECTED_PARAMS, target: CommonToken): META_VALUE_INJECTED_PARAMS | undefined;
8
8
  export declare function getOwnMetadata(metadataKey: META_KEY_INJECTED_PROPS, target: CommonToken): META_VALUE_INJECTED_PROPS | undefined;
9
9
  export declare function getOwnMetadata(metadataKey: META_KEY_POST_CONSTRUCT, target: CommonToken): META_VALUE_POST_CONSTRUCT | undefined;
10
10
  export declare function getOwnMetadata(metadataKey: META_KEY_PRE_DESTROY, target: CommonToken): META_VALUE_PRE_DESTROY | undefined;
11
- export declare function getOwnMetadata(metadataKey: META_KEYS, target: CommonToken): META_VALUES | undefined;
11
+ export declare function getOwnMetadata(metadataKey: string, target: CommonToken): any | undefined;
12
12
  /**
13
13
  * 使用hasParentClass判断当前target有没有父类
14
14
  * 如果没有父类直接使用getOwnMetadata获取数据
@@ -18,4 +18,4 @@ export declare function getOwnMetadata(metadataKey: META_KEYS, target: CommonTok
18
18
  export declare function getMetadata(metadataKey: META_KEY_INJECTED_PROPS, target: CommonToken): META_VALUE_INJECTED_PROPS | undefined;
19
19
  export declare function getMetadata(metadataKey: META_KEY_POST_CONSTRUCT, target: CommonToken): META_VALUE_POST_CONSTRUCT | undefined;
20
20
  export declare function getMetadata(metadataKey: META_KEY_PRE_DESTROY, target: CommonToken): META_VALUE_PRE_DESTROY | undefined;
21
- export declare function getMetadata(metadataKey: Exclude<META_KEYS, META_KEY_INJECTED_PARAMS>, target: CommonToken): Exclude<META_VALUES, META_VALUE_INJECTED_PARAMS> | undefined;
21
+ export declare function getMetadata(metadataKey: string, target: CommonToken): any | undefined;
@@ -1,14 +1,14 @@
1
- import { CommonToken, META_KEYS, META_KEY_INJECTED_PARAMS, META_KEY_INJECTED_PROPS, META_KEY_POST_CONSTRUCT, META_KEY_PRE_DESTROY, META_VALUES, META_VALUE_INJECTED_PARAMS, META_VALUE_INJECTED_PROPS, META_VALUE_POST_CONSTRUCT, META_VALUE_PRE_DESTROY } from './interfaces';
1
+ import { CommonToken, META_KEY_INJECTED_PARAMS, META_KEY_INJECTED_PROPS, META_KEY_POST_CONSTRUCT, META_KEY_PRE_DESTROY, META_VALUE_INJECTED_PARAMS, META_VALUE_INJECTED_PROPS, META_VALUE_POST_CONSTRUCT, META_VALUE_PRE_DESTROY } from './interfaces';
2
2
  export declare function defineMetadata(metadataKey: META_KEY_INJECTED_PARAMS, metadataValue: META_VALUE_INJECTED_PARAMS, target: CommonToken): void;
3
3
  export declare function defineMetadata(metadataKey: META_KEY_INJECTED_PROPS, metadataValue: META_VALUE_INJECTED_PROPS, target: CommonToken): void;
4
4
  export declare function defineMetadata(metadataKey: META_KEY_POST_CONSTRUCT, metadataValue: META_VALUE_POST_CONSTRUCT, target: CommonToken): void;
5
5
  export declare function defineMetadata(metadataKey: META_KEY_PRE_DESTROY, metadataValue: META_VALUE_PRE_DESTROY, target: CommonToken): void;
6
- export declare function defineMetadata(metadataKey: META_KEYS, metadataValue: META_VALUES, target: CommonToken): void;
6
+ export declare function defineMetadata(metadataKey: string, metadataValue: any, target: CommonToken): void;
7
7
  export declare function getOwnMetadata(metadataKey: META_KEY_INJECTED_PARAMS, target: CommonToken): META_VALUE_INJECTED_PARAMS | undefined;
8
8
  export declare function getOwnMetadata(metadataKey: META_KEY_INJECTED_PROPS, target: CommonToken): META_VALUE_INJECTED_PROPS | undefined;
9
9
  export declare function getOwnMetadata(metadataKey: META_KEY_POST_CONSTRUCT, target: CommonToken): META_VALUE_POST_CONSTRUCT | undefined;
10
10
  export declare function getOwnMetadata(metadataKey: META_KEY_PRE_DESTROY, target: CommonToken): META_VALUE_PRE_DESTROY | undefined;
11
- export declare function getOwnMetadata(metadataKey: META_KEYS, target: CommonToken): META_VALUES | undefined;
11
+ export declare function getOwnMetadata(metadataKey: string, target: CommonToken): any | undefined;
12
12
  /**
13
13
  * 使用hasParentClass判断当前target有没有父类
14
14
  * 如果没有父类直接使用getOwnMetadata获取数据
@@ -18,4 +18,4 @@ export declare function getOwnMetadata(metadataKey: META_KEYS, target: CommonTok
18
18
  export declare function getMetadata(metadataKey: META_KEY_INJECTED_PROPS, target: CommonToken): META_VALUE_INJECTED_PROPS | undefined;
19
19
  export declare function getMetadata(metadataKey: META_KEY_POST_CONSTRUCT, target: CommonToken): META_VALUE_POST_CONSTRUCT | undefined;
20
20
  export declare function getMetadata(metadataKey: META_KEY_PRE_DESTROY, target: CommonToken): META_VALUE_PRE_DESTROY | undefined;
21
- export declare function getMetadata(metadataKey: Exclude<META_KEYS, META_KEY_INJECTED_PARAMS>, target: CommonToken): Exclude<META_VALUES, META_VALUE_INJECTED_PARAMS> | undefined;
21
+ export declare function getMetadata(metadataKey: string, target: CommonToken): any | undefined;
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var w=Object.defineProperty;var _=(n,t,e)=>t in n?w(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var a=(n,t,e)=>_(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h={INJECTED_PARAMS:"injected:params",INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},p={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},d={Invalid:"Invalid",Instance:"Instance",ConstantValue:"ConstantValue",DynamicValue:"DynamicValue"},I={POST_CONSTRUCT:"Cannot apply @PostConstruct decorator multiple times in the same class.",PRE_DESTROY:"Cannot apply @PreDestroy decorator multiple times in the same class.",MISS_INJECT:"Expected a @Inject decorator to explicitly specify the token.",MISS_CONTAINER:"@LazyInject decorator cannot find the corresponding container."},P=Symbol(),S=new WeakMap;function j(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function N(n,t,e){const s=S.get(e)||{};s[n]=t,S.set(e,s)}function C(n,t){return(S.get(t)||{})[n]}function g(n,t){const e=C(n,t);if(!j(t))return e;const s=g(n,Object.getPrototypeOf(t));if(s||e)return{...s||{},...e||{}}}class B{constructor(t){a(this,"_","");a(this,"name");this.name=t}}class O{constructor(t){a(this,"callback");this.callback=t}resolve(){return this.callback()}}function v(n){if(!n)throw new Error(I.MISS_INJECT);return n instanceof O?n.resolve():n}class T extends Error{constructor(t,e){super(),this.name=this.constructor.name,this.message=`${t}${e==null?void 0:e.name}`}}class b extends T{constructor(t){super("");const e=[];let s=t;for(;s&&s.token;)e.push(s.token),s=s.parent;const i=e.reverse().map(o=>o.name).join(" --> ");this.message=`Circular dependency found: ${i}`}}class k extends T{constructor(t){super("Invalid binding: ",t)}}class M extends b{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}class L{constructor(t,e){a(this,"container");a(this,"context");a(this,"token");a(this,"type",d.Invalid);a(this,"status",p.DEFAULT);a(this,"classValue");a(this,"constantValue");a(this,"dynamicValue");a(this,"cache");a(this,"postConstructResult",P);a(this,"onActivationHandler");a(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=d.Instance,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=d.ConstantValue,this.constantValue=t,this}toDynamicValue(t){return this.type=d.DynamicValue,this.dynamicValue=t,this}toService(t){return this.toDynamicValue(e=>e.container.get(t,{parent:{token:this.token}}))}get(t){if(p.INITING===this.status)throw new b(t);if(p.ACTIVATED===this.status)return this.cache;if(d.Instance===this.type)return this.resolveInstanceValue(t);if(d.ConstantValue===this.type)return this.resolveConstantValue();if(d.DynamicValue===this.type)return this.resolveDynamicValue();throw new k(this.token)}getAwaitBindings(t,e){return e===!0?t:Array.isArray(e)?t.filter(s=>e.includes(s.token)):typeof e=="function"?t.filter(e):[]}postConstruct(t,e,s){if(d.Instance===this.type){const{key:i,value:o}=g(h.POST_CONSTRUCT,this.classValue)||{};if(i)if(o){const r=[...e,...s].filter(u=>d.Instance===(u==null?void 0:u.type)),c=this.getAwaitBindings(r,o);for(const u of c)if(u&&u.postConstructResult===P)throw new M({token:u.token,parent:t});const l=c.map(u=>u.postConstructResult);this.postConstructResult=Promise.all(l).then(()=>this.execute(i))}else this.postConstructResult=this.execute(i);else this.postConstructResult=void 0}}preDestroy(){if(d.Instance===this.type){const{key:t}=g(h.PRE_DESTROY,this.classValue)||{};if(t)return this.execute(t)}this.container=null,this.context=null}execute(t){const e=this.cache[t];return e==null?void 0:e.call(this.cache)}resolveInstanceValue(t){this.status=p.INITING;const e=this.classValue,[s,i]=this.getConstructorParameters(t),o=new e(...s);this.cache=this.activate(o),this.status=p.ACTIVATED,m.set(this.cache,this.container);const[r,c]=this.getInjectProperties(t);return Object.assign(this.cache,r),this.postConstruct(t,i,c),this.cache}resolveConstantValue(){return this.status=p.INITING,this.cache=this.activate(this.constantValue),this.status=p.ACTIVATED,this.cache}resolveDynamicValue(){this.status=p.INITING;const t=this.dynamicValue.call(this,this.context);return this.cache=this.activate(t),this.status=p.ACTIVATED,this.cache}getConstructorParameters(t){const e=C(h.INJECTED_PARAMS,this.classValue)||[],s=[],i=[];for(let o=0;o<e.length;o++){const r=e[o],{inject:c,...l}=r;l.parent=t;const u=this.container.get(v(c),l);s.push(u),i.push(l.binding)}return[s,i]}getInjectProperties(t){const e=g(h.INJECTED_PROPS,this.classValue)||{},s=Object.keys(e),i=Object.create(null),o=[];for(let r=0;r<s.length;r++){const c=s[r],l=e[c],{inject:u,...f}=l;f.parent=t;const y=this.container.get(v(u),f);y===void 0&&l.optional||(i[c]=y),o.push(f.binding)}return[i,o]}}class x extends T{constructor(t){super("No matching binding found for token: ",t)}}class H extends T{constructor(t){super("Cannot bind token multiple times: ",t)}}class D{constructor(){a(this,"parent");a(this,"bindings",new Map);a(this,"onActivationHandler");a(this,"onDeactivationHandler")}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.preDestroy(),this.bindings.delete(t)}}unbindAll(){this.bindings.forEach(t=>{this.unbind(t.token)})}isCurrentBound(t){return this.bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new D;return t.parent=this,t}get(t,e={}){const s=this.getBinding(t);if(e.skipSelf){if(this.parent)return e.skipSelf=!1,this.parent.get(t,e)}else if(e.self||s){if(s)return e.token=t,e.binding=s,s.get(e)}else if(this.parent)return this.parent.get(t,e);return 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),t.deactivate()}buildBinding(t){return new L(t,this)}getBinding(t){return this.bindings.get(t)}checkBindingNotFoundError(t,e){if(!e.optional)throw new x(t)}}const m=new WeakMap;function E(n,t){return function(e){return function(s,i,o){const r=typeof o=="number",c=r?s:s.constructor,l=r?o:i,u=r?h.INJECTED_PARAMS:h.INJECTED_PROPS,f=r?C(u,c)||[]:g(u,c)||{},y=f[l]||{};y[n]=e===void 0?t:e,f[l]=y,N(u,f,c)}}}function V(n,t){return e=>(s,i)=>{if(C(n,s.constructor))throw new Error(t);N(n,{key:i,value:e},s.constructor)}}const F=E(h.INJECT),J=E(h.SELF,!0),U=E(h.SKIP_SELF,!0),z=E(h.OPTIONAL,!0),G=V(h.POST_CONSTRUCT,I.POST_CONSTRUCT),Y=V(h.PRE_DESTROY,I.PRE_DESTROY);function A(n,t,e,s){for(let i=n.length-1;i>=0;i--)n[i](t,e,s)}function K(n,t,e){n=Array.isArray(n)?n:[n],typeof e=="number"?A(n,t,void 0,e):typeof e=="string"&&A(n,t.prototype,e)}function $(n,t,e,s){function i(){const r=Symbol.for(t);if(!this.hasOwnProperty(r)){const c=s||m.get(this),l=this.constructor;if(!c)throw new Error(`${I.MISS_CONTAINER} ${l.name}`);this[r]=c.get(v(e),{parent:{token:l}})}return this[r]}function o(r){const c=Symbol.for(t);this[c]=r}Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get:i,set:o})}function R(n,t){return function(e,s){$(e,s,n,t)}}function W(n){return function(t){return R(t,n)}}exports.CONTAINER_MAP=m;exports.Container=D;exports.Inject=F;exports.LazyInject=R;exports.LazyToken=O;exports.Optional=z;exports.PostConstruct=G;exports.PreDestroy=Y;exports.Self=J;exports.SkipSelf=U;exports.Token=B;exports.createLazyInject=W;exports.decorate=K;
1
+ "use strict";var w=Object.defineProperty;var _=(n,t,e)=>t in n?w(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var a=(n,t,e)=>_(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h={INJECTED_PARAMS:"injected:params",INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},p={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},d={Invalid:"Invalid",Instance:"Instance",ConstantValue:"ConstantValue",DynamicValue:"DynamicValue"},C={POST_CONSTRUCT:"Cannot apply @PostConstruct decorator multiple times in the same class.",PRE_DESTROY:"Cannot apply @PreDestroy decorator multiple times in the same class.",MISS_INJECT:"Expected a @Inject decorator to explicitly specify the token.",MISS_CONTAINER:"@LazyInject decorator cannot find the corresponding container."},A=Symbol(),S=new WeakMap;function M(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function D(n,t,e){const s=S.get(e)||{};s[n]=t,S.set(e,s)}function I(n,t){return(S.get(t)||{})[n]}function g(n,t){const e=I(n,t);if(!M(t))return e;const s=g(n,Object.getPrototypeOf(t));if(s||e)return{...s||{},...e||{}}}class j{constructor(t){a(this,"_","");a(this,"name");this.name=t}}class O{constructor(t){a(this,"callback");this.callback=t}resolve(){return this.callback()}}function v(n){if(!n)throw new Error(C.MISS_INJECT);return n instanceof O?n.resolve():n}class T extends Error{constructor(t,e){super(),this.name=this.constructor.name,this.message=`${t}${e==null?void 0:e.name}`}}class b extends T{constructor(t){super("");const e=[];let s=t;for(;s&&s.token;)e.push(s.token),s=s.parent;const i=e.reverse().map(o=>o.name).join(" --> ");this.message=`Circular dependency found: ${i}`}}class B extends T{constructor(t){super("Invalid binding: ",t)}}class k extends b{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}class L{constructor(t,e){a(this,"container");a(this,"context");a(this,"token");a(this,"type",d.Invalid);a(this,"status",p.DEFAULT);a(this,"classValue");a(this,"constantValue");a(this,"dynamicValue");a(this,"cache");a(this,"postConstructResult",A);a(this,"onActivationHandler");a(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=d.Instance,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=d.ConstantValue,this.constantValue=t,this}toDynamicValue(t){return this.type=d.DynamicValue,this.dynamicValue=t,this}toService(t){return this.toDynamicValue(e=>e.container.get(t,{parent:{token:this.token}}))}get(t){if(p.INITING===this.status)throw new b(t);if(p.ACTIVATED===this.status)return this.cache;if(d.Instance===this.type)return this.resolveInstanceValue(t);if(d.ConstantValue===this.type)return this.resolveConstantValue();if(d.DynamicValue===this.type)return this.resolveDynamicValue();throw new B(this.token)}getAwaitBindings(t,e){return e===!0?t:Array.isArray(e)?t.filter(s=>e.includes(s.token)):typeof e=="function"?t.filter(e):[]}postConstruct(t,e,s){if(d.Instance===this.type){const{key:i,value:o}=g(h.POST_CONSTRUCT,this.classValue)||{};if(i)if(o){const r=[...e,...s].filter(u=>d.Instance===(u==null?void 0:u.type)),c=this.getAwaitBindings(r,o);for(const u of c)if(u&&u.postConstructResult===A)throw new k({token:u.token,parent:t});const l=c.map(u=>u.postConstructResult);this.postConstructResult=Promise.all(l).then(()=>this.execute(i))}else this.postConstructResult=this.execute(i);else this.postConstructResult=void 0}}preDestroy(){if(d.Instance===this.type){const{key:t}=g(h.PRE_DESTROY,this.classValue)||{};if(t)return this.execute(t)}this.container=null,this.context=null}execute(t){const e=this.cache[t];return e==null?void 0:e.call(this.cache)}resolveInstanceValue(t){this.status=p.INITING;const e=this.classValue,[s,i]=this.getConstructorParameters(t),o=new e(...s);this.cache=this.activate(o),this.status=p.ACTIVATED,P.set(this.cache,this.container);const[r,c]=this.getInjectProperties(t);return Object.assign(this.cache,r),this.postConstruct(t,i,c),this.cache}resolveConstantValue(){return this.status=p.INITING,this.cache=this.activate(this.constantValue),this.status=p.ACTIVATED,this.cache}resolveDynamicValue(){this.status=p.INITING;const t=this.dynamicValue.call(this,this.context);return this.cache=this.activate(t),this.status=p.ACTIVATED,this.cache}getConstructorParameters(t){const e=I(h.INJECTED_PARAMS,this.classValue)||[],s=[],i=[];for(let o=0;o<e.length;o++){const r=e[o],{inject:c,...l}=r;l.parent=t;const u=this.container.get(v(c),l);s.push(u),i.push(l.binding)}return[s,i]}getInjectProperties(t){const e=g(h.INJECTED_PROPS,this.classValue)||{},s=Object.keys(e),i=Object.create(null),o=[];for(let r=0;r<s.length;r++){const c=s[r],l=e[c],{inject:u,...f}=l;f.parent=t;const y=this.container.get(v(u),f);y===void 0&&l.optional||(i[c]=y),o.push(f.binding)}return[i,o]}}class x extends T{constructor(t){super("No matching binding found for token: ",t)}}class H extends T{constructor(t){super("Cannot bind token multiple times: ",t)}}class m{constructor(){a(this,"parent");a(this,"bindings",new Map);a(this,"onActivationHandler");a(this,"onDeactivationHandler")}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.preDestroy(),this.bindings.delete(t)}}unbindAll(){this.bindings.forEach(t=>{this.unbind(t.token)})}isCurrentBound(t){return this.bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new m;return t.parent=this,t}get(t,e={}){const s=this.getBinding(t);if(e.skipSelf){if(this.parent)return e.skipSelf=!1,this.parent.get(t,e)}else if(e.self||s){if(s)return e.token=t,e.binding=s,s.get(e)}else if(this.parent)return this.parent.get(t,e);return 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),t.deactivate()}buildBinding(t){return new L(t,this)}getBinding(t){return this.bindings.get(t)}checkBindingNotFoundError(t,e){if(!e.optional)throw new x(t)}}const P=new WeakMap;function E(n,t){return function(e){return function(s,i,o){const r=typeof o=="number",c=r?s:s.constructor,l=r?o:i,u=r?h.INJECTED_PARAMS:h.INJECTED_PROPS,f=r?I(u,c)||[]:g(u,c)||{},y=f[l]||{};y[n]=e===void 0?t:e,f[l]=y,D(u,f,c)}}}function V(n,t){return e=>(s,i)=>{if(I(n,s.constructor))throw new Error(t);D(n,{key:i,value:e},s.constructor)}}const F=E(h.INJECT),J=E(h.SELF,!0),U=E(h.SKIP_SELF,!0),z=E(h.OPTIONAL,!0),G=V(h.POST_CONSTRUCT,C.POST_CONSTRUCT),Y=V(h.PRE_DESTROY,C.PRE_DESTROY);function N(n,t,e,s){for(let i=n.length-1;i>=0;i--)n[i](t,e,s)}function K(n,t,e){n=Array.isArray(n)?n:[n],typeof e=="number"?N(n,t,void 0,e):typeof e=="string"&&N(n,t.prototype,e)}function $(n,t,e,s){function i(){const r=Symbol.for(t);if(!this.hasOwnProperty(r)){const c=s||P.get(this),l=this.constructor;if(!c)throw new Error(`${C.MISS_CONTAINER} ${l.name}`);this[r]=c.get(v(e),{parent:{token:l}})}return this[r]}function o(r){const c=Symbol.for(t);this[c]=r}Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get:i,set:o})}function R(n,t){return function(e,s){$(e,s,n,t)}}function W(n){return function(t){return R(t,n)}}exports.CONTAINER_MAP=P;exports.Container=m;exports.Inject=F;exports.LazyInject=R;exports.LazyToken=O;exports.Optional=z;exports.PostConstruct=G;exports.PreDestroy=Y;exports.Self=J;exports.SkipSelf=U;exports.Token=j;exports.createLazyInject=W;exports.decorate=K;exports.defineMetadata=D;exports.getMetadata=g;exports.getOwnMetadata=I;
package/dist/index.d.cts CHANGED
@@ -3,3 +3,4 @@ export { Container, CONTAINER_MAP } from './container';
3
3
  export { Token, LazyToken } from './token';
4
4
  export { Inject, Self, SkipSelf, Optional, PostConstruct, PreDestroy, decorate, } from './decorator';
5
5
  export { LazyInject, createLazyInject } from './lazyinject';
6
+ export { defineMetadata, getOwnMetadata, getMetadata } from './cachemap';
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export { Container, CONTAINER_MAP } from './container';
3
3
  export { Token, LazyToken } from './token';
4
4
  export { Inject, Self, SkipSelf, Optional, PostConstruct, PreDestroy, decorate, } from './decorator';
5
5
  export { LazyInject, createLazyInject } from './lazyinject';
6
+ export { defineMetadata, getOwnMetadata, getMetadata } from './cachemap';
package/dist/index.js CHANGED
@@ -44,7 +44,10 @@ function I(e, t) {
44
44
  return (v.get(t) || {})[e];
45
45
  }
46
46
  function y(e, t) {
47
- const n = I(e, t);
47
+ const n = I(
48
+ e,
49
+ t
50
+ );
48
51
  if (!w(t))
49
52
  return n;
50
53
  const s = y(
@@ -398,5 +401,8 @@ export {
398
401
  Y as SkipSelf,
399
402
  J as Token,
400
403
  q as createLazyInject,
401
- W as decorate
404
+ W as decorate,
405
+ A as defineMetadata,
406
+ y as getMetadata,
407
+ I as getOwnMetadata
402
408
  };
@@ -45,4 +45,5 @@ export interface CacheMapValue {
45
45
  [KEYS.INJECTED_PROPS]: Record<string, Options>;
46
46
  [KEYS.POST_CONSTRUCT]: KV<PostConstructParam>;
47
47
  [KEYS.PRE_DESTROY]: KV<void>;
48
+ [key: string]: any;
48
49
  }
@@ -45,4 +45,5 @@ export interface CacheMapValue {
45
45
  [KEYS.INJECTED_PROPS]: Record<string, Options>;
46
46
  [KEYS.POST_CONSTRUCT]: KV<PostConstructParam>;
47
47
  [KEYS.PRE_DESTROY]: KV<void>;
48
+ [key: string]: any;
48
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaokei/di",
3
- "version": "3.0.4",
3
+ "version": "3.0.5",
4
4
  "type": "module",
5
5
  "description": "Tiny di library depends on typescript decorator.",
6
6
  "main": "./dist/index.cjs",