@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/dist/index.js CHANGED
@@ -1,24 +1,22 @@
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
- var r = (e, t, n) => R(e, typeof t != "symbol" ? t + "" : t, n);
4
- const l = {
5
- // 记录构造函数参数装饰器对应的数据的key
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装饰器的key
7
+ // Inject 装饰器的键
10
8
  INJECT: "inject",
11
- // Self装饰器的key
9
+ // Self 装饰器的键
12
10
  SELF: "self",
13
- // SkipSelf装饰器的key
11
+ // SkipSelf 装饰器的键
14
12
  SKIP_SELF: "skipSelf",
15
- // Optional装饰器的key
13
+ // Optional 装饰器的键
16
14
  OPTIONAL: "optional",
17
- // PostConstruct装饰器的key
15
+ // PostConstruct 装饰器的键
18
16
  POST_CONSTRUCT: "postConstruct",
19
- // PreDestroy装饰器的key
17
+ // PreDestroy 装饰器的键
20
18
  PRE_DESTROY: "preDestroy"
21
- }, p = {
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
- }, C = {
31
- POST_CONSTRUCT: "Cannot apply @PostConstruct decorator multiple times in the same class.",
32
- PRE_DESTROY: "Cannot apply @PreDestroy decorator multiple times in the same class.",
33
- MISS_INJECT: "Expected a @Inject decorator to explicitly specify the token.",
34
- MISS_CONTAINER: "@LazyInject decorator cannot find the corresponding container."
35
- }, D = Symbol(), S = /* @__PURE__ */ new WeakMap();
36
- function w(e) {
37
- return typeof e == "function" && Object.getPrototypeOf(e) !== Function.prototype;
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 b(e, t, n) {
40
- const s = S.get(n) || {};
41
- s[e] = t, S.set(n, s);
44
+ function P(n, t) {
45
+ C.set(n, t);
42
46
  }
43
- function E(e, t) {
44
- return (S.get(t) || {})[e];
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 y(e, t) {
47
- const n = E(e, t);
48
- if (!w(t))
49
- return n;
50
- const s = y(
51
- e,
52
- Object.getPrototypeOf(t)
53
- );
54
- if (s || n)
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
- ...s || {},
57
- ...n || {}
68
+ ...i || {},
69
+ ...e || {}
58
70
  };
59
71
  }
60
- class J {
72
+ class Y {
61
73
  constructor(t) {
62
- r(this, "_", "");
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, "callback");
70
- this.callback = t;
81
+ r(this, "_callback");
82
+ this._callback = t;
71
83
  }
72
84
  resolve() {
73
- return this.callback();
85
+ return this._callback();
74
86
  }
75
87
  }
76
- function A(e) {
77
- if (!e)
78
- throw new Error(C.MISS_INJECT);
79
- return e instanceof B ? e.resolve() : e;
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 T extends Error {
82
- constructor(t, n) {
83
- super(), this.name = this.constructor.name, this.message = `${t}${n == null ? void 0 : n.name}`;
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 V extends T {
101
+ class R extends y {
87
102
  constructor(t) {
88
103
  super("");
89
- const n = [];
90
- let s = t;
91
- for (; s && s.token; )
92
- n.push(s.token), s = s.parent;
93
- const i = n.reverse().map((o) => o.name).join(" --> ");
94
- this.message = `Circular dependency found: ${i}`;
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 _ extends T {
112
+ class H extends y {
98
113
  constructor(t) {
99
114
  super("Invalid binding: ", t);
100
115
  }
101
116
  }
102
- class H extends V {
117
+ class x extends R {
103
118
  constructor(t) {
104
119
  super(t), this.name = "CircularDependencyError inside @PostConstruct";
105
120
  }
106
121
  }
107
- class j {
108
- constructor(t, n) {
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", p.DEFAULT);
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", D);
133
+ r(this, "postConstructResult", m);
119
134
  r(this, "onActivationHandler");
120
135
  r(this, "onDeactivationHandler");
121
- this.container = n, this.context = { container: this.container }, this.token = t;
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 n = this.onActivationHandler ? this.onActivationHandler(this.context, t) : t;
131
- return this.container.activate(n, this.token);
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
- (n) => n.container.get(t, { parent: { token: this.token } })
165
+ (e) => e.container.get(t, { parent: { token: this.token } })
151
166
  );
152
167
  }
153
168
  get(t) {
154
- if (p.INITING === this.status)
155
- throw new V(t);
156
- if (p.ACTIVATED === this.status)
169
+ if (f.INITING === this.status)
170
+ throw new R(t);
171
+ if (f.ACTIVATED === this.status)
157
172
  return this.cache;
158
- if (d.Instance === this.type)
159
- return this.resolveInstanceValue(t);
160
- if (d.ConstantValue === this.type)
161
- return this.resolveConstantValue();
162
- if (d.DynamicValue === this.type)
163
- return this.resolveDynamicValue();
164
- throw new _(this.token);
165
- }
166
- getAwaitBindings(t, n) {
167
- return n === !0 ? t : Array.isArray(n) ? t.filter((s) => n.includes(s.token)) : typeof n == "function" ? t.filter(n) : [];
168
- }
169
- postConstruct(t, n, s) {
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: o } = y(l.POST_CONSTRUCT, this.classValue) || {};
196
+ const { key: i, value: s } = S(this.classValue) || {};
172
197
  if (i)
173
- if (o) {
174
- const a = [...n, ...s].filter(
175
- (u) => d.Instance === (u == null ? void 0 : u.type)
176
- ), c = this.getAwaitBindings(a, o);
177
- for (const u of c)
178
- if (u && u.postConstructResult === D)
179
- throw new H({
180
- token: u.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 h = c.map((u) => u.postConstructResult);
184
- this.postConstructResult = Promise.all(h).then(
185
- () => this.execute(i)
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.execute(i);
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 } = y(l.PRE_DESTROY, this.classValue) || {};
196
- t && this.execute(t);
220
+ const { key: t } = V(this.classValue) || {};
221
+ t && this._execute(t);
197
222
  }
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;
199
- }
200
- execute(t) {
201
- const n = this.cache[t];
202
- return n == null ? void 0 : n.call(this.cache);
203
- }
204
- resolveInstanceValue(t) {
205
- this.status = p.INITING;
206
- const n = this.classValue, [s, i] = this.getConstructorParameters(t), o = new n(...s);
207
- this.cache = this.activate(o), this.status = p.ACTIVATED, I.map.set(this.cache, this.container);
208
- const [a, c] = this.getInjectProperties(t);
209
- return Object.assign(this.cache, a), this.postConstruct(t, i, c), this.cache;
210
- }
211
- resolveConstantValue() {
212
- return this.status = p.INITING, this.cache = this.activate(this.constantValue), this.status = p.ACTIVATED, this.cache;
213
- }
214
- resolveDynamicValue() {
215
- this.status = p.INITING;
216
- const t = this.dynamicValue.call(this, this.context);
217
- return this.cache = this.activate(t), this.status = p.ACTIVATED, this.cache;
218
- }
219
- getConstructorParameters(t) {
220
- const n = E(l.INJECTED_PARAMS, this.classValue) || [], s = [], i = [];
221
- for (let o = 0; o < n.length; o++) {
222
- const a = n[o], { inject: c, ...h } = a;
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 u = this.container.get(A(c), h);
225
- s.push(u), i.push(h.binding);
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 [s, i];
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
- class k extends T {
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 x extends T {
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, "bindings", /* @__PURE__ */ new Map());
255
- r(this, "onActivationHandler");
256
- r(this, "onDeactivationHandler");
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.bindings.has(t))
260
- throw new x(t);
261
- const n = this.buildBinding(t);
262
- return this.bindings.set(t, n), n;
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.bindings.has(t)) {
266
- const n = this.getBinding(t);
267
- this.deactivate(n), n.deactivate(), n.preDestroy(), this.bindings.delete(t);
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.bindings.forEach((t) => {
272
- this.unbind(t.token);
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.bindings.has(t);
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, n, s;
287
- this.unbindAll(), this.bindings.clear(), (n = (t = this.parent) == null ? void 0 : t.children) == null || n.delete(this), this.parent = void 0, (s = this.children) == null || s.clear(), this.children = void 0, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
288
- }
289
- get(t, n = {}) {
290
- const s = this.getBinding(t);
291
- if (n.skipSelf) {
292
- if (this.parent)
293
- return n.skipSelf = !1, this.parent.get(t, n);
294
- } else if (n.self || s) {
295
- if (s)
296
- return n.token = t, n.binding = s, s.get(n);
297
- } else if (this.parent)
298
- return this.parent.get(t, n);
299
- return this.checkBindingNotFoundError(t, n);
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.onActivationHandler = t;
354
+ this._onActivationHandler = t;
303
355
  }
304
356
  onDeactivation(t) {
305
- this.onDeactivationHandler = t;
357
+ this._onDeactivationHandler = t;
306
358
  }
307
- activate(t, n) {
308
- return this.onActivationHandler ? this.onActivationHandler({ container: this }, t, n) : t;
359
+ activate(t, e) {
360
+ return this._onActivationHandler ? this._onActivationHandler({ container: this }, t, e) : t;
309
361
  }
310
362
  deactivate(t) {
311
- this.onDeactivationHandler && this.onDeactivationHandler(t.cache, t.token);
363
+ this._onDeactivationHandler && this._onDeactivationHandler(t.cache, t.token);
312
364
  }
313
- buildBinding(t) {
314
- return new j(t, this);
365
+ _buildBinding(t) {
366
+ return new A(t, this);
315
367
  }
316
- getBinding(t) {
317
- return this.bindings.get(t);
368
+ _getBinding(t) {
369
+ return this._bindings.get(t);
318
370
  }
319
- checkBindingNotFoundError(t, n) {
320
- if (!n.optional)
321
- throw new k(t);
371
+ _checkBindingNotFoundError(t, e) {
372
+ if (!e.optional)
373
+ throw new M(t);
322
374
  }
323
375
  };
324
- r(v, "map", /* @__PURE__ */ new WeakMap());
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
- function m(e, t) {
327
- return function(n) {
328
- return function(s, i, o) {
329
- const a = typeof o == "number", c = a ? s : s.constructor, h = a ? o : i, u = a ? l.INJECTED_PARAMS : l.INJECTED_PROPS, f = a ? E(u, c) || [] : y(u, c) || {}, g = f[h] || {};
330
- g[e] = n === void 0 ? t : n, f[h] = g, b(u, f, c);
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 N(e, t) {
335
- return (n) => (s, i) => {
336
- if (E(e, s.constructor))
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
- b(
339
- e,
340
- { key: i, value: n },
341
- s.constructor
342
- );
407
+ a[n] = { key: o, value: e };
343
408
  };
344
409
  }
345
- const U = m(l.INJECT), G = m(l.SELF, !0), Y = m(l.SKIP_SELF, !0), z = m(l.OPTIONAL, !0), K = N(
346
- l.POST_CONSTRUCT,
347
- C.POST_CONSTRUCT
348
- ), $ = N(
349
- l.PRE_DESTROY,
350
- C.PRE_DESTROY
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 P(e, t, n, s) {
353
- for (let i = e.length - 1; i >= 0; i--)
354
- e[i](t, n, s);
417
+ function X(n, t) {
418
+ const e = t.metadata;
419
+ P(n, e);
355
420
  }
356
- function W(e, t, n) {
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, {
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: i,
380
- set: o
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 L(e, t) {
384
- return function(n, s) {
385
- M(n, s, e, t);
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 q(e) {
452
+ function tt(n) {
389
453
  return function(t) {
390
- return L(t, e);
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
- U as Inject,
396
- L as LazyInject,
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
- z as Optional,
399
- K as PostConstruct,
400
- $ as PreDestroy,
401
- G as Self,
402
- Y as SkipSelf,
403
- J as Token,
404
- q as createLazyInject,
405
- W as decorate
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
  };