@kaokei/di 3.0.9 → 5.0.0
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 +27 -3
- package/dist/binding.d.cts +41 -17
- package/dist/binding.d.ts +41 -17
- package/dist/cachemap.d.cts +38 -8
- package/dist/cachemap.d.ts +38 -8
- package/dist/constants.d.cts +19 -6
- package/dist/constants.d.ts +19 -6
- package/dist/container.d.cts +11 -7
- package/dist/container.d.ts +11 -7
- package/dist/decorator.d.cts +40 -13
- package/dist/decorator.d.ts +40 -13
- package/dist/errors/BaseError.d.cts +1 -0
- package/dist/errors/BaseError.d.ts +1 -0
- package/dist/errors/ContainerNotFoundError.d.cts +5 -0
- package/dist/errors/ContainerNotFoundError.d.ts +5 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +10 -3
- package/dist/index.d.ts +10 -3
- package/dist/index.js +337 -239
- package/dist/interfaces.d.cts +1 -1
- package/dist/interfaces.d.ts +1 -1
- package/dist/token.d.cts +1 -1
- package/dist/token.d.ts +1 -1
- package/package.json +8 -3
- package/dist/lazyinject.d.cts +0 -4
- package/dist/lazyinject.d.ts +0 -4
package/dist/index.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var r = (
|
|
4
|
-
const
|
|
5
|
-
//
|
|
6
|
-
INJECTED_PARAMS: "injected:params",
|
|
7
|
-
// 记录实例属性装饰器对应的数据的key
|
|
1
|
+
var L = Object.defineProperty;
|
|
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 r = (n, t, e) => k(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
const u = {
|
|
5
|
+
// 记录实例属性装饰器对应的数据的键
|
|
8
6
|
INJECTED_PROPS: "injected:props",
|
|
9
|
-
// Inject
|
|
7
|
+
// Inject 装饰器的键
|
|
10
8
|
INJECT: "inject",
|
|
11
|
-
// Self
|
|
9
|
+
// Self 装饰器的键
|
|
12
10
|
SELF: "self",
|
|
13
|
-
// SkipSelf
|
|
11
|
+
// SkipSelf 装饰器的键
|
|
14
12
|
SKIP_SELF: "skipSelf",
|
|
15
|
-
// Optional
|
|
13
|
+
// Optional 装饰器的键
|
|
16
14
|
OPTIONAL: "optional",
|
|
17
|
-
// PostConstruct
|
|
15
|
+
// PostConstruct 装饰器的键
|
|
18
16
|
POST_CONSTRUCT: "postConstruct",
|
|
19
|
-
// PreDestroy
|
|
17
|
+
// PreDestroy 装饰器的键
|
|
20
18
|
PRE_DESTROY: "preDestroy"
|
|
21
|
-
},
|
|
19
|
+
}, f = {
|
|
22
20
|
DEFAULT: "default",
|
|
23
21
|
INITING: "initing",
|
|
24
22
|
ACTIVATED: "activated"
|
|
@@ -27,98 +25,115 @@ const l = {
|
|
|
27
25
|
Instance: "Instance",
|
|
28
26
|
ConstantValue: "ConstantValue",
|
|
29
27
|
DynamicValue: "DynamicValue"
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
}, E = {
|
|
29
|
+
// 用于 decorator.ts 的 createMetaDecorator —— 重复装饰器检测
|
|
30
|
+
POST_CONSTRUCT: "Multiple @PostConstruct decorators are not allowed in a single class.",
|
|
31
|
+
PRE_DESTROY: "Multiple @PreDestroy decorators are not allowed in a single class.",
|
|
32
|
+
// 用于 token.ts 的 resolveToken —— 无效 token
|
|
33
|
+
INVALID_TOKEN: "@Inject or @LazyInject requires a valid token, but received null or undefined.",
|
|
34
|
+
// 用于 decorator.ts 的 defineLazyProperty —— 无效 token
|
|
35
|
+
LAZY_INJECT_INVALID_TOKEN: "@LazyInject requires a valid token, but received null or undefined."
|
|
36
|
+
};
|
|
37
|
+
function g(n, t) {
|
|
38
|
+
return Object.prototype.hasOwnProperty.call(n, t);
|
|
39
|
+
}
|
|
40
|
+
const m = Symbol("UNINITIALIZED"), C = /* @__PURE__ */ new WeakMap();
|
|
41
|
+
function O(n) {
|
|
42
|
+
return typeof n == "function" && Object.getPrototypeOf(n) !== Function.prototype;
|
|
38
43
|
}
|
|
39
|
-
function
|
|
40
|
-
|
|
41
|
-
s[e] = t, S.set(n, s);
|
|
44
|
+
function P(n, t) {
|
|
45
|
+
C.set(n, t);
|
|
42
46
|
}
|
|
43
|
-
function
|
|
44
|
-
|
|
47
|
+
function S(n) {
|
|
48
|
+
const t = C.get(n);
|
|
49
|
+
if (t)
|
|
50
|
+
return t[u.POST_CONSTRUCT];
|
|
51
|
+
if (O(n))
|
|
52
|
+
return S(Object.getPrototypeOf(n));
|
|
45
53
|
}
|
|
46
|
-
function
|
|
47
|
-
const
|
|
48
|
-
if (
|
|
49
|
-
return
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
function V(n) {
|
|
55
|
+
const t = C.get(n);
|
|
56
|
+
if (t)
|
|
57
|
+
return t[u.PRE_DESTROY];
|
|
58
|
+
if (O(n))
|
|
59
|
+
return V(Object.getPrototypeOf(n));
|
|
60
|
+
}
|
|
61
|
+
function w(n) {
|
|
62
|
+
const t = C.get(n), e = t && g(t, u.INJECTED_PROPS) ? t[u.INJECTED_PROPS] : void 0;
|
|
63
|
+
if (!O(n))
|
|
64
|
+
return e;
|
|
65
|
+
const i = w(Object.getPrototypeOf(n));
|
|
66
|
+
if (i || e)
|
|
55
67
|
return {
|
|
56
|
-
...
|
|
57
|
-
...
|
|
68
|
+
...i || {},
|
|
69
|
+
...e || {}
|
|
58
70
|
};
|
|
59
71
|
}
|
|
60
|
-
class
|
|
72
|
+
class Y {
|
|
61
73
|
constructor(t) {
|
|
62
|
-
|
|
74
|
+
// 仅类型层面存在,无运行时开销
|
|
63
75
|
r(this, "name");
|
|
64
76
|
this.name = t;
|
|
65
77
|
}
|
|
66
78
|
}
|
|
67
79
|
class B {
|
|
68
80
|
constructor(t) {
|
|
69
|
-
r(this, "
|
|
70
|
-
this.
|
|
81
|
+
r(this, "_callback");
|
|
82
|
+
this._callback = t;
|
|
71
83
|
}
|
|
72
84
|
resolve() {
|
|
73
|
-
return this.
|
|
85
|
+
return this._callback();
|
|
74
86
|
}
|
|
75
87
|
}
|
|
76
|
-
function
|
|
77
|
-
if (!
|
|
78
|
-
throw new Error(
|
|
79
|
-
return
|
|
88
|
+
function b(n) {
|
|
89
|
+
if (!n)
|
|
90
|
+
throw new Error(E.INVALID_TOKEN);
|
|
91
|
+
return n instanceof B ? n.resolve() : n;
|
|
80
92
|
}
|
|
81
|
-
class
|
|
82
|
-
constructor(
|
|
83
|
-
|
|
93
|
+
class y extends Error {
|
|
94
|
+
constructor(e, i) {
|
|
95
|
+
const s = (i == null ? void 0 : i.name) || "<unknown token>";
|
|
96
|
+
super(`${e}${s}`);
|
|
97
|
+
r(this, "token");
|
|
98
|
+
this.name = this.constructor.name, this.token = i;
|
|
84
99
|
}
|
|
85
100
|
}
|
|
86
|
-
class
|
|
101
|
+
class R extends y {
|
|
87
102
|
constructor(t) {
|
|
88
103
|
super("");
|
|
89
|
-
const
|
|
90
|
-
let
|
|
91
|
-
for (;
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
this.message = `Circular dependency found: ${
|
|
104
|
+
const e = [];
|
|
105
|
+
let i = t;
|
|
106
|
+
for (; i && i.token; )
|
|
107
|
+
e.push(i.token), i = i.parent;
|
|
108
|
+
const s = e.reverse().map((o) => o.name || "<anonymous>").join(" --> ");
|
|
109
|
+
this.message = `Circular dependency found: ${s}`;
|
|
95
110
|
}
|
|
96
111
|
}
|
|
97
|
-
class
|
|
112
|
+
class H extends y {
|
|
98
113
|
constructor(t) {
|
|
99
114
|
super("Invalid binding: ", t);
|
|
100
115
|
}
|
|
101
116
|
}
|
|
102
|
-
class
|
|
117
|
+
class x extends R {
|
|
103
118
|
constructor(t) {
|
|
104
119
|
super(t), this.name = "CircularDependencyError inside @PostConstruct";
|
|
105
120
|
}
|
|
106
121
|
}
|
|
107
|
-
class
|
|
108
|
-
constructor(t,
|
|
122
|
+
const T = class T {
|
|
123
|
+
constructor(t, e) {
|
|
109
124
|
r(this, "container");
|
|
110
125
|
r(this, "context");
|
|
111
126
|
r(this, "token");
|
|
112
127
|
r(this, "type", d.Invalid);
|
|
113
|
-
r(this, "status",
|
|
128
|
+
r(this, "status", f.DEFAULT);
|
|
114
129
|
r(this, "classValue");
|
|
115
130
|
r(this, "constantValue");
|
|
116
131
|
r(this, "dynamicValue");
|
|
117
132
|
r(this, "cache");
|
|
118
|
-
r(this, "postConstructResult",
|
|
133
|
+
r(this, "postConstructResult", m);
|
|
119
134
|
r(this, "onActivationHandler");
|
|
120
135
|
r(this, "onDeactivationHandler");
|
|
121
|
-
this.container =
|
|
136
|
+
this.container = e, this.context = { container: this.container }, this.token = t;
|
|
122
137
|
}
|
|
123
138
|
onActivation(t) {
|
|
124
139
|
this.onActivationHandler = t;
|
|
@@ -127,8 +142,8 @@ class j {
|
|
|
127
142
|
this.onDeactivationHandler = t;
|
|
128
143
|
}
|
|
129
144
|
activate(t) {
|
|
130
|
-
const
|
|
131
|
-
return this.container.activate(
|
|
145
|
+
const e = this.onActivationHandler ? this.onActivationHandler(this.context, t) : t;
|
|
146
|
+
return this.container.activate(e, this.token);
|
|
132
147
|
}
|
|
133
148
|
deactivate() {
|
|
134
149
|
this.onDeactivationHandler && this.onDeactivationHandler(this.cache);
|
|
@@ -147,102 +162,125 @@ class j {
|
|
|
147
162
|
}
|
|
148
163
|
toService(t) {
|
|
149
164
|
return this.toDynamicValue(
|
|
150
|
-
(
|
|
165
|
+
(e) => e.container.get(t, { parent: { token: this.token } })
|
|
151
166
|
);
|
|
152
167
|
}
|
|
153
168
|
get(t) {
|
|
154
|
-
if (
|
|
155
|
-
throw new
|
|
156
|
-
if (
|
|
169
|
+
if (f.INITING === this.status)
|
|
170
|
+
throw new R(t);
|
|
171
|
+
if (f.ACTIVATED === this.status)
|
|
157
172
|
return this.cache;
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
173
|
+
const e = T._resolvers[this.type];
|
|
174
|
+
if (e)
|
|
175
|
+
return this[e](t);
|
|
176
|
+
throw new H(this.token);
|
|
177
|
+
}
|
|
178
|
+
_getAwaitBindings(t, e) {
|
|
179
|
+
return e === !0 ? t : Array.isArray(e) ? t.filter((i) => e.includes(i.token)) : typeof e == "function" ? t.filter(e) : [];
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* PostConstruct 生命周期处理
|
|
183
|
+
*
|
|
184
|
+
* postConstructResult 的三种状态:
|
|
185
|
+
* - UNINITIALIZED(Symbol):PostConstruct 尚未执行,用于循环依赖检测
|
|
186
|
+
* - undefined:没有使用 @PostConstruct 装饰器,或 @PostConstruct() 无参数时同步执行完毕
|
|
187
|
+
* - Promise<void>:@PostConstruct(value) 有参数时,等待前置服务初始化后异步执行
|
|
188
|
+
*
|
|
189
|
+
* @PostConstruct() 无参数时:同步执行,不等待任何前置服务
|
|
190
|
+
* @PostConstruct(value) 有参数时:等待指定的前置服务初始化完成后再执行
|
|
191
|
+
* - 如果前置服务初始化成功,执行当前服务的 PostConstruct 方法
|
|
192
|
+
* - 如果前置服务初始化失败,rejected promise 自然传播,当前服务的 PostConstruct 不执行
|
|
193
|
+
*/
|
|
194
|
+
_postConstruct(t, e) {
|
|
170
195
|
if (d.Instance === this.type) {
|
|
171
|
-
const { key: i, value:
|
|
196
|
+
const { key: i, value: s } = S(this.classValue) || {};
|
|
172
197
|
if (i)
|
|
173
|
-
if (
|
|
174
|
-
const
|
|
175
|
-
(
|
|
176
|
-
),
|
|
177
|
-
for (const
|
|
178
|
-
if (
|
|
179
|
-
throw new
|
|
180
|
-
token:
|
|
198
|
+
if (s) {
|
|
199
|
+
const o = e.filter(
|
|
200
|
+
(c) => d.Instance === (c == null ? void 0 : c.type)
|
|
201
|
+
), a = this._getAwaitBindings(o, s);
|
|
202
|
+
for (const c of a)
|
|
203
|
+
if (c && c.postConstructResult === m)
|
|
204
|
+
throw new x({
|
|
205
|
+
token: c.token,
|
|
181
206
|
parent: t
|
|
182
207
|
});
|
|
183
|
-
const
|
|
184
|
-
this.postConstructResult = Promise.all(
|
|
185
|
-
() => this.
|
|
208
|
+
const l = a.map((c) => c.postConstructResult);
|
|
209
|
+
this.postConstructResult = Promise.all(l).then(
|
|
210
|
+
() => this._execute(i)
|
|
186
211
|
);
|
|
187
212
|
} else
|
|
188
|
-
this.postConstructResult = this.
|
|
213
|
+
this.postConstructResult = this._execute(i);
|
|
189
214
|
else
|
|
190
215
|
this.postConstructResult = void 0;
|
|
191
216
|
}
|
|
192
217
|
}
|
|
193
218
|
preDestroy() {
|
|
194
219
|
if (d.Instance === this.type) {
|
|
195
|
-
const { key: t } =
|
|
196
|
-
t && this.
|
|
220
|
+
const { key: t } = V(this.classValue) || {};
|
|
221
|
+
t && this._execute(t);
|
|
197
222
|
}
|
|
198
|
-
I.
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
return
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
this.status =
|
|
206
|
-
const
|
|
207
|
-
this.cache = this.activate(
|
|
208
|
-
const
|
|
209
|
-
return
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
+
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 = m, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
|
|
224
|
+
}
|
|
225
|
+
_execute(t) {
|
|
226
|
+
const e = this.cache[t];
|
|
227
|
+
return e == null ? void 0 : e.call(this.cache);
|
|
228
|
+
}
|
|
229
|
+
_resolveInstanceValue(t) {
|
|
230
|
+
this.status = f.INITING;
|
|
231
|
+
const e = this._createInstance();
|
|
232
|
+
this.cache = this.activate(e), this.status = f.ACTIVATED, this._registerInstance();
|
|
233
|
+
const { properties: i, bindings: s } = this._getInjectProperties(t);
|
|
234
|
+
return this._injectProperties(i), this._postConstruct(t, s), this.cache;
|
|
235
|
+
}
|
|
236
|
+
// 创建类的实例
|
|
237
|
+
_createInstance() {
|
|
238
|
+
const t = this.classValue;
|
|
239
|
+
return new t();
|
|
240
|
+
}
|
|
241
|
+
// 注册实例与容器的映射关系
|
|
242
|
+
_registerInstance() {
|
|
243
|
+
I._instanceContainerMap.set(this.cache, this.container);
|
|
244
|
+
}
|
|
245
|
+
// 将解析后的属性注入到实例上
|
|
246
|
+
_injectProperties(t) {
|
|
247
|
+
Object.assign(this.cache, t);
|
|
248
|
+
}
|
|
249
|
+
_resolveConstantValue() {
|
|
250
|
+
return this.status = f.INITING, this.cache = this.activate(this.constantValue), this.status = f.ACTIVATED, this.cache;
|
|
251
|
+
}
|
|
252
|
+
_resolveDynamicValue() {
|
|
253
|
+
this.status = f.INITING;
|
|
254
|
+
const t = this.dynamicValue(this.context);
|
|
255
|
+
return this.cache = this.activate(t), this.status = f.ACTIVATED, this.cache;
|
|
256
|
+
}
|
|
257
|
+
_getInjectProperties(t) {
|
|
258
|
+
const e = w(this.classValue) || {}, i = Object.keys(e), s = /* @__PURE__ */ Object.create(null), o = [];
|
|
259
|
+
for (let a = 0; a < i.length; a++) {
|
|
260
|
+
const l = i[a], c = e[l], { inject: _, ...h } = c;
|
|
223
261
|
h.parent = t;
|
|
224
|
-
const
|
|
225
|
-
|
|
262
|
+
const p = this.container.get(
|
|
263
|
+
b(_),
|
|
264
|
+
h
|
|
265
|
+
);
|
|
266
|
+
p === void 0 && c.optional || (s[l] = p), o.push(h.binding);
|
|
226
267
|
}
|
|
227
|
-
return
|
|
228
|
-
}
|
|
229
|
-
getInjectProperties(t) {
|
|
230
|
-
const n = y(l.INJECTED_PROPS, this.classValue) || {}, s = Object.keys(n), i = /* @__PURE__ */ Object.create(null), o = [];
|
|
231
|
-
for (let a = 0; a < s.length; a++) {
|
|
232
|
-
const c = s[a], h = n[c], { inject: u, ...f } = h;
|
|
233
|
-
f.parent = t;
|
|
234
|
-
const g = this.container.get(A(u), f);
|
|
235
|
-
g === void 0 && h.optional || (i[c] = g), o.push(f.binding);
|
|
236
|
-
}
|
|
237
|
-
return [i, o];
|
|
268
|
+
return { properties: s, bindings: o };
|
|
238
269
|
}
|
|
239
|
-
}
|
|
240
|
-
|
|
270
|
+
};
|
|
271
|
+
// 类型到解析方法的静态映射表,用于替代 get 方法中的 if-else 链
|
|
272
|
+
r(T, "_resolvers", {
|
|
273
|
+
[d.Instance]: "_resolveInstanceValue",
|
|
274
|
+
[d.ConstantValue]: "_resolveConstantValue",
|
|
275
|
+
[d.DynamicValue]: "_resolveDynamicValue"
|
|
276
|
+
});
|
|
277
|
+
let A = T;
|
|
278
|
+
class M extends y {
|
|
241
279
|
constructor(t) {
|
|
242
280
|
super("No matching binding found for token: ", t);
|
|
243
281
|
}
|
|
244
282
|
}
|
|
245
|
-
class
|
|
283
|
+
class F extends y {
|
|
246
284
|
constructor(t) {
|
|
247
285
|
super("Cannot bind token multiple times: ", t);
|
|
248
286
|
}
|
|
@@ -251,29 +289,33 @@ const v = class v {
|
|
|
251
289
|
constructor() {
|
|
252
290
|
r(this, "parent");
|
|
253
291
|
r(this, "children");
|
|
254
|
-
r(this, "
|
|
255
|
-
r(this, "
|
|
256
|
-
r(this, "
|
|
292
|
+
r(this, "_bindings", /* @__PURE__ */ new Map());
|
|
293
|
+
r(this, "_onActivationHandler");
|
|
294
|
+
r(this, "_onDeactivationHandler");
|
|
295
|
+
}
|
|
296
|
+
// 查询实例所属的容器
|
|
297
|
+
static getContainerOf(t) {
|
|
298
|
+
return v._instanceContainerMap.get(t);
|
|
257
299
|
}
|
|
258
300
|
bind(t) {
|
|
259
|
-
if (this.
|
|
260
|
-
throw new
|
|
261
|
-
const
|
|
262
|
-
return this.
|
|
301
|
+
if (this._bindings.has(t))
|
|
302
|
+
throw new F(t);
|
|
303
|
+
const e = this._buildBinding(t);
|
|
304
|
+
return this._bindings.set(t, e), e;
|
|
263
305
|
}
|
|
264
306
|
unbind(t) {
|
|
265
|
-
if (this.
|
|
266
|
-
const
|
|
267
|
-
this.deactivate(
|
|
307
|
+
if (this._bindings.has(t)) {
|
|
308
|
+
const e = this._getBinding(t);
|
|
309
|
+
this.deactivate(e), e.deactivate(), e.preDestroy(), this._bindings.delete(t);
|
|
268
310
|
}
|
|
269
311
|
}
|
|
270
312
|
unbindAll() {
|
|
271
|
-
this.
|
|
272
|
-
|
|
273
|
-
|
|
313
|
+
const t = Array.from(this._bindings.keys());
|
|
314
|
+
for (const e of t)
|
|
315
|
+
this.unbind(e);
|
|
274
316
|
}
|
|
275
317
|
isCurrentBound(t) {
|
|
276
|
-
return this.
|
|
318
|
+
return this._bindings.has(t);
|
|
277
319
|
}
|
|
278
320
|
isBound(t) {
|
|
279
321
|
return this.isCurrentBound(t) || !!this.parent && this.parent.isBound(t);
|
|
@@ -283,124 +325,180 @@ const v = class v {
|
|
|
283
325
|
return t.parent = this, this.children || (this.children = /* @__PURE__ */ new Set()), this.children.add(t), t;
|
|
284
326
|
}
|
|
285
327
|
destroy() {
|
|
286
|
-
var t,
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
return this.
|
|
328
|
+
var t, e;
|
|
329
|
+
if (this.children) {
|
|
330
|
+
const i = Array.from(this.children);
|
|
331
|
+
for (const s of i)
|
|
332
|
+
s.destroy();
|
|
333
|
+
}
|
|
334
|
+
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;
|
|
335
|
+
}
|
|
336
|
+
get(t, e = {}) {
|
|
337
|
+
return e.skipSelf ? this._resolveSkipSelf(t, e) : e.self ? this._resolveSelf(t, e) : this._resolveDefault(t, e);
|
|
338
|
+
}
|
|
339
|
+
// 处理 skipSelf 选项:跳过当前容器,委托父容器解析
|
|
340
|
+
_resolveSkipSelf(t, e) {
|
|
341
|
+
return this.parent ? (e.skipSelf = !1, this.parent.get(t, e)) : this._checkBindingNotFoundError(t, e);
|
|
342
|
+
}
|
|
343
|
+
// 处理 self 选项:仅在当前容器中查找
|
|
344
|
+
_resolveSelf(t, e) {
|
|
345
|
+
const i = this._getBinding(t);
|
|
346
|
+
return i ? (e.token = t, e.binding = i, i.get(e)) : this._checkBindingNotFoundError(t, e);
|
|
347
|
+
}
|
|
348
|
+
// 默认解析流程:当前容器 → 父容器 → 抛错
|
|
349
|
+
_resolveDefault(t, e) {
|
|
350
|
+
const i = this._getBinding(t);
|
|
351
|
+
return i ? (e.token = t, e.binding = i, i.get(e)) : this.parent ? this.parent.get(t, e) : this._checkBindingNotFoundError(t, e);
|
|
300
352
|
}
|
|
301
353
|
onActivation(t) {
|
|
302
|
-
this.
|
|
354
|
+
this._onActivationHandler = t;
|
|
303
355
|
}
|
|
304
356
|
onDeactivation(t) {
|
|
305
|
-
this.
|
|
357
|
+
this._onDeactivationHandler = t;
|
|
306
358
|
}
|
|
307
|
-
activate(t,
|
|
308
|
-
return this.
|
|
359
|
+
activate(t, e) {
|
|
360
|
+
return this._onActivationHandler ? this._onActivationHandler({ container: this }, t, e) : t;
|
|
309
361
|
}
|
|
310
362
|
deactivate(t) {
|
|
311
|
-
this.
|
|
363
|
+
this._onDeactivationHandler && this._onDeactivationHandler(t.cache, t.token);
|
|
312
364
|
}
|
|
313
|
-
|
|
314
|
-
return new
|
|
365
|
+
_buildBinding(t) {
|
|
366
|
+
return new A(t, this);
|
|
315
367
|
}
|
|
316
|
-
|
|
317
|
-
return this.
|
|
368
|
+
_getBinding(t) {
|
|
369
|
+
return this._bindings.get(t);
|
|
318
370
|
}
|
|
319
|
-
|
|
320
|
-
if (!
|
|
321
|
-
throw new
|
|
371
|
+
_checkBindingNotFoundError(t, e) {
|
|
372
|
+
if (!e.optional)
|
|
373
|
+
throw new M(t);
|
|
322
374
|
}
|
|
323
375
|
};
|
|
324
|
-
|
|
376
|
+
// 实例到容器的映射表,用于 @LazyInject 查找实例所属容器
|
|
377
|
+
// 仅 Instance 类型的 binding 会注册映射(_registerInstance 仅在 _resolveInstanceValue 中调用)
|
|
378
|
+
// toConstantValue 和 toDynamicValue 不会触发注册,原因:
|
|
379
|
+
// 同一对象可能通过 toConstantValue 被绑定到多个容器,WeakMap 只能保留最后一次映射,
|
|
380
|
+
// 会导致 @LazyInject 从错误的容器解析依赖。
|
|
381
|
+
// 由于 Instance 类型每次都通过 new ClassName() 创建新实例,不存在同一实例被多个容器注册的覆盖风险
|
|
382
|
+
r(v, "_instanceContainerMap", /* @__PURE__ */ new WeakMap());
|
|
325
383
|
let I = v;
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
384
|
+
class z extends y {
|
|
385
|
+
constructor(t, e) {
|
|
386
|
+
super(
|
|
387
|
+
`@LazyInject(${t == null ? void 0 : t.name}) in class ${e.name} requires a registered container but none was found. Token: `,
|
|
388
|
+
t
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function D(n, t) {
|
|
393
|
+
return function(e) {
|
|
394
|
+
return function(i, s) {
|
|
395
|
+
const o = s.name, a = s.metadata;
|
|
396
|
+
g(a, u.INJECTED_PROPS) || (a[u.INJECTED_PROPS] = {});
|
|
397
|
+
const l = a[u.INJECTED_PROPS];
|
|
398
|
+
l[o] || (l[o] = {}), l[o][n] = e === void 0 ? t : e;
|
|
331
399
|
};
|
|
332
400
|
};
|
|
333
401
|
}
|
|
334
|
-
function
|
|
335
|
-
return (
|
|
336
|
-
|
|
402
|
+
function j(n, t) {
|
|
403
|
+
return (e) => (i, s) => {
|
|
404
|
+
const o = s.name, a = s.metadata;
|
|
405
|
+
if (g(a, n))
|
|
337
406
|
throw new Error(t);
|
|
338
|
-
|
|
339
|
-
e,
|
|
340
|
-
{ key: i, value: n },
|
|
341
|
-
s.constructor
|
|
342
|
-
);
|
|
407
|
+
a[n] = { key: o, value: e };
|
|
343
408
|
};
|
|
344
409
|
}
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
),
|
|
349
|
-
|
|
350
|
-
|
|
410
|
+
const G = D(u.INJECT), $ = D(u.SELF, !0), Z = D(u.SKIP_SELF, !0), q = D(u.OPTIONAL, !0), W = j(
|
|
411
|
+
u.POST_CONSTRUCT,
|
|
412
|
+
E.POST_CONSTRUCT
|
|
413
|
+
), Q = j(
|
|
414
|
+
u.PRE_DESTROY,
|
|
415
|
+
E.PRE_DESTROY
|
|
351
416
|
);
|
|
352
|
-
function
|
|
353
|
-
|
|
354
|
-
|
|
417
|
+
function X(n, t) {
|
|
418
|
+
const e = t.metadata;
|
|
419
|
+
P(n, e);
|
|
355
420
|
}
|
|
356
|
-
function
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
const a = Symbol.for(t);
|
|
362
|
-
if (!this.hasOwnProperty(a)) {
|
|
363
|
-
const c = s || I.map.get(this), h = this.constructor;
|
|
364
|
-
if (!c)
|
|
365
|
-
throw new Error(`${C.MISS_CONTAINER} ${h.name}`);
|
|
366
|
-
this[a] = c.get(A(n), {
|
|
367
|
-
parent: { token: h }
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
return this[a];
|
|
371
|
-
}
|
|
372
|
-
function o(a) {
|
|
373
|
-
const c = Symbol.for(t);
|
|
374
|
-
this[c] = a;
|
|
375
|
-
}
|
|
376
|
-
Object.defineProperty(e, t, {
|
|
421
|
+
function J(n, t, e, i) {
|
|
422
|
+
if (e == null)
|
|
423
|
+
throw new Error(E.LAZY_INJECT_INVALID_TOKEN);
|
|
424
|
+
const s = Symbol.for(t);
|
|
425
|
+
Object.defineProperty(n, t, {
|
|
377
426
|
configurable: !0,
|
|
378
427
|
enumerable: !0,
|
|
379
|
-
get
|
|
380
|
-
|
|
428
|
+
get() {
|
|
429
|
+
if (!g(n, s)) {
|
|
430
|
+
const o = i || I.getContainerOf(n), a = n.constructor;
|
|
431
|
+
if (!o)
|
|
432
|
+
throw new z(b(e), a);
|
|
433
|
+
n[s] = o.get(b(e), {
|
|
434
|
+
parent: { token: a }
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
return n[s];
|
|
438
|
+
},
|
|
439
|
+
set(o) {
|
|
440
|
+
n[s] = o;
|
|
441
|
+
}
|
|
381
442
|
});
|
|
382
443
|
}
|
|
383
|
-
function
|
|
384
|
-
return function(
|
|
385
|
-
|
|
444
|
+
function U(n, t) {
|
|
445
|
+
return function(e, i) {
|
|
446
|
+
const s = i.name;
|
|
447
|
+
i.addInitializer(function() {
|
|
448
|
+
J(this, s, n, t);
|
|
449
|
+
});
|
|
386
450
|
};
|
|
387
451
|
}
|
|
388
|
-
function
|
|
452
|
+
function tt(n) {
|
|
389
453
|
return function(t) {
|
|
390
|
-
return
|
|
454
|
+
return U(t, n);
|
|
391
455
|
};
|
|
392
456
|
}
|
|
457
|
+
const N = Symbol("decorate.metadata");
|
|
458
|
+
function et(n, t, e) {
|
|
459
|
+
const i = Array.isArray(n) ? n : [n], s = t.prototype, o = typeof s[e] == "function", a = [];
|
|
460
|
+
g(t, N) || (t[N] = {});
|
|
461
|
+
const l = t[N], c = {
|
|
462
|
+
kind: o ? "method" : "field",
|
|
463
|
+
name: e,
|
|
464
|
+
static: !1,
|
|
465
|
+
private: !1,
|
|
466
|
+
addInitializer(h) {
|
|
467
|
+
a.push(h);
|
|
468
|
+
},
|
|
469
|
+
metadata: l
|
|
470
|
+
};
|
|
471
|
+
let _ = o ? s[e] : void 0;
|
|
472
|
+
for (let h = i.length - 1; h >= 0; h--) {
|
|
473
|
+
const p = i[h](_, c);
|
|
474
|
+
o && typeof p == "function" && (_ = p);
|
|
475
|
+
}
|
|
476
|
+
if (o && _ !== s[e] && (s[e] = _), P(t, l), a.length > 0) {
|
|
477
|
+
const h = Object.create(s);
|
|
478
|
+
for (const p of a)
|
|
479
|
+
p.call(h);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
393
482
|
export {
|
|
483
|
+
y as BaseError,
|
|
484
|
+
A as Binding,
|
|
485
|
+
M as BindingNotFoundError,
|
|
486
|
+
H as BindingNotValidError,
|
|
487
|
+
R as CircularDependencyError,
|
|
394
488
|
I as Container,
|
|
395
|
-
|
|
396
|
-
|
|
489
|
+
z as ContainerNotFoundError,
|
|
490
|
+
F as DuplicateBindingError,
|
|
491
|
+
G as Inject,
|
|
492
|
+
X as Injectable,
|
|
493
|
+
U as LazyInject,
|
|
397
494
|
B as LazyToken,
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
495
|
+
q as Optional,
|
|
496
|
+
W as PostConstruct,
|
|
497
|
+
x as PostConstructError,
|
|
498
|
+
Q as PreDestroy,
|
|
499
|
+
$ as Self,
|
|
500
|
+
Z as SkipSelf,
|
|
501
|
+
Y as Token,
|
|
502
|
+
tt as createLazyInject,
|
|
503
|
+
et as decorate
|
|
406
504
|
};
|