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