@lppedd/di-wise-neo 0.5.1 → 0.5.3
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/cjs/index.d.ts +19 -1
- package/dist/cjs/index.js +40 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.d.mts +19 -1
- package/dist/es/index.mjs +40 -4
- package/dist/es/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/es/index.mjs
CHANGED
@@ -154,16 +154,35 @@ function injectAll(token) {
|
|
154
154
|
return context.container.resolveAll(token);
|
155
155
|
}
|
156
156
|
|
157
|
+
/**
|
158
|
+
* Registers a mapping between a generated (e.g., decorated or proxied) constructor
|
159
|
+
* and its original, underlying constructor.
|
160
|
+
*
|
161
|
+
* This allows libraries or consumers that manipulate constructors, such as through
|
162
|
+
* class decorators, to inform the DI system about the real "identity" of a class.
|
163
|
+
*
|
164
|
+
* @param transformedClass The constructor function returned by a class decorator or factory.
|
165
|
+
* @param originalClass The original constructor function.
|
166
|
+
*
|
167
|
+
* @remarks
|
168
|
+
* This API affects the core class identity resolution mechanism of the DI system.
|
169
|
+
* Incorrect usage may cause metadata to be misassociated, leading to subtle errors.
|
170
|
+
* Use only when manipulating constructors (e.g., via decorators or proxies),
|
171
|
+
* and ensure the mapping is correct.
|
172
|
+
*/ function setClassIdentityMapping(transformedClass, originalClass) {
|
173
|
+
classIdentityMap.set(transformedClass, originalClass);
|
174
|
+
}
|
157
175
|
// @internal
|
158
176
|
function getMetadata(Class) {
|
159
|
-
|
177
|
+
const originalClass = classIdentityMap.get(Class) ?? Class;
|
178
|
+
let metadata = metadataMap.get(originalClass);
|
160
179
|
if (!metadata) {
|
161
|
-
metadataMap.set(
|
180
|
+
metadataMap.set(originalClass, metadata = {
|
162
181
|
tokensRef: {
|
163
182
|
getRefTokens: ()=>new Set()
|
164
183
|
},
|
165
184
|
provider: {
|
166
|
-
useClass:
|
185
|
+
useClass: originalClass
|
167
186
|
},
|
168
187
|
dependencies: {
|
169
188
|
constructor: [],
|
@@ -171,8 +190,25 @@ function getMetadata(Class) {
|
|
171
190
|
}
|
172
191
|
});
|
173
192
|
}
|
193
|
+
if (metadata.provider.useClass !== Class) {
|
194
|
+
// This is part of the class identity mapping API (see setClassIdentityMapping).
|
195
|
+
//
|
196
|
+
// Scenario:
|
197
|
+
// 1. Metadata is created for class A (the original class) because of a parameter decorator.
|
198
|
+
// 2. Later, because of a class decorator that extends the decorated class, a third-party
|
199
|
+
// registers a class identity mapping from class B to class A, where B is the class
|
200
|
+
// generated from the class decorator by extending A.
|
201
|
+
//
|
202
|
+
// We must update useClass to be the extender class B so that instances created by the
|
203
|
+
// DI container match the consumer's registered class. Without this update, the DI
|
204
|
+
// system would instantiate the original class A, causing behavior inconsistencies.
|
205
|
+
//
|
206
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
207
|
+
metadata.provider.useClass = Class;
|
208
|
+
}
|
174
209
|
return metadata;
|
175
210
|
}
|
211
|
+
const classIdentityMap = new WeakMap();
|
176
212
|
const metadataMap = new WeakMap();
|
177
213
|
|
178
214
|
function optional(token) {
|
@@ -1100,5 +1136,5 @@ function OptionalAll(token) {
|
|
1100
1136
|
return container;
|
1101
1137
|
}
|
1102
1138
|
|
1103
|
-
export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy };
|
1139
|
+
export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy, setClassIdentityMapping };
|
1104
1140
|
//# sourceMappingURL=index.mjs.map
|