@kaokei/di 5.0.8 → 5.0.9

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.
@@ -29,7 +29,7 @@ export declare const ERRORS: {
29
29
  readonly PRE_DESTROY: "Multiple @PreDestroy decorators are not allowed in a single class.";
30
30
  readonly INVALID_TOKEN: "@Inject requires a valid token, but received null or undefined.";
31
31
  readonly LAZY_INJECT_INVALID_TOKEN: "@LazyInject requires a valid token, but received null or undefined.";
32
- readonly DECORATE_NOT_SUPPORT_INITIALIZER: "decorate does not support decorators that rely on context.addInitializer (e.g. @LazyInject, @autobind).";
32
+ readonly DECORATE_NOT_SUPPORT_INITIALIZER: "decorate does not support decorators that rely on context.addInitializer (e.g. @LazyInject).";
33
33
  };
34
34
  export declare const UNINITIALIZED: unique symbol;
35
35
  export type BindingType = (typeof BINDING)[keyof typeof BINDING];
@@ -29,7 +29,7 @@ export declare const ERRORS: {
29
29
  readonly PRE_DESTROY: "Multiple @PreDestroy decorators are not allowed in a single class.";
30
30
  readonly INVALID_TOKEN: "@Inject requires a valid token, but received null or undefined.";
31
31
  readonly LAZY_INJECT_INVALID_TOKEN: "@LazyInject requires a valid token, but received null or undefined.";
32
- readonly DECORATE_NOT_SUPPORT_INITIALIZER: "decorate does not support decorators that rely on context.addInitializer (e.g. @LazyInject, @autobind).";
32
+ readonly DECORATE_NOT_SUPPORT_INITIALIZER: "decorate does not support decorators that rely on context.addInitializer (e.g. @LazyInject).";
33
33
  };
34
34
  export declare const UNINITIALIZED: unique symbol;
35
35
  export type BindingType = (typeof BINDING)[keyof typeof BINDING];
@@ -1,5 +1,15 @@
1
1
  import { Binding } from './binding';
2
2
  import { GetOptions, Options, CommonToken, ActivationHandler, DeactivationHandler } from './interfaces';
