@kaokei/di 3.0.5 → 3.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/container.d.cts +2 -0
- package/dist/container.d.ts +2 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +47 -42
- package/package.json +1 -1
package/dist/container.d.cts
CHANGED
|
@@ -2,6 +2,7 @@ import { Binding } from './binding';
|
|
|
2
2
|
import { Options, CommonToken, ActivationHandler, DeactivationHandler } from './interfaces';
|
|
3
3
|
export declare class Container {
|
|
4
4
|
parent?: Container;
|
|
5
|
+
children?: Set<Container>;
|
|
5
6
|
private bindings;
|
|
6
7
|
private onActivationHandler?;
|
|
7
8
|
private onDeactivationHandler?;
|
|
@@ -11,6 +12,7 @@ export declare class Container {
|
|
|
11
12
|
isCurrentBound<T>(token: CommonToken<T>): boolean;
|
|
12
13
|
isBound<T>(token: CommonToken<T>): boolean;
|
|
13
14
|
createChild(): Container;
|
|
15
|
+
destroy(): void;
|
|
14
16
|
get<T>(token: CommonToken<T>, options: Options<T> & {
|
|
15
17
|
optional: true;
|
|
16
18
|
}): T | void;
|
package/dist/container.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Binding } from './binding';
|
|
|
2
2
|
import { Options, CommonToken, ActivationHandler, DeactivationHandler } from './interfaces';
|
|
3
3
|
export declare class Container {
|
|
4
4
|
parent?: Container;
|
|
5
|
+
children?: Set<Container>;
|
|
5
6
|
private bindings;
|
|
6
7
|
private onActivationHandler?;
|
|
7
8
|
private onDeactivationHandler?;
|
|
@@ -11,6 +12,7 @@ export declare class Container {
|
|
|
11
12
|
isCurrentBound<T>(token: CommonToken<T>): boolean;
|
|
12
13
|
isBound<T>(token: CommonToken<T>): boolean;
|
|
13
14
|
createChild(): Container;
|
|
15
|
+
destroy(): void;
|
|
14
16
|
get<T>(token: CommonToken<T>, options: Options<T> & {
|
|
15
17
|
optional: true;
|
|
16
18
|
}): T | void;
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var w=Object.defineProperty;var _=(n,t,e)=>t in n?w(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var
|
|
1
|
+
"use strict";var w=Object.defineProperty;var _=(n,t,e)=>t in n?w(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var r=(n,t,e)=>_(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l={INJECTED_PARAMS:"injected:params",INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},p={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},d={Invalid:"Invalid",Instance:"Instance",ConstantValue:"ConstantValue",DynamicValue:"DynamicValue"},C={POST_CONSTRUCT:"Cannot apply @PostConstruct decorator multiple times in the same class.",PRE_DESTROY:"Cannot apply @PreDestroy decorator multiple times in the same class.",MISS_INJECT:"Expected a @Inject decorator to explicitly specify the token.",MISS_CONTAINER:"@LazyInject decorator cannot find the corresponding container."},P=Symbol(),v=new WeakMap;function M(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function D(n,t,e){const s=v.get(e)||{};s[n]=t,v.set(e,s)}function I(n,t){return(v.get(t)||{})[n]}function g(n,t){const e=I(n,t);if(!M(t))return e;const s=g(n,Object.getPrototypeOf(t));if(s||e)return{...s||{},...e||{}}}class j{constructor(t){r(this,"_","");r(this,"name");this.name=t}}class N{constructor(t){r(this,"callback");this.callback=t}resolve(){return this.callback()}}function S(n){if(!n)throw new Error(C.MISS_INJECT);return n instanceof N?n.resolve():n}class T extends Error{constructor(t,e){super(),this.name=this.constructor.name,this.message=`${t}${e==null?void 0:e.name}`}}class O extends T{constructor(t){super("");const e=[];let s=t;for(;s&&s.token;)e.push(s.token),s=s.parent;const i=e.reverse().map(o=>o.name).join(" --> ");this.message=`Circular dependency found: ${i}`}}class B extends T{constructor(t){super("Invalid binding: ",t)}}class k extends O{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}class L{constructor(t,e){r(this,"container");r(this,"context");r(this,"token");r(this,"type",d.Invalid);r(this,"status",p.DEFAULT);r(this,"classValue");r(this,"constantValue");r(this,"dynamicValue");r(this,"cache");r(this,"postConstructResult",P);r(this,"onActivationHandler");r(this,"onDeactivationHandler");this.container=e,this.context={container:this.container},this.token=t}onActivation(t){this.onActivationHandler=t}onDeactivation(t){this.onDeactivationHandler=t}activate(t){const e=this.onActivationHandler?this.onActivationHandler(this.context,t):t;return this.container.activate(e,this.token)}deactivate(){this.onDeactivationHandler&&this.onDeactivationHandler(this.cache)}to(t){return this.type=d.Instance,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=d.ConstantValue,this.constantValue=t,this}toDynamicValue(t){return this.type=d.DynamicValue,this.dynamicValue=t,this}toService(t){return this.toDynamicValue(e=>e.container.get(t,{parent:{token:this.token}}))}get(t){if(p.INITING===this.status)throw new O(t);if(p.ACTIVATED===this.status)return this.cache;if(d.Instance===this.type)return this.resolveInstanceValue(t);if(d.ConstantValue===this.type)return this.resolveConstantValue();if(d.DynamicValue===this.type)return this.resolveDynamicValue();throw new B(this.token)}getAwaitBindings(t,e){return e===!0?t:Array.isArray(e)?t.filter(s=>e.includes(s.token)):typeof e=="function"?t.filter(e):[]}postConstruct(t,e,s){if(d.Instance===this.type){const{key:i,value:o}=g(l.POST_CONSTRUCT,this.classValue)||{};if(i)if(o){const a=[...e,...s].filter(u=>d.Instance===(u==null?void 0:u.type)),c=this.getAwaitBindings(a,o);for(const u of c)if(u&&u.postConstructResult===P)throw new k({token:u.token,parent:t});const h=c.map(u=>u.postConstructResult);this.postConstructResult=Promise.all(h).then(()=>this.execute(i))}else this.postConstructResult=this.execute(i);else this.postConstructResult=void 0}}preDestroy(){if(d.Instance===this.type){const{key:t}=g(l.PRE_DESTROY,this.classValue)||{};if(t)return this.execute(t)}this.container=null,this.context=null}execute(t){const e=this.cache[t];return e==null?void 0:e.call(this.cache)}resolveInstanceValue(t){this.status=p.INITING;const e=this.classValue,[s,i]=this.getConstructorParameters(t),o=new e(...s);this.cache=this.activate(o),this.status=p.ACTIVATED,A.set(this.cache,this.container);const[a,c]=this.getInjectProperties(t);return Object.assign(this.cache,a),this.postConstruct(t,i,c),this.cache}resolveConstantValue(){return this.status=p.INITING,this.cache=this.activate(this.constantValue),this.status=p.ACTIVATED,this.cache}resolveDynamicValue(){this.status=p.INITING;const t=this.dynamicValue.call(this,this.context);return this.cache=this.activate(t),this.status=p.ACTIVATED,this.cache}getConstructorParameters(t){const e=I(l.INJECTED_PARAMS,this.classValue)||[],s=[],i=[];for(let o=0;o<e.length;o++){const a=e[o],{inject:c,...h}=a;h.parent=t;const u=this.container.get(S(c),h);s.push(u),i.push(h.binding)}return[s,i]}getInjectProperties(t){const e=g(l.INJECTED_PROPS,this.classValue)||{},s=Object.keys(e),i=Object.create(null),o=[];for(let a=0;a<s.length;a++){const c=s[a],h=e[c],{inject:u,...f}=h;f.parent=t;const y=this.container.get(S(u),f);y===void 0&&h.optional||(i[c]=y),o.push(f.binding)}return[i,o]}}class H extends T{constructor(t){super("No matching binding found for token: ",t)}}class x extends T{constructor(t){super("Cannot bind token multiple times: ",t)}}class m{constructor(){r(this,"parent");r(this,"children");r(this,"bindings",new Map);r(this,"onActivationHandler");r(this,"onDeactivationHandler")}bind(t){if(this.bindings.has(t))throw new x(t);const e=this.buildBinding(t);return this.bindings.set(t,e),e}unbind(t){if(this.bindings.has(t)){const e=this.getBinding(t);this.deactivate(e),e.preDestroy(),this.bindings.delete(t)}}unbindAll(){this.bindings.forEach(t=>{this.unbind(t.token)})}isCurrentBound(t){return this.bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new m;return t.parent=this,this.children||(this.children=new Set),this.children.add(t),t}destroy(){var t,e,s;(e=(t=this.parent)==null?void 0:t.children)==null||e.delete(this),this.unbindAll(),this.bindings.clear(),this.parent=void 0,(s=this.children)==null||s.clear(),this.children=void 0,this.onActivationHandler=void 0,this.onDeactivationHandler=void 0}get(t,e={}){const s=this.getBinding(t);if(e.skipSelf){if(this.parent)return e.skipSelf=!1,this.parent.get(t,e)}else if(e.self||s){if(s)return e.token=t,e.binding=s,s.get(e)}else if(this.parent)return this.parent.get(t,e);return this.checkBindingNotFoundError(t,e)}onActivation(t){this.onActivationHandler=t}onDeactivation(t){this.onDeactivationHandler=t}activate(t,e){return this.onActivationHandler?this.onActivationHandler({container:this},t,e):t}deactivate(t){this.onDeactivationHandler&&this.onDeactivationHandler(t.cache,t.token),t.deactivate()}buildBinding(t){return new L(t,this)}getBinding(t){return this.bindings.get(t)}checkBindingNotFoundError(t,e){if(!e.optional)throw new H(t)}}const A=new WeakMap;function E(n,t){return function(e){return function(s,i,o){const a=typeof o=="number",c=a?s:s.constructor,h=a?o:i,u=a?l.INJECTED_PARAMS:l.INJECTED_PROPS,f=a?I(u,c)||[]:g(u,c)||{},y=f[h]||{};y[n]=e===void 0?t:e,f[h]=y,D(u,f,c)}}}function V(n,t){return e=>(s,i)=>{if(I(n,s.constructor))throw new Error(t);D(n,{key:i,value:e},s.constructor)}}const F=E(l.INJECT),J=E(l.SELF,!0),U=E(l.SKIP_SELF,!0),z=E(l.OPTIONAL,!0),G=V(l.POST_CONSTRUCT,C.POST_CONSTRUCT),Y=V(l.PRE_DESTROY,C.PRE_DESTROY);function b(n,t,e,s){for(let i=n.length-1;i>=0;i--)n[i](t,e,s)}function K(n,t,e){n=Array.isArray(n)?n:[n],typeof e=="number"?b(n,t,void 0,e):typeof e=="string"&&b(n,t.prototype,e)}function $(n,t,e,s){function i(){const a=Symbol.for(t);if(!this.hasOwnProperty(a)){const c=s||A.get(this),h=this.constructor;if(!c)throw new Error(`${C.MISS_CONTAINER} ${h.name}`);this[a]=c.get(S(e),{parent:{token:h}})}return this[a]}function o(a){const c=Symbol.for(t);this[c]=a}Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get:i,set:o})}function R(n,t){return function(e,s){$(e,s,n,t)}}function W(n){return function(t){return R(t,n)}}exports.CONTAINER_MAP=A;exports.Container=m;exports.Inject=F;exports.LazyInject=R;exports.LazyToken=N;exports.Optional=z;exports.PostConstruct=G;exports.PreDestroy=Y;exports.Self=J;exports.SkipSelf=U;exports.Token=j;exports.createLazyInject=W;exports.decorate=K;exports.defineMetadata=D;exports.getMetadata=g;exports.getOwnMetadata=I;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
var V = Object.defineProperty;
|
|
2
2
|
var R = (e, t, n) => t in e ? V(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
-
var
|
|
3
|
+
var r = (e, t, n) => R(e, typeof t != "symbol" ? t + "" : t, n);
|
|
4
4
|
const l = {
|
|
5
5
|
// 记录构造函数参数装饰器对应的数据的key
|
|
6
6
|
INJECTED_PARAMS: "injected:params",
|
|
@@ -62,14 +62,14 @@ function y(e, t) {
|
|
|
62
62
|
}
|
|
63
63
|
class J {
|
|
64
64
|
constructor(t) {
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
r(this, "_", "");
|
|
66
|
+
r(this, "name");
|
|
67
67
|
this.name = t;
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
class _ {
|
|
71
71
|
constructor(t) {
|
|
72
|
-
|
|
72
|
+
r(this, "callback");
|
|
73
73
|
this.callback = t;
|
|
74
74
|
}
|
|
75
75
|
resolve() {
|
|
@@ -109,18 +109,18 @@ class j extends P {
|
|
|
109
109
|
}
|
|
110
110
|
class M {
|
|
111
111
|
constructor(t, n) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
112
|
+
r(this, "container");
|
|
113
|
+
r(this, "context");
|
|
114
|
+
r(this, "token");
|
|
115
|
+
r(this, "type", d.Invalid);
|
|
116
|
+
r(this, "status", p.DEFAULT);
|
|
117
|
+
r(this, "classValue");
|
|
118
|
+
r(this, "constantValue");
|
|
119
|
+
r(this, "dynamicValue");
|
|
120
|
+
r(this, "cache");
|
|
121
|
+
r(this, "postConstructResult", S);
|
|
122
|
+
r(this, "onActivationHandler");
|
|
123
|
+
r(this, "onDeactivationHandler");
|
|
124
124
|
this.container = n, this.context = { container: this.container }, this.token = t;
|
|
125
125
|
}
|
|
126
126
|
onActivation(t) {
|
|
@@ -174,9 +174,9 @@ class M {
|
|
|
174
174
|
const { key: i, value: o } = y(l.POST_CONSTRUCT, this.classValue) || {};
|
|
175
175
|
if (i)
|
|
176
176
|
if (o) {
|
|
177
|
-
const
|
|
177
|
+
const a = [...n, ...s].filter(
|
|
178
178
|
(u) => d.Instance === (u == null ? void 0 : u.type)
|
|
179
|
-
), c = this.getAwaitBindings(
|
|
179
|
+
), c = this.getAwaitBindings(a, o);
|
|
180
180
|
for (const u of c)
|
|
181
181
|
if (u && u.postConstructResult === S)
|
|
182
182
|
throw new j({
|
|
@@ -208,9 +208,9 @@ class M {
|
|
|
208
208
|
resolveInstanceValue(t) {
|
|
209
209
|
this.status = p.INITING;
|
|
210
210
|
const n = this.classValue, [s, i] = this.getConstructorParameters(t), o = new n(...s);
|
|
211
|
-
this.cache = this.activate(o), this.status = p.ACTIVATED,
|
|
212
|
-
const [
|
|
213
|
-
return Object.assign(this.cache,
|
|
211
|
+
this.cache = this.activate(o), this.status = p.ACTIVATED, N.set(this.cache, this.container);
|
|
212
|
+
const [a, c] = this.getInjectProperties(t);
|
|
213
|
+
return Object.assign(this.cache, a), this.postConstruct(t, i, c), this.cache;
|
|
214
214
|
}
|
|
215
215
|
resolveConstantValue() {
|
|
216
216
|
return this.status = p.INITING, this.cache = this.activate(this.constantValue), this.status = p.ACTIVATED, this.cache;
|
|
@@ -223,7 +223,7 @@ class M {
|
|
|
223
223
|
getConstructorParameters(t) {
|
|
224
224
|
const n = I(l.INJECTED_PARAMS, this.classValue) || [], s = [], i = [];
|
|
225
225
|
for (let o = 0; o < n.length; o++) {
|
|
226
|
-
const
|
|
226
|
+
const a = n[o], { inject: c, ...h } = a;
|
|
227
227
|
h.parent = t;
|
|
228
228
|
const u = this.container.get(D(c), h);
|
|
229
229
|
s.push(u), i.push(h.binding);
|
|
@@ -232,8 +232,8 @@ class M {
|
|
|
232
232
|
}
|
|
233
233
|
getInjectProperties(t) {
|
|
234
234
|
const n = y(l.INJECTED_PROPS, this.classValue) || {}, s = Object.keys(n), i = /* @__PURE__ */ Object.create(null), o = [];
|
|
235
|
-
for (let
|
|
236
|
-
const c = s[
|
|
235
|
+
for (let a = 0; a < s.length; a++) {
|
|
236
|
+
const c = s[a], h = n[c], { inject: u, ...f } = h;
|
|
237
237
|
f.parent = t;
|
|
238
238
|
const g = this.container.get(D(u), f);
|
|
239
239
|
g === void 0 && h.optional || (i[c] = g), o.push(f.binding);
|
|
@@ -251,12 +251,13 @@ class x extends E {
|
|
|
251
251
|
super("Cannot bind token multiple times: ", t);
|
|
252
252
|
}
|
|
253
253
|
}
|
|
254
|
-
class
|
|
254
|
+
class b {
|
|
255
255
|
constructor() {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
256
|
+
r(this, "parent");
|
|
257
|
+
r(this, "children");
|
|
258
|
+
r(this, "bindings", /* @__PURE__ */ new Map());
|
|
259
|
+
r(this, "onActivationHandler");
|
|
260
|
+
r(this, "onDeactivationHandler");
|
|
260
261
|
}
|
|
261
262
|
bind(t) {
|
|
262
263
|
if (this.bindings.has(t))
|
|
@@ -282,8 +283,12 @@ class N {
|
|
|
282
283
|
return this.isCurrentBound(t) || !!this.parent && this.parent.isBound(t);
|
|
283
284
|
}
|
|
284
285
|
createChild() {
|
|
285
|
-
const t = new
|
|
286
|
-
return t.parent = this, t;
|
|
286
|
+
const t = new b();
|
|
287
|
+
return t.parent = this, this.children || (this.children = /* @__PURE__ */ new Set()), this.children.add(t), t;
|
|
288
|
+
}
|
|
289
|
+
destroy() {
|
|
290
|
+
var t, n, s;
|
|
291
|
+
(n = (t = this.parent) == null ? void 0 : t.children) == null || n.delete(this), this.unbindAll(), this.bindings.clear(), this.parent = void 0, (s = this.children) == null || s.clear(), this.children = void 0, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
|
|
287
292
|
}
|
|
288
293
|
get(t, n = {}) {
|
|
289
294
|
const s = this.getBinding(t);
|
|
@@ -320,11 +325,11 @@ class N {
|
|
|
320
325
|
throw new k(t);
|
|
321
326
|
}
|
|
322
327
|
}
|
|
323
|
-
const
|
|
328
|
+
const N = /* @__PURE__ */ new WeakMap();
|
|
324
329
|
function T(e, t) {
|
|
325
330
|
return function(n) {
|
|
326
331
|
return function(s, i, o) {
|
|
327
|
-
const
|
|
332
|
+
const a = typeof o == "number", c = a ? s : s.constructor, h = a ? o : i, u = a ? l.INJECTED_PARAMS : l.INJECTED_PROPS, f = a ? I(u, c) || [] : y(u, c) || {}, g = f[h] || {};
|
|
328
333
|
g[e] = n === void 0 ? t : n, f[h] = g, A(u, f, c);
|
|
329
334
|
};
|
|
330
335
|
};
|
|
@@ -356,20 +361,20 @@ function W(e, t, n) {
|
|
|
356
361
|
}
|
|
357
362
|
function H(e, t, n, s) {
|
|
358
363
|
function i() {
|
|
359
|
-
const
|
|
360
|
-
if (!this.hasOwnProperty(
|
|
361
|
-
const c = s ||
|
|
364
|
+
const a = Symbol.for(t);
|
|
365
|
+
if (!this.hasOwnProperty(a)) {
|
|
366
|
+
const c = s || N.get(this), h = this.constructor;
|
|
362
367
|
if (!c)
|
|
363
368
|
throw new Error(`${C.MISS_CONTAINER} ${h.name}`);
|
|
364
|
-
this[
|
|
369
|
+
this[a] = c.get(D(n), {
|
|
365
370
|
parent: { token: h }
|
|
366
371
|
});
|
|
367
372
|
}
|
|
368
|
-
return this[
|
|
373
|
+
return this[a];
|
|
369
374
|
}
|
|
370
|
-
function o(
|
|
375
|
+
function o(a) {
|
|
371
376
|
const c = Symbol.for(t);
|
|
372
|
-
this[c] =
|
|
377
|
+
this[c] = a;
|
|
373
378
|
}
|
|
374
379
|
Object.defineProperty(e, t, {
|
|
375
380
|
configurable: !0,
|
|
@@ -389,8 +394,8 @@ function q(e) {
|
|
|
389
394
|
};
|
|
390
395
|
}
|
|
391
396
|
export {
|
|
392
|
-
|
|
393
|
-
|
|
397
|
+
N as CONTAINER_MAP,
|
|
398
|
+
b as Container,
|
|
394
399
|
U as Inject,
|
|
395
400
|
L as LazyInject,
|
|
396
401
|
_ as LazyToken,
|