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