3
+ /**
4
+ * 依赖注入容器。
5
+ *
6
+ * @example 基本用法
7
+ * ```typescript
8
+ * const container = new Container();
9
+ * container.bind(MyService).toSelf();
10
+ * const myService = container.get(MyService);
11
+ * ```
12
+ */
3
13
  export declare class Container {
4
14
  static _instanceContainerMap: WeakMap<object, Container>;
5
15
  static getContainerOf(instance: object): Container | undefined;
@@ -1,5 +1,15 @@
1
1
  import { Binding } from './binding';
2
2
  import { GetOptions, Options, CommonToken, ActivationHandler, DeactivationHandler } from './interfaces';
3
+ /**
4
+ * 依赖注入容器。
5
+ *
6
+ * @example 基本用法
7
+ * ```typescript
8
+ * const container = new Container();
9
+ * container.bind(MyService).toSelf();
10
+ * const myService = container.get(MyService);
11
+ * ```
12
+ */
3
13
  export declare class Container {
4
14
  static _instanceContainerMap: WeakMap<object, Container>;
5
15
  static getContainerOf(instance: object): Container | undefined;
@@ -41,18 +41,5 @@ export declare function LazyInject<T>(token: GenericToken<T>, container?: Contai
41
41
  * 创建绑定到指定容器的延迟注入装饰器工厂。
42
42
  */
43
43
  export declare function createLazyInject(container: Container): <T>(token: GenericToken<T>) => (_value: undefined, context: ClassFieldDecoratorContext) => void;
44
- /**
45
- * 方法装饰器:自动绑定方法的 this 到实例(Stage 3 Method Decorator)
46
- *
47
- * 解决方法作为回调传递时丢失 this 的问题,例如:
48
- * - Vue 模板中 `@click="service.method"` 丢失 this
49
- * - `promise.then(service.method)` 丢失 this
50
- *
51
- * 通过 context.addInitializer 在实例创建时执行 bind,
52
- * 每个实例都会拥有自己的绑定版本,互不影响。
53
- *
54
- * 使用方式:@autobind(无参数,直接作为装饰器使用)
55
- */
56
- export declare function autobind<T extends (...args: any[]) => any>(value: T, context: ClassMethodDecoratorContext): void;
57
44
  export declare function decorate(decorator: any, target: any, key: string): void;
58
45
  export {};
@@ -41,18 +41,5 @@ export declare function LazyInject<T>(token: GenericToken<T>, container?: Contai
41
41
  * 创建绑定到指定容器的延迟注入装饰器工厂。
42
42
  */
43
43
  export declare function createLazyInject(container: Container): <T>(token: GenericToken<T>) => (_value: undefined, context: ClassFieldDecoratorContext) => void;
44
- /**
45
- * 方法装饰器:自动绑定方法的 this 到实例(Stage 3 Method Decorator)
46
- *
47
- * 解决方法作为回调传递时丢失 this 的问题,例如:
48
- * - Vue 模板中 `@click="service.method"` 丢失 this
49
- * - `promise.then(service.method)` 丢失 this
50
- *
51
- * 通过 context.addInitializer 在实例创建时执行 bind,
52
- * 每个实例都会拥有自己的绑定版本,互不影响。
53
- *
54
- * 使用方式:@autobind(无参数,直接作为装饰器使用)
55
- */
56
- export declare function autobind<T extends (...args: any[]) => any>(value: T, context: ClassMethodDecoratorContext): void;
57
44
  export declare function decorate(decorator: any, target: any, key: string): void;
58
45
  export {};
package/dist/index.cjs CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";var U=Object.defineProperty;var W=(n,t,e)=>t in n?U(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var s=(n,t,e)=>W(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l={INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},d={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},f={INVALID:"Invalid",INSTANCE:"Instance",CONSTANT:"ConstantValue",DYNAMIC:"DynamicValue"},I={POST_CONSTRUCT:"Multiple @PostConstruct decorators are not allowed in a single class.",PRE_DESTROY:"Multiple @PreDestroy decorators are not allowed in a single class.",INVALID_TOKEN:"@Inject requires a valid token, but received null or undefined.",LAZY_INJECT_INVALID_TOKEN:"@LazyInject requires a valid token, but received null or undefined.",DECORATE_NOT_SUPPORT_INITIALIZER:"decorate does not support decorators that rely on context.addInitializer (e.g. @LazyInject, @autobind)."},b=Symbol("UNINITIALIZED");function C(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function Y(n){return n!==null&&typeof n=="object"}const y=new WeakMap;function P(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function A(n,t){y.set(n,{metadata:t})}function j(n,t){const e=y.get(t);if(e)return C(e.metadata,n)?e.metadata[n]:void 0}function T(n,t){const e=y.get(t);if(e&&C(e.metadata,n))return e.metadata[n];if(P(t))return T(n,Object.getPrototypeOf(t))}function w(n){const t=y.get(n);if(t&&t.injectedPropsCache!==void 0){const i=t.injectedPropsCache;return i?Object.assign({},i):void 0}const e=J(n);return t&&(t.injectedPropsCache=e??null),e&&Object.assign({},e)}function J(n){const t=j(l.INJECTED_PROPS,n);if(!P(n))return t;const e=w(Object.getPrototypeOf(n));if(e||t)return Object.assign({},e,t)}class G{constructor(t){s(this,"name");this.name=t}}class V{constructor(t){s(this,"_callback");this._callback=t}resolve(){return this._callback()}}function S(n){if(!n)throw new Error(I.INVALID_TOKEN);return n instanceof V?n.resolve():n}class p extends Error{constructor(e,i){const r=(i==null?void 0:i.name)||"<unknown token>";super(`${e}${r}`);s(this,"token");this.name=this.constructor.name,this.token=i}}function R(n){const t=[];let e=n;for(;e&&e.token;)t.push(e.token.name||"<anonymous>"),e=e.parent;return t.reverse()}class m extends p{constructor(t){super(""),this.message="Circular dependency found: "+R(t).join(" --> ")}}class L extends p{constructor(t){super(`Binding is incomplete: container.bind(${t==null?void 0:t.name}) was called but missing to/toSelf/toConstantValue/toDynamicValue method. `,t)}}class B extends m{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const N=class N{constructor(t,e){s(this,"container");s(this,"context");s(this,"token");s(this,"type",f.INVALID);s(this,"status",d.DEFAULT);s(this,"classValue");s(this,"constantValue");s(this,"dynamicValue");s(this,"cache");s(this,"postConstructResult",b);s(this,"transient",!1);s(this,"onActivationHandler");s(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._resolveWithInternalOpts(t,{parent:{token:this.token}}))}get(t){if(d.INITING===this.status)throw new m(t);if(d.ACTIVATED===this.status)if(this.transient)this.status=d.DEFAULT;else return this.cache;const e=N._resolvers.get(this.type);if(e)return e.call(this,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:r}=T(l.POST_CONSTRUCT,this.classValue)||{};if(i)if(r){const a=e.filter(c=>f.INSTANCE===(c==null?void 0:c.type)),o=this._getAwaitBindings(a,r);for(const c of o)if(c&&c.postConstructResult===b)throw new B({token:c.token,parent:t});const u=o.map(c=>c.postConstructResult);this.postConstructResult=Promise.all(u).then(()=>this._execute(i))}else this.postConstructResult=this._execute(i);else this.postConstructResult=void 0}}preDestroy(){if(f.INSTANCE===this.type&&this.cache!==void 0){const{key:t}=T(l.PRE_DESTROY,this.classValue)||{};t&&this._execute(t)}this.container&&!this.container._destroyed&&_._instanceContainerMap.delete(this.cache),this.container=void 0,this.context=void 0,this.classValue=void 0,this.constantValue=void 0,this.dynamicValue=void 0,this.cache=void 0,this.postConstructResult=b,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:r}=this._getInjectProperties(t);return this._injectProperties(i),this._postConstruct(t,r),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=w(this.classValue)||{},i=Object.keys(e),r=Object.create(null),a=[];for(let o=0;o<i.length;o++){const u=i[o],c=e[u],h=Object.assign({},c);h.parent=t;const v=this.container._resolveWithInternalOpts(S(h.inject),h);v===void 0&&h.optional||(r[u]=v),a.push(h.binding)}return{properties:r,bindings:a}}};s(N,"_resolvers",new Map([[f.INSTANCE,function(t){return this._resolveInstanceValue(t)}],[f.CONSTANT,function(t){return this._resolveConstantValue()}],[f.DYNAMIC,function(t){return this._resolveDynamicValue()}]]));let E=N;class M extends p{constructor(t,e){if(super("No matching binding found for token: ",t),e!=null&&e.parent){const i=R(e.parent);i.length>0&&(this.message+=`
1
+ "use strict";var U=Object.defineProperty;var W=(n,t,e)=>t in n?U(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var s=(n,t,e)=>W(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l={INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},d={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},f={INVALID:"Invalid",INSTANCE:"Instance",CONSTANT:"ConstantValue",DYNAMIC:"DynamicValue"},I={POST_CONSTRUCT:"Multiple @PostConstruct decorators are not allowed in a single class.",PRE_DESTROY:"Multiple @PreDestroy decorators are not allowed in a single class.",INVALID_TOKEN:"@Inject requires a valid token, but received null or undefined.",LAZY_INJECT_INVALID_TOKEN:"@LazyInject requires a valid token, but received null or undefined.",DECORATE_NOT_SUPPORT_INITIALIZER:"decorate does not support decorators that rely on context.addInitializer (e.g. @LazyInject)."},b=Symbol("UNINITIALIZED");function C(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function Y(n){return n!==null&&typeof n=="object"}const y=new WeakMap;function P(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function A(n,t){y.set(n,{metadata:t})}function j(n,t){const e=y.get(t);if(e)return C(e.metadata,n)?e.metadata[n]:void 0}function T(n,t){const e=y.get(t);if(e&&C(e.metadata,n))return e.metadata[n];if(P(t))return T(n,Object.getPrototypeOf(t))}function w(n){const t=y.get(n);if(t&&t.injectedPropsCache!==void 0){const i=t.injectedPropsCache;return i?Object.assign({},i):void 0}const e=J(n);return t&&(t.injectedPropsCache=e??null),e&&Object.assign({},e)}function J(n){const t=j(l.INJECTED_PROPS,n);if(!P(n))return t;const e=w(Object.getPrototypeOf(n));if(e||t)return Object.assign({},e,t)}class G{constructor(t){s(this,"name");this.name=t}}class V{constructor(t){s(this,"_callback");this._callback=t}resolve(){return this._callback()}}function S(n){if(!n)throw new Error(I.INVALID_TOKEN);return n instanceof V?n.resolve():n}class p extends Error{constructor(e,i){const r=(i==null?void 0:i.name)||"<unknown token>";super(`${e}${r}`);s(this,"token");this.name=this.constructor.name,this.token=i}}function R(n){const t=[];let e=n;for(;e&&e.token;)t.push(e.token.name||"<anonymous>"),e=e.parent;return t.reverse()}class m extends p{constructor(t){super(""),this.message="Circular dependency found: "+R(t).join(" --> ")}}class L extends p{constructor(t){super(`Binding is incomplete: container.bind(${t==null?void 0:t.name}) was called but missing to/toSelf/toConstantValue/toDynamicValue method. `,t)}}class B extends m{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const N=class N{constructor(t,e){s(this,"container");s(this,"context");s(this,"token");s(this,"type",f.INVALID);s(this,"status",d.DEFAULT);s(this,"classValue");s(this,"constantValue");s(this,"dynamicValue");s(this,"cache");s(this,"postConstructResult",b);s(this,"transient",!1);s(this,"onActivationHandler");s(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._resolveWithInternalOpts(t,{parent:{token:this.token}}))}get(t){if(d.INITING===this.status)throw new m(t);if(d.ACTIVATED===this.status)if(this.transient)this.status=d.DEFAULT;else return this.cache;const e=N._resolvers.get(this.type);if(e)return e.call(this,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:r}=T(l.POST_CONSTRUCT,this.classValue)||{};if(i)if(r){const o=e.filter(c=>f.INSTANCE===(c==null?void 0:c.type)),a=this._getAwaitBindings(o,r);for(const c of a)if(c&&c.postConstructResult===b)throw new B({token:c.token,parent:t});const u=a.map(c=>c.postConstructResult);this.postConstructResult=Promise.all(u).then(()=>this._execute(i))}else this.postConstructResult=this._execute(i);else this.postConstructResult=void 0}}preDestroy(){if(f.INSTANCE===this.type&&this.cache!==void 0){const{key:t}=T(l.PRE_DESTROY,this.classValue)||{};t&&this._execute(t)}this.container&&!this.container._destroyed&&_._instanceContainerMap.delete(this.cache),this.container=void 0,this.context=void 0,this.classValue=void 0,this.constantValue=void 0,this.dynamicValue=void 0,this.cache=void 0,this.postConstructResult=b,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:r}=this._getInjectProperties(t);return this._injectProperties(i),this._postConstruct(t,r),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=w(this.classValue)||{},i=Object.keys(e),r=Object.create(null),o=[];for(let a=0;a<i.length;a++){const u=i[a],c=e[u],h=Object.assign({},c);h.parent=t;const v=this.container._resolveWithInternalOpts(S(h.inject),h);v===void 0&&h.optional||(r[u]=v),o.push(h.binding)}return{properties:r,bindings:o}}};s(N,"_resolvers",new Map([[f.INSTANCE,function(t){return this._resolveInstanceValue(t)}],[f.CONSTANT,function(t){return this._resolveConstantValue()}],[f.DYNAMIC,function(t){return this._resolveDynamicValue()}]]));let E=N;class M extends p{constructor(t,e){if(super("No matching binding found for token: ",t),e!=null&&e.parent){const i=R(e.parent);i.length>0&&(this.message+=`
2
2
  `+i.map(r=>" required by: "+r).join(`
3
- `))}}}class H extends p{constructor(t){super("Cannot bind token multiple times: ",t)}}class F extends p{constructor(t){super("Container has been destroyed. Cannot call get() for token: ",t)}}const g=class g{constructor(){s(this,"parent");s(this,"_children");s(this,"_bindings",new Map);s(this,"_destroyed",!1);s(this,"_onActivationHandler");s(this,"_onDeactivationHandler")}static getContainerOf(t){return g._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)}}tryGet(t){return this.get(t,{optional:!0})}rebind(t){return this._bindings.has(t)&&this.unbind(t),this.bind(t)}unbindAll(){const t=Array.from(this._bindings.keys());for(const e of t)this.unbind(e)}isCurrentBound(t){return this._bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new g;return t.parent=this,this._children||(this._children=new Set),this._children.add(t),t}getChildren(){return this._children}destroy(){var t,e;if(this._destroyed=!0,this._children){const i=Array.from(this._children);for(const r of i)r.destroy()}this.unbindAll(),this._bindings.clear(),(e=(t=this.parent)==null?void 0:t._children)==null||e.delete(this),this.parent=void 0,this._children=void 0,this._onActivationHandler=void 0,this._onDeactivationHandler=void 0}get(t,e={}){const i=Object.assign({},e);return this._resolveWithInternalOpts(t,i)}getAsync(t,e={}){const i=Object.assign({},e);let r;try{r=this._resolveWithInternalOpts(t,i)}catch(o){return Promise.reject(o)}const a=i.binding;return(a==null?void 0:a.postConstructResult)instanceof Promise?a.postConstructResult.then(()=>r):Promise.resolve(r)}_resolveWithInternalOpts(t,e){if(this._destroyed)throw new F(t);return e.skipSelf?this._resolveSkipSelf(t,e):e.self?this._resolveSelf(t,e):this._resolveDefault(t,e)}_resolveSkipSelf(t,e){if(this.parent){const i=Object.assign({},e,{skipSelf:!1});return this.parent._resolveWithInternalOpts(t,i)}return this._checkBindingNotFoundError(t,e)}_resolveSelf(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this._checkBindingNotFoundError(t,e)}_resolveDefault(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this.parent?this.parent._resolveWithInternalOpts(t,e):this._checkBindingNotFoundError(t,e)}onActivation(t){this._onActivationHandler=t}onDeactivation(t){this._onDeactivationHandler=t}activate(t,e){return this._onActivationHandler?this._onActivationHandler({container:this},t,e):t}deactivate(t){this._onDeactivationHandler&&this._onDeactivationHandler(t.cache,t.token)}_buildBinding(t){return new E(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new M(t,e)}};s(g,"_instanceContainerMap",new WeakMap);let _=g;class k extends p{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 O(n,t){return function(e){return function(i,r){const a=r.name,o=r.metadata;C(o,l.INJECTED_PROPS)||(o[l.INJECTED_PROPS]={});const u=o[l.INJECTED_PROPS];u[a]||(u[a]={}),u[a][n]=e===void 0?t:e}}}function x(n,t){return e=>(i,r)=>{const a=r.name,o=r.metadata;if(C(o,n))throw new Error(t);o[n]={key:a,value:e}}}const K=O(l.INJECT),Z=O(l.SELF,!0),$=O(l.SKIP_SELF,!0),q=O(l.OPTIONAL,!0),Q=x(l.POST_CONSTRUCT,I.POST_CONSTRUCT),X=x(l.PRE_DESTROY,I.PRE_DESTROY);function tt(){return function(n,t){const e=t.metadata;A(n,e)}}function et(n,t,e,i){if(e==null)throw new Error(I.LAZY_INJECT_INVALID_TOKEN);let r,a=!1;Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get(){if(!a){const o=i||_.getContainerOf(n),u=n.constructor;if(!o)throw new k(S(e),u);r=o._resolveWithInternalOpts(S(e),{parent:{token:u}}),a=!0}return r},set(o){r=o,a=!0}})}function z(n,t){return function(e,i){const r=i.name;i.addInitializer(function(){et(this,r,n,t)})}}function nt(n){return function(t){return z(t,n)}}function it(n,t){const e=t.name;t.addInitializer(function(){this[e]=n.bind(this)})}const D=new WeakMap;function rt(n,t,e){const i=Array.isArray(n)?n:[n],r=t.prototype,a=typeof r[e]=="function";D.has(t)||D.set(t,{});const o=D.get(t),u={kind:a?"method":"field",name:e,static:!1,private:!1,addInitializer(h){throw new Error(I.DECORATE_NOT_SUPPORT_INITIALIZER)},metadata:o};let c=a?r[e]:void 0;for(let h=i.length-1;h>=0;h--){const v=i[h](c,u);a&&typeof v=="function"&&(c=v)}a&&c!==r[e]&&(r[e]=c),A(t,o)}exports.BaseError=p;exports.Binding=E;exports.BindingNotFoundError=M;exports.BindingNotValidError=L;exports.CircularDependencyError=m;exports.Container=_;exports.ContainerDestroyedError=F;exports.ContainerNotFoundError=k;exports.DuplicateBindingError=H;exports.ERRORS=I;exports.Inject=K;exports.Injectable=tt;exports.LazyInject=z;exports.LazyToken=V;exports.Optional=q;exports.PostConstruct=Q;exports.PostConstructError=B;exports.PreDestroy=X;exports.Self=Z;exports.SkipSelf=$;exports.Token=G;exports.autobind=it;exports.createLazyInject=nt;exports.decorate=rt;exports.defineMetadata=A;exports.getMetadata=T;exports.getOwnMetadata=j;exports.hasOwn=C;exports.isObject=Y;
3
+ `))}}}class H extends p{constructor(t){super("Cannot bind token multiple times: ",t)}}class x extends p{constructor(t){super("Container has been destroyed. Cannot call get() for token: ",t)}}const g=class g{constructor(){s(this,"parent");s(this,"_children");s(this,"_bindings",new Map);s(this,"_destroyed",!1);s(this,"_onActivationHandler");s(this,"_onDeactivationHandler")}static getContainerOf(t){return g._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)}}tryGet(t){return this.get(t,{optional:!0})}rebind(t){return this._bindings.has(t)&&this.unbind(t),this.bind(t)}unbindAll(){const t=Array.from(this._bindings.keys());for(const e of t)this.unbind(e)}isCurrentBound(t){return this._bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new g;return t.parent=this,this._children||(this._children=new Set),this._children.add(t),t}getChildren(){return this._children}destroy(){var t,e;if(this._destroyed=!0,this._children){const i=Array.from(this._children);for(const r of i)r.destroy()}this.unbindAll(),this._bindings.clear(),(e=(t=this.parent)==null?void 0:t._children)==null||e.delete(this),this.parent=void 0,this._children=void 0,this._onActivationHandler=void 0,this._onDeactivationHandler=void 0}get(t,e={}){const i=Object.assign({},e);return this._resolveWithInternalOpts(t,i)}getAsync(t,e={}){const i=Object.assign({},e);let r;try{r=this._resolveWithInternalOpts(t,i)}catch(a){return Promise.reject(a)}const o=i.binding;return(o==null?void 0:o.postConstructResult)instanceof Promise?o.postConstructResult.then(()=>r):Promise.resolve(r)}_resolveWithInternalOpts(t,e){if(this._destroyed)throw new x(t);return e.skipSelf?this._resolveSkipSelf(t,e):e.self?this._resolveSelf(t,e):this._resolveDefault(t,e)}_resolveSkipSelf(t,e){if(this.parent){const i=Object.assign({},e,{skipSelf:!1});return this.parent._resolveWithInternalOpts(t,i)}return this._checkBindingNotFoundError(t,e)}_resolveSelf(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this._checkBindingNotFoundError(t,e)}_resolveDefault(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this.parent?this.parent._resolveWithInternalOpts(t,e):this._checkBindingNotFoundError(t,e)}onActivation(t){this._onActivationHandler=t}onDeactivation(t){this._onDeactivationHandler=t}activate(t,e){return this._onActivationHandler?this._onActivationHandler({container:this},t,e):t}deactivate(t){this._onDeactivationHandler&&this._onDeactivationHandler(t.cache,t.token)}_buildBinding(t){return new E(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new M(t,e)}};s(g,"_instanceContainerMap",new WeakMap);let _=g;class F extends p{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 O(n,t){return function(e){return function(i,r){const o=r.name,a=r.metadata;C(a,l.INJECTED_PROPS)||(a[l.INJECTED_PROPS]={});const u=a[l.INJECTED_PROPS];u[o]||(u[o]={}),u[o][n]=e===void 0?t:e}}}function k(n,t){return e=>(i,r)=>{const o=r.name,a=r.metadata;if(C(a,n))throw new Error(t);a[n]={key:o,value:e}}}const K=O(l.INJECT),Z=O(l.SELF,!0),$=O(l.SKIP_SELF,!0),q=O(l.OPTIONAL,!0),Q=k(l.POST_CONSTRUCT,I.POST_CONSTRUCT),X=k(l.PRE_DESTROY,I.PRE_DESTROY);function tt(){return function(n,t){const e=t.metadata;A(n,e)}}function et(n,t,e,i){if(e==null)throw new Error(I.LAZY_INJECT_INVALID_TOKEN);let r,o=!1;Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get(){if(!o){const a=i||_.getContainerOf(n),u=n.constructor;if(!a)throw new F(S(e),u);r=a._resolveWithInternalOpts(S(e),{parent:{token:u}}),o=!0}return r},set(a){r=a,o=!0}})}function z(n,t){return function(e,i){const r=i.name;i.addInitializer(function(){et(this,r,n,t)})}}function nt(n){return function(t){return z(t,n)}}const D=new WeakMap;function it(n,t,e){const i=Array.isArray(n)?n:[n],r=t.prototype,o=typeof r[e]=="function";D.has(t)||D.set(t,{});const a=D.get(t),u={kind:o?"method":"field",name:e,static:!1,private:!1,addInitializer(h){throw new Error(I.DECORATE_NOT_SUPPORT_INITIALIZER)},metadata:a};let c=o?r[e]:void 0;for(let h=i.length-1;h>=0;h--){const v=i[h](c,u);o&&typeof v=="function"&&(c=v)}o&&c!==r[e]&&(r[e]=c),A(t,a)}exports.BaseError=p;exports.Binding=E;exports.BindingNotFoundError=M;exports.BindingNotValidError=L;exports.CircularDependencyError=m;exports.Container=_;exports.ContainerDestroyedError=x;exports.ContainerNotFoundError=F;exports.DuplicateBindingError=H;exports.ERRORS=I;exports.Inject=K;exports.Injectable=tt;exports.LazyInject=z;exports.LazyToken=V;exports.Optional=q;exports.PostConstruct=Q;exports.PostConstructError=B;exports.PreDestroy=X;exports.Self=Z;exports.SkipSelf=$;exports.Token=G;exports.createLazyInject=nt;exports.decorate=it;exports.defineMetadata=A;exports.getMetadata=T;exports.getOwnMetadata=j;exports.hasOwn=C;exports.isObject=Y;
package/dist/index.d.cts CHANGED
@@ -2,7 +2,7 @@ export type { Newable, InjectFunction, CommonToken, TokenType, GenericToken, Laz
2
2
  export { Container } from './container';
3
3
  export { Binding } from './binding';
4
4
  export { Token, LazyToken } from './token';
5
- export { Inject, Self, SkipSelf, Optional, PostConstruct, PreDestroy, Injectable, decorate, LazyInject, createLazyInject, autobind, } from './decorator';
5
+ export { Inject, Self, SkipSelf, Optional, PostConstruct, PreDestroy, Injectable, decorate, LazyInject, createLazyInject, } from './decorator';
6
6
  export { BaseError } from './errors/BaseError';
7
7
  export { BindingNotFoundError } from './errors/BindingNotFoundError';
8
8
  export { BindingNotValidError } from './errors/BindingNotValidError';
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export type { Newable, InjectFunction, CommonToken, TokenType, GenericToken, Laz
2
2
  export { Container } from './container';
3
3
  export { Binding } from './binding';
4
4
  export { Token, LazyToken } from './token';
5
- export { Inject, Self, SkipSelf, Optional, PostConstruct, PreDestroy, Injectable, decorate, LazyInject, createLazyInject, autobind, } from './decorator';
5
+ export { Inject, Self, SkipSelf, Optional, PostConstruct, PreDestroy, Injectable, decorate, LazyInject, createLazyInject, } from './decorator';
6
6
  export { BaseError } from './errors/BaseError';
7
7
  export { BindingNotFoundError } from './errors/BindingNotFoundError';
8
8
  export { BindingNotValidError } from './errors/BindingNotValidError';
package/dist/index.js CHANGED
@@ -34,7 +34,7 @@ const l = {
34
34
  // 用于 decorator.ts 的 defineLazyProperty —— 无效 token
35
35
  LAZY_INJECT_INVALID_TOKEN: "@LazyInject requires a valid token, but received null or undefined.",
36
36
  // 用于 decorator.ts 的 decorate —— 不支持 addInitializer 的装饰器
37
- DECORATE_NOT_SUPPORT_INITIALIZER: "decorate does not support decorators that rely on context.addInitializer (e.g. @LazyInject, @autobind)."
37
+ DECORATE_NOT_SUPPORT_INITIALIZER: "decorate does not support decorators that rely on context.addInitializer (e.g. @LazyInject)."
38
38
  }, O = Symbol("UNINITIALIZED");
39
39
  function g(n, t) {
40
40
  return Object.prototype.hasOwnProperty.call(n, t);
@@ -43,10 +43,10 @@ function K(n) {
43
43
  return n !== null && typeof n == "object";
44
44
  }
45
45
  const N = /* @__PURE__ */ new WeakMap();
46
- function D(n) {
46
+ function S(n) {
47
47
  return typeof n == "function" && Object.getPrototypeOf(n) !== Function.prototype;
48
48
  }
49
- function S(n, t) {
49
+ function m(n, t) {
50
50
  N.set(n, { metadata: t });
51
51
  }
52
52
  function M(n, t) {
@@ -54,12 +54,12 @@ function M(n, t) {
54
54
  if (e)
55
55
  return g(e.metadata, n) ? e.metadata[n] : void 0;
56
56
  }
57
- function b(n, t) {
57
+ function A(n, t) {
58
58
  const e = N.get(t);
59
59
  if (e && g(e.metadata, n))
60
60
  return e.metadata[n];
61
- if (D(t))
62
- return b(n, Object.getPrototypeOf(t));
61
+ if (S(t))
62
+ return A(n, Object.getPrototypeOf(t));
63
63
  }
64
64
  function P(n) {
65
65
  const t = N.get(n);
@@ -72,7 +72,7 @@ function P(n) {
72
72
  }
73
73
  function B(n) {
74
74
  const t = M(l.INJECTED_PROPS, n);
75
- if (!D(n))
75
+ if (!S(n))
76
76
  return t;
77
77
  const e = P(Object.getPrototypeOf(n));
78
78
  if (e || t)
@@ -94,7 +94,7 @@ class H {
94
94
  return this._callback();
95
95
  }
96
96
  }
97
- function A(n) {
97
+ function b(n) {
98
98
  if (!n)
99
99
  throw new Error(T.INVALID_TOKEN);
100
100
  return n instanceof H ? n.resolve() : n;
@@ -214,7 +214,7 @@ const C = class C {
214
214
  */
215
215
  _postConstruct(t, e) {
216
216
  if (f.INSTANCE === this.type) {
217
- const { key: i, value: s } = b(l.POST_CONSTRUCT, this.classValue) || {};
217
+ const { key: i, value: s } = A(l.POST_CONSTRUCT, this.classValue) || {};
218
218
  if (i)
219
219
  if (s) {
220
220
  const a = e.filter(
@@ -238,7 +238,7 @@ const C = class C {
238
238
  }
239
239
  preDestroy() {
240
240
  if (f.INSTANCE === this.type && this.cache !== void 0) {
241
- const { key: t } = b(l.PRE_DESTROY, this.classValue) || {};
241
+ const { key: t } = A(l.PRE_DESTROY, this.classValue) || {};
242
242
  t && this._execute(t);
243
243
  }
244
244
  this.container && !this.container._destroyed && v._instanceContainerMap.delete(this.cache), this.container = void 0, this.context = void 0, this.classValue = void 0, this.constantValue = void 0, this.dynamicValue = void 0, this.cache = void 0, this.postConstructResult = O, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
@@ -281,7 +281,7 @@ const C = class C {
281
281
  const u = i[o], c = e[u], h = Object.assign({}, c);
282
282
  h.parent = t;
283
283
  const _ = this.container._resolveWithInternalOpts(
284
- A(h.inject),
284
+ b(h.inject),
285
285
  h
286
286
  );
287
287
  _ === void 0 && h.optional || (s[u] = _), a.push(h.binding);
@@ -301,7 +301,7 @@ r(C, "_resolvers", /* @__PURE__ */ new Map([
301
301
  return this._resolveDynamicValue();
302
302
  }]
303
303
  ]));
304
- let m = C;
304
+ let D = C;
305
305
  class U extends p {
306
306
  constructor(t, e) {
307
307
  if (super("No matching binding found for token: ", t), e != null && e.parent) {
@@ -317,7 +317,7 @@ class k extends p {
317
317
  super("Cannot bind token multiple times: ", t);
318
318
  }
319
319
  }
320
- class z extends p {
320
+ class W extends p {
321
321
  constructor(t) {
322
322
  super("Container has been destroyed. Cannot call get() for token: ", t);
323
323
  }
@@ -398,7 +398,7 @@ const I = class I {
398
398
  // 内部解析入口,接受完整 Options;被 getAsync、toService、_getInjectProperties 等内部路径调用
399
399
  _resolveWithInternalOpts(t, e) {
400
400
  if (this._destroyed)
401
- throw new z(t);
401
+ throw new W(t);
402
402
  return e.skipSelf ? this._resolveSkipSelf(t, e) : e.self ? this._resolveSelf(t, e) : this._resolveDefault(t, e);
403
403
  }
404
404
  // 处理 skipSelf 选项:跳过当前容器,委托父容器解析
@@ -432,7 +432,7 @@ const I = class I {
432
432
  this._onDeactivationHandler && this._onDeactivationHandler(t.cache, t.token);
433
433
  }
434
434
  _buildBinding(t) {
435
- return new m(t, this);
435
+ return new D(t, this);
436
436
  }
437
437
  _getBinding(t) {
438
438
  return this._bindings.get(t);
@@ -450,7 +450,7 @@ const I = class I {
450
450
  // 由于 Instance 类型每次都通过 new ClassName() 创建新实例,不存在同一实例被多个容器注册的覆盖风险
451
451
  r(I, "_instanceContainerMap", /* @__PURE__ */ new WeakMap());
452
452
  let v = I;
453
- class W extends p {
453
+ class Y extends p {
454
454
  constructor(t, e) {
455
455
  super(
456
456
  `@LazyInject(${t == null ? void 0 : t.name}) in class ${e.name} requires a registered container but none was found. Token: `,
@@ -486,10 +486,10 @@ const $ = E(l.INJECT), q = E(l.SELF, !0), Q = E(l.SKIP_SELF, !0), X = E(l.OPTION
486
486
  function nt() {
487
487
  return function(n, t) {
488
488
  const e = t.metadata;
489
- S(n, e);
489
+ m(n, e);
490
490
  };
491
491
  }
492
- function Y(n, t, e, i) {
492
+ function z(n, t, e, i) {
493
493
  if (e == null)
494
494
  throw new Error(T.LAZY_INJECT_INVALID_TOKEN);
495
495
  let s, a = !1;
@@ -500,8 +500,8 @@ function Y(n, t, e, i) {
500
500
  if (!a) {
501
501
  const o = i || v.getContainerOf(n), u = n.constructor;
502
502
  if (!o)
503
- throw new W(A(e), u);
504
- s = o._resolveWithInternalOpts(A(e), {
503
+ throw new Y(b(e), u);
504
+ s = o._resolveWithInternalOpts(b(e), {
505
505
  parent: { token: u }
506
506
  }), a = !0;
507
507
  }
@@ -516,7 +516,7 @@ function J(n, t) {
516
516
  return function(e, i) {
517
517
  const s = i.name;
518
518
  i.addInitializer(function() {
519
- Y(this, s, n, t);
519
+ z(this, s, n, t);
520
520
  });
521
521
  };
522
522
  }
@@ -525,14 +525,8 @@ function it(n) {
525
525
  return J(t, n);
526
526
  };
527
527
  }
528
- function st(n, t) {
529
- const e = t.name;
530
- t.addInitializer(function() {
531
- this[e] = n.bind(this);
532
- });
533
- }
534
528
  const y = /* @__PURE__ */ new WeakMap();
535
- function rt(n, t, e) {
529
+ function st(n, t, e) {
536
530
  const i = Array.isArray(n) ? n : [n], s = t.prototype, a = typeof s[e] == "function";
537
531
  y.has(t) || y.set(t, {});
538
532
  const o = y.get(t), u = {
@@ -550,17 +544,17 @@ function rt(n, t, e) {
550
544
  const _ = i[h](c, u);
551
545
  a && typeof _ == "function" && (c = _);
552
546
  }
553
- a && c !== s[e] && (s[e] = c), S(t, o);
547
+ a && c !== s[e] && (s[e] = c), m(t, o);
554
548
  }
555
549
  export {
556
550
  p as BaseError,
557
- m as Binding,
551
+ D as Binding,
558
552
  U as BindingNotFoundError,
559
553
  x as BindingNotValidError,
560
554
  V as CircularDependencyError,
561
555
  v as Container,
562
- z as ContainerDestroyedError,
563
- W as ContainerNotFoundError,
556
+ W as ContainerDestroyedError,
557
+ Y as ContainerNotFoundError,
564
558
  k as DuplicateBindingError,
565
559
  T as ERRORS,
566
560
  $ as Inject,
@@ -574,11 +568,10 @@ export {
574
568
  q as Self,
575
569
  Q as SkipSelf,
576
570
  Z as Token,
577
- st as autobind,
578
571
  it as createLazyInject,
579
- rt as decorate,
580
- S as defineMetadata,
581
- b as getMetadata,
572
+ st as decorate,
573
+ m as defineMetadata,
574
+ A as getMetadata,
582
575
  M as getOwnMetadata,
583
576
  g as hasOwn,
584
577
  K as isObject
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaokei/di",
3
- "version": "5.0.8",
3
+ "version": "5.0.9",
4
4
  "type": "module",
5
5
  "description": "Tiny di library depends on typescript decorator.",
6
6
  "main": "./dist/index.cjs",
@@ -20,7 +20,7 @@
20
20
  "./package.json": "./package.json"
21
21
  },
22
22
  "sideEffects": false,
23
- "homepage": "https://github.com/kaokei/di",
23
+ "homepage": "https://di.kaokei.com/",
24
24
  "author": "kaokei",
25
25
  "license": "MIT",
26
26
  "bugs": {