@kaokei/di 3.0.4 → 3.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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;
@@ -2,6 +2,7 @@ import { Binding } from './binding';
2
2
  import { Options, CommonToken, ActivationHandler, DeactivationHandler } from './interfaces';
3
3
  export declare class Container {
4
4
  parent?: Container;
5
+ children?: Set<Container>;
5
6
  private bindings;
6
7
  private onActivationHandler?;
7
8
  private onDeactivationHandler?;
@@ -11,6 +12,7 @@ export declare class Container {
11
12
  isCurrentBound<T>(token: CommonToken<T>): boolean;
12
13
  isBound<T>(token: CommonToken<T>): boolean;
13
14
  createChild(): Container;
15
+ destroy(): void;
14
16
  get<T>(token: CommonToken<T>, options: Options<T> & {
15
17
  optional: true;
16
18
  }): T | void;
@@ -2,6 +2,7 @@ import { Binding } from './binding';
2
2
  import { Options, CommonToken, ActivationHandler, DeactivationHandler } from './interfaces';
3
3
  export declare class Container {
4
4
  parent?: Container;
5
+ children?: Set<Container>;
5
6
  private bindings;
6
7
  private onActivationHandler?;
7
8
  private onDeactivationHandler?;
@@ -11,6 +12,7 @@ export declare class Container {
11
12
  isCurrentBound<T>(token: CommonToken<T>): boolean;
12
13
  isBound<T>(token: CommonToken<T>): boolean;
13
14
  createChild(): Container;
15
+ destroy(): void;
14
16
  get<T>(token: CommonToken<T>, options: Options<T> & {
15
17
  optional: true;
16
18
  }): T | void;
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 r=(n,t,e)=>_(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l={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."},P=Symbol(),v=new WeakMap;function M(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function D(n,t,e){const s=v.get(e)||{};s[n]=t,v.set(e,s)}function I(n,t){return(v.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){r(this,"_","");r(this,"name");this.name=t}}class N{constructor(t){r(this,"callback");this.callback=t}resolve(){return this.callback()}}function S(n){if(!n)throw new Error(C.MISS_INJECT);return n instanceof N?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 O 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 O{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}class L{constructor(t,e){r(this,"container");r(this,"context");r(this,"token");r(this,"type",d.Invalid);r(this,"status",p.DEFAULT);r(this,"classValue");r(this,"constantValue");r(this,"dynamicValue");r(this,"cache");r(this,"postConstructResult",P);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=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 O(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(l.POST_CONSTRUCT,this.classValue)||{};if(i)if(o){const a=[...e,...s].filter(u=>d.Instance===(u==null?void 0:u.type)),c=this.getAwaitBindings(a,o);for(const u of c)if(u&&u.postConstructResult===P)throw new k({token:u.token,parent:t});const h=c.map(u=>u.postConstructResult);this.postConstructResult=Promise.all(h).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(l.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,A.set(this.cache,this.container);const[a,c]=this.getInjectProperties(t);return Object.assign(this.cache,a),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(l.INJECTED_PARAMS,this.classValue)||[],s=[],i=[];for(let o=0;o<e.length;o++){const a=e[o],{inject:c,...h}=a;h.parent=t;const u=this.container.get(S(c),h);s.push(u),i.push(h.binding)}return[s,i]}getInjectProperties(t){const e=g(l.INJECTED_PROPS,this.classValue)||{},s=Object.keys(e),i=Object.create(null),o=[];for(let a=0;a<s.length;a++){const c=s[a],h=e[c],{inject:u,...f}=h;f.parent=t;const y=this.container.get(S(u),f);y===void 0&&h.optional||(i[c]=y),o.push(f.binding)}return[i,o]}}class H extends T{constructor(t){super("No matching binding found for token: ",t)}}class x extends T{constructor(t){super("Cannot bind token multiple times: ",t)}}class m{constructor(){r(this,"parent");r(this,"children");r(this,"bindings",new Map);r(this,"onActivationHandler");r(this,"onDeactivationHandler")}bind(t){if(this.bindings.has(t))throw new x(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,this.children||(this.children=new Set),this.children.add(t),t}destroy(){var t,e,s;(e=(t=this.parent)==null?void 0:t.children)==null||e.delete(this),this.unbindAll(),this.bindings.clear(),this.parent=void 0,(s=this.children)==null||s.clear(),this.children=void 0,this.onActivationHandler=void 0,this.onDeactivationHandler=void 0}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 H(t)}}const A=new WeakMap;function E(n,t){return function(e){return function(s,i,o){const a=typeof o=="number",c=a?s:s.constructor,h=a?o:i,u=a?l.INJECTED_PARAMS:l.INJECTED_PROPS,f=a?I(u,c)||[]:g(u,c)||{},y=f[h]||{};y[n]=e===void 0?t:e,f[h]=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(l.INJECT),J=E(l.SELF,!0),U=E(l.SKIP_SELF,!0),z=E(l.OPTIONAL,!0),G=V(l.POST_CONSTRUCT,C.POST_CONSTRUCT),Y=V(l.PRE_DESTROY,C.PRE_DESTROY);function b(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"?b(n,t,void 0,e):typeof e=="string"&&b(n,t.prototype,e)}function $(n,t,e,s){function i(){const a=Symbol.for(t);if(!this.hasOwnProperty(a)){const c=s||A.get(this),h=this.constructor;if(!c)throw new Error(`${C.MISS_CONTAINER} ${h.name}`);this[a]=c.get(S(e),{parent:{token:h}})}return this[a]}function o(a){const c=Symbol.for(t);this[c]=a}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=A;exports.Container=m;exports.Inject=F;exports.LazyInject=R;exports.LazyToken=N;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
@@ -1,6 +1,6 @@
1
1
  var V = Object.defineProperty;
2
2
  var R = (e, t, n) => t in e ? V(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var a = (e, t, n) => R(e, typeof t != "symbol" ? t + "" : t, n);
3
+ var r = (e, t, n) => R(e, typeof t != "symbol" ? t + "" : t, n);
4
4
  const l = {
5
5
  // 记录构造函数参数装饰器对应的数据的key
6
6
  INJECTED_PARAMS: "injected:params",
@@ -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(
@@ -59,14 +62,14 @@ function y(e, t) {
59
62
  }
60
63
  class J {
61
64
  constructor(t) {
62
- a(this, "_", "");
63
- a(this, "name");
65
+ r(this, "_", "");
66
+ r(this, "name");
64
67
  this.name = t;
65
68
  }
66
69
  }
67
70
  class _ {
68
71
  constructor(t) {
69
- a(this, "callback");
72
+ r(this, "callback");
70
73
  this.callback = t;
71
74
  }
72
75
  resolve() {
@@ -106,18 +109,18 @@ class j extends P {
106
109
  }
107
110
  class M {
108
111
  constructor(t, n) {
109
- a(this, "container");
110
- a(this, "context");
111
- a(this, "token");
112
- a(this, "type", d.Invalid);
113
- a(this, "status", p.DEFAULT);
114
- a(this, "classValue");
115
- a(this, "constantValue");
116
- a(this, "dynamicValue");
117
- a(this, "cache");
118
- a(this, "postConstructResult", S);
119
- a(this, "onActivationHandler");
120
- a(this, "onDeactivationHandler");
112
+ r(this, "container");
113
+ r(this, "context");
114
+ r(this, "token");
115
+ r(this, "type", d.Invalid);
116
+ r(this, "status", p.DEFAULT);
117
+ r(this, "classValue");
118
+ r(this, "constantValue");
119
+ r(this, "dynamicValue");
120
+ r(this, "cache");
121
+ r(this, "postConstructResult", S);
122
+ r(this, "onActivationHandler");
123
+ r(this, "onDeactivationHandler");
121
124
  this.container = n, this.context = { container: this.container }, this.token = t;
122
125
  }
123
126
  onActivation(t) {
@@ -171,9 +174,9 @@ class M {
171
174
  const { key: i, value: o } = y(l.POST_CONSTRUCT, this.classValue) || {};
172
175
  if (i)
173
176
  if (o) {
174
- const r = [...n, ...s].filter(
177
+ const a = [...n, ...s].filter(
175
178
  (u) => d.Instance === (u == null ? void 0 : u.type)
176
- ), c = this.getAwaitBindings(r, o);
179
+ ), c = this.getAwaitBindings(a, o);
177
180
  for (const u of c)
178
181
  if (u && u.postConstructResult === S)
179
182
  throw new j({
@@ -205,9 +208,9 @@ class M {
205
208
  resolveInstanceValue(t) {
206
209
  this.status = p.INITING;
207
210
  const n = this.classValue, [s, i] = this.getConstructorParameters(t), o = new n(...s);
208
- this.cache = this.activate(o), this.status = p.ACTIVATED, b.set(this.cache, this.container);
209
- const [r, c] = this.getInjectProperties(t);
210
- return Object.assign(this.cache, r), this.postConstruct(t, i, c), this.cache;
211
+ this.cache = this.activate(o), this.status = p.ACTIVATED, N.set(this.cache, this.container);
212
+ const [a, c] = this.getInjectProperties(t);
213
+ return Object.assign(this.cache, a), this.postConstruct(t, i, c), this.cache;
211
214
  }
212
215
  resolveConstantValue() {
213
216
  return this.status = p.INITING, this.cache = this.activate(this.constantValue), this.status = p.ACTIVATED, this.cache;
@@ -220,7 +223,7 @@ class M {
220
223
  getConstructorParameters(t) {
221
224
  const n = I(l.INJECTED_PARAMS, this.classValue) || [], s = [], i = [];
222
225
  for (let o = 0; o < n.length; o++) {
223
- const r = n[o], { inject: c, ...h } = r;
226
+ const a = n[o], { inject: c, ...h } = a;
224
227
  h.parent = t;
225
228
  const u = this.container.get(D(c), h);
226
229
  s.push(u), i.push(h.binding);
@@ -229,8 +232,8 @@ class M {
229
232
  }
230
233
  getInjectProperties(t) {
231
234
  const n = y(l.INJECTED_PROPS, this.classValue) || {}, s = Object.keys(n), i = /* @__PURE__ */ Object.create(null), o = [];
232
- for (let r = 0; r < s.length; r++) {
233
- const c = s[r], h = n[c], { inject: u, ...f } = h;
235
+ for (let a = 0; a < s.length; a++) {
236
+ const c = s[a], h = n[c], { inject: u, ...f } = h;
234
237
  f.parent = t;
235
238
  const g = this.container.get(D(u), f);
236
239
  g === void 0 && h.optional || (i[c] = g), o.push(f.binding);
@@ -248,12 +251,13 @@ class x extends E {
248
251
  super("Cannot bind token multiple times: ", t);
249
252
  }
250
253
  }
251
- class N {
254
+ class b {
252
255
  constructor() {
253
- a(this, "parent");
254
- a(this, "bindings", /* @__PURE__ */ new Map());
255
- a(this, "onActivationHandler");
256
- a(this, "onDeactivationHandler");
256
+ r(this, "parent");
257
+ r(this, "children");
258
+ r(this, "bindings", /* @__PURE__ */ new Map());
259
+ r(this, "onActivationHandler");
260
+ r(this, "onDeactivationHandler");
257
261
  }
258
262
  bind(t) {
259
263
  if (this.bindings.has(t))
@@ -279,8 +283,12 @@ class N {
279
283
  return this.isCurrentBound(t) || !!this.parent && this.parent.isBound(t);
280
284
  }
281
285
  createChild() {
282
- const t = new N();
283
- return t.parent = this, t;
286
+ const t = new b();
287
+ return t.parent = this, this.children || (this.children = /* @__PURE__ */ new Set()), this.children.add(t), t;
288
+ }
289
+ destroy() {
290
+ var t, n, s;
291
+ (n = (t = this.parent) == null ? void 0 : t.children) == null || n.delete(this), this.unbindAll(), this.bindings.clear(), this.parent = void 0, (s = this.children) == null || s.clear(), this.children = void 0, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
284
292
  }
285
293
  get(t, n = {}) {
286
294
  const s = this.getBinding(t);
@@ -317,11 +325,11 @@ class N {
317
325
  throw new k(t);
318
326
  }
319
327
  }
320
- const b = /* @__PURE__ */ new WeakMap();
328
+ const N = /* @__PURE__ */ new WeakMap();
321
329
  function T(e, t) {
322
330
  return function(n) {
323
331
  return function(s, i, o) {
324
- const r = typeof o == "number", c = r ? s : s.constructor, h = r ? o : i, u = r ? l.INJECTED_PARAMS : l.INJECTED_PROPS, f = r ? I(u, c) || [] : y(u, c) || {}, g = f[h] || {};
332
+ const a = typeof o == "number", c = a ? s : s.constructor, h = a ? o : i, u = a ? l.INJECTED_PARAMS : l.INJECTED_PROPS, f = a ? I(u, c) || [] : y(u, c) || {}, g = f[h] || {};
325
333
  g[e] = n === void 0 ? t : n, f[h] = g, A(u, f, c);
326
334
  };
327
335
  };
@@ -353,20 +361,20 @@ function W(e, t, n) {
353
361
  }
354
362
  function H(e, t, n, s) {
355
363
  function i() {
356
- const r = Symbol.for(t);
357
- if (!this.hasOwnProperty(r)) {
358
- const c = s || b.get(this), h = this.constructor;
364
+ const a = Symbol.for(t);
365
+ if (!this.hasOwnProperty(a)) {
366
+ const c = s || N.get(this), h = this.constructor;
359
367
  if (!c)
360
368
  throw new Error(`${C.MISS_CONTAINER} ${h.name}`);
361
- this[r] = c.get(D(n), {
369
+ this[a] = c.get(D(n), {
362
370
  parent: { token: h }
363
371
  });
364
372
  }
365
- return this[r];
373
+ return this[a];
366
374
  }
367
- function o(r) {
375
+ function o(a) {
368
376
  const c = Symbol.for(t);
369
- this[c] = r;
377
+ this[c] = a;
370
378
  }
371
379
  Object.defineProperty(e, t, {
372
380
  configurable: !0,
@@ -386,8 +394,8 @@ function q(e) {
386
394
  };
387
395
  }
388
396
  export {
389
- b as CONTAINER_MAP,
390
- N as Container,
397
+ N as CONTAINER_MAP,
398
+ b as Container,
391
399
  U as Inject,
392
400
  L as LazyInject,
393
401
  _ as LazyToken,
@@ -398,5 +406,8 @@ export {
398
406
  Y as SkipSelf,
399
407
  J as Token,
400
408
  q as createLazyInject,
401
- W as decorate
409
+ W as decorate,
410
+ A as defineMetadata,
411
+ y as getMetadata,
412
+ I as getOwnMetadata
402
413
  };
@@ -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.6",
4
4
  "type": "module",
5
5
  "description": "Tiny di library depends on typescript decorator.",
6
6
  "main": "./dist/index.cjs",