@kaokei/di 3.0.7 → 3.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.
- package/dist/binding.d.cts +1 -1
- package/dist/binding.d.ts +1 -1
- package/dist/decorator.d.cts +2 -2
- package/dist/decorator.d.ts +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.js +10 -11
- package/package.json +1 -1
package/dist/binding.d.cts
CHANGED
|
@@ -26,7 +26,7 @@ export declare class Binding<T = unknown> {
|
|
|
26
26
|
get(options: Options<T>): T;
|
|
27
27
|
private getAwaitBindings;
|
|
28
28
|
private postConstruct;
|
|
29
|
-
preDestroy():
|
|
29
|
+
preDestroy(): void;
|
|
30
30
|
private execute;
|
|
31
31
|
private resolveInstanceValue;
|
|
32
32
|
private resolveConstantValue;
|
package/dist/binding.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ export declare class Binding<T = unknown> {
|
|
|
26
26
|
get(options: Options<T>): T;
|
|
27
27
|
private getAwaitBindings;
|
|
28
28
|
private postConstruct;
|
|
29
|
-
preDestroy():
|
|
29
|
+
preDestroy(): void;
|
|
30
30
|
private execute;
|
|
31
31
|
private resolveInstanceValue;
|
|
32
32
|
private resolveConstantValue;
|
package/dist/decorator.d.cts
CHANGED
|
@@ -12,7 +12,7 @@ export declare const Inject: InjectFunction<ReturnType<typeof createDecorator>>;
|
|
|
12
12
|
export declare const Self: (decoratorValue?: any) => (target: any, targetKey?: string, index?: number) => void;
|
|
13
13
|
export declare const SkipSelf: (decoratorValue?: any) => (target: any, targetKey?: string, index?: number) => void;
|
|
14
14
|
export declare const Optional: (decoratorValue?: any) => (target: any, targetKey?: string, index?: number) => void;
|
|
15
|
-
export declare const PostConstruct: (metaValue
|
|
16
|
-
export declare const PreDestroy: (metaValue
|
|
15
|
+
export declare const PostConstruct: (metaValue?: any) => (target: any, propertyKey: string) => void;
|
|
16
|
+
export declare const PreDestroy: (metaValue?: any) => (target: any, propertyKey: string) => void;
|
|
17
17
|
export declare function decorate(decorator: any, target: any, key: number | string): void;
|
|
18
18
|
export {};
|
package/dist/decorator.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare const Inject: InjectFunction<ReturnType<typeof createDecorator>>;
|
|
|
12
12
|
export declare const Self: (decoratorValue?: any) => (target: any, targetKey?: string, index?: number) => void;
|
|
13
13
|
export declare const SkipSelf: (decoratorValue?: any) => (target: any, targetKey?: string, index?: number) => void;
|
|
14
14
|
export declare const Optional: (decoratorValue?: any) => (target: any, targetKey?: string, index?: number) => void;
|
|
15
|
-
export declare const PostConstruct: (metaValue
|
|
16
|
-
export declare const PreDestroy: (metaValue
|
|
15
|
+
export declare const PostConstruct: (metaValue?: any) => (target: any, propertyKey: string) => void;
|
|
16
|
+
export declare const PreDestroy: (metaValue?: any) => (target: any, propertyKey: string) => void;
|
|
17
17
|
export declare function decorate(decorator: any, target: any, key: number | string): void;
|
|
18
18
|
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var w=Object.defineProperty;var j=(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)=>j(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";var w=Object.defineProperty;var j=(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)=>j(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"},v={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."},m=Symbol(),D=new WeakMap;function B(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function b(n,t,e){const s=D.get(e)||{};s[n]=t,D.set(e,s)}function T(n,t){return(D.get(t)||{})[n]}function I(n,t){const e=T(n,t);if(!B(t))return e;const s=I(n,Object.getPrototypeOf(t));if(s||e)return{...s||{},...e||{}}}class _{constructor(t){r(this,"_","");r(this,"name");this.name=t}}class O{constructor(t){r(this,"callback");this.callback=t}resolve(){return this.callback()}}function A(n){if(!n)throw new Error(v.MISS_INJECT);return n instanceof O?n.resolve():n}class E extends Error{constructor(t,e){super(),this.name=this.constructor.name,this.message=`${t}${e==null?void 0:e.name}`}}class V extends E{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 E{constructor(t){super("Invalid binding: ",t)}}class H extends V{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",m);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 V(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}=I(h.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===m)throw new H({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}=I(h.PRE_DESTROY,this.classValue)||{};t&&this.execute(t)}y.map.delete(this.cache),this.container=null,this.context=null,this.classValue=null,this.constantValue=null,this.dynamicValue=null,this.cache=null,this.postConstructResult=m,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=p.INITING;const e=this.classValue,[s,i]=this.getConstructorParameters(t),o=new e(...s);this.cache=this.activate(o),this.status=p.ACTIVATED,y.map.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=T(h.INJECTED_PARAMS,this.classValue)||[],s=[],i=[];for(let o=0;o<e.length;o++){const a=e[o],{inject:c,...l}=a;l.parent=t;const u=this.container.get(A(c),l);s.push(u),i.push(l.binding)}return[s,i]}getInjectProperties(t){const e=I(h.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],l=e[c],{inject:u,...f}=l;f.parent=t;const g=this.container.get(A(u),f);g===void 0&&l.optional||(i[c]=g),o.push(f.binding)}return[i,o]}}class M extends E{constructor(t){super("No matching binding found for token: ",t)}}class x extends E{constructor(t){super("Cannot bind token multiple times: ",t)}}const C=class C{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.deactivate(),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 C;return t.parent=this,this.children||(this.children=new Set),this.children.add(t),t}destroy(){var t,e,s;this.unbindAll(),this.bindings.clear(),(e=(t=this.parent)==null?void 0:t.children)==null||e.delete(this),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)}buildBinding(t){return new L(t,this)}getBinding(t){return this.bindings.get(t)}checkBindingNotFoundError(t,e){if(!e.optional)throw new M(t)}};r(C,"map",new WeakMap);let y=C;function S(n,t){return function(e){return function(s,i,o){const a=typeof o=="number",c=a?s:s.constructor,l=a?o:i,u=a?h.INJECTED_PARAMS:h.INJECTED_PROPS,f=a?T(u,c)||[]:I(u,c)||{},g=f[l]||{};g[n]=e===void 0?t:e,f[l]=g,b(u,f,c)}}}function N(n,t){return e=>(s,i)=>{if(T(n,s.constructor))throw new Error(t);b(n,{key:i,value:e},s.constructor)}}const F=S(h.INJECT),J=S(h.SELF,!0),U=S(h.SKIP_SELF,!0),z=S(h.OPTIONAL,!0),G=N(h.POST_CONSTRUCT,v.POST_CONSTRUCT),Y=N(h.PRE_DESTROY,v.PRE_DESTROY);function P(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"?P(n,t,void 0,e):typeof e=="string"&&P(n,t.prototype,e)}function $(n,t,e,s){function i(){const a=Symbol.for(t);if(!this.hasOwnProperty(a)){const c=s||y.map.get(this),l=this.constructor;if(!c)throw new Error(`${v.MISS_CONTAINER} ${l.name}`);this[a]=c.get(A(e),{parent:{token:l}})}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=y;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=_;exports.createLazyInject=W;exports.decorate=K;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
var R = (e, t, n) => t in e ?
|
|
1
|
+
var O = Object.defineProperty;
|
|
2
|
+
var R = (e, t, n) => t in e ? O(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
3
|
var r = (e, t, n) => R(e, typeof t != "symbol" ? t + "" : t, n);
|
|
4
4
|
const l = {
|
|
5
5
|
// 记录构造函数参数装饰器对应的数据的key
|
|
@@ -83,7 +83,7 @@ class T extends Error {
|
|
|
83
83
|
super(), this.name = this.constructor.name, this.message = `${t}${n == null ? void 0 : n.name}`;
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
class
|
|
86
|
+
class V extends T {
|
|
87
87
|
constructor(t) {
|
|
88
88
|
super("");
|
|
89
89
|
const n = [];
|
|
@@ -99,7 +99,7 @@ class _ extends T {
|
|
|
99
99
|
super("Invalid binding: ", t);
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
-
class H extends
|
|
102
|
+
class H extends V {
|
|
103
103
|
constructor(t) {
|
|
104
104
|
super(t), this.name = "CircularDependencyError inside @PostConstruct";
|
|
105
105
|
}
|
|
@@ -152,7 +152,7 @@ class j {
|
|
|
152
152
|
}
|
|
153
153
|
get(t) {
|
|
154
154
|
if (p.INITING === this.status)
|
|
155
|
-
throw new
|
|
155
|
+
throw new V(t);
|
|
156
156
|
if (p.ACTIVATED === this.status)
|
|
157
157
|
return this.cache;
|
|
158
158
|
if (d.Instance === this.type)
|
|
@@ -193,10 +193,9 @@ class j {
|
|
|
193
193
|
preDestroy() {
|
|
194
194
|
if (d.Instance === this.type) {
|
|
195
195
|
const { key: t } = y(l.PRE_DESTROY, this.classValue) || {};
|
|
196
|
-
|
|
197
|
-
return this.execute(t);
|
|
196
|
+
t && this.execute(t);
|
|
198
197
|
}
|
|
199
|
-
I.map.delete(this.cache), this.container = null, this.context = null, this.cache = null, this.postConstructResult = D, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
|
|
198
|
+
I.map.delete(this.cache), this.container = null, this.context = null, this.classValue = null, this.constantValue = null, this.dynamicValue = null, this.cache = null, this.postConstructResult = D, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
|
|
200
199
|
}
|
|
201
200
|
execute(t) {
|
|
202
201
|
const n = this.cache[t];
|
|
@@ -332,7 +331,7 @@ function m(e, t) {
|
|
|
332
331
|
};
|
|
333
332
|
};
|
|
334
333
|
}
|
|
335
|
-
function
|
|
334
|
+
function N(e, t) {
|
|
336
335
|
return (n) => (s, i) => {
|
|
337
336
|
if (E(e, s.constructor))
|
|
338
337
|
throw new Error(t);
|
|
@@ -343,10 +342,10 @@ function O(e, t) {
|
|
|
343
342
|
);
|
|
344
343
|
};
|
|
345
344
|
}
|
|
346
|
-
const U = m(l.INJECT), G = m(l.SELF, !0), Y = m(l.SKIP_SELF, !0), z = m(l.OPTIONAL, !0), K =
|
|
345
|
+
const U = m(l.INJECT), G = m(l.SELF, !0), Y = m(l.SKIP_SELF, !0), z = m(l.OPTIONAL, !0), K = N(
|
|
347
346
|
l.POST_CONSTRUCT,
|
|
348
347
|
C.POST_CONSTRUCT
|
|
349
|
-
), $ =
|
|
348
|
+
), $ = N(
|
|
350
349
|
l.PRE_DESTROY,
|
|
351
350
|
C.PRE_DESTROY
|
|
352
351
|
);
|