@vercube/di 0.0.24 → 0.0.26
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.d.mts +9 -1
- package/dist/index.mjs +33 -8
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -53,6 +53,7 @@ declare class ContainerEvents {
|
|
|
53
53
|
* simpler and (probably) more performant on larger scales.
|
|
54
54
|
*/
|
|
55
55
|
declare class Container {
|
|
56
|
+
protected fContext: string | undefined;
|
|
56
57
|
protected fLocked: boolean;
|
|
57
58
|
protected fDefaultParams: IOC.ContainerParams;
|
|
58
59
|
protected fServices: Map<IOC.ServiceKey, IOC.ServiceDef>;
|
|
@@ -63,6 +64,7 @@ declare class Container {
|
|
|
63
64
|
/**
|
|
64
65
|
* Constructor for container.
|
|
65
66
|
* @param params initial params for container
|
|
67
|
+
* @param context the context of the container
|
|
66
68
|
*/
|
|
67
69
|
constructor(params?: Partial<IOC.ContainerParams>);
|
|
68
70
|
/**
|
|
@@ -74,6 +76,11 @@ declare class Container {
|
|
|
74
76
|
* Returns events handler.
|
|
75
77
|
*/
|
|
76
78
|
get events(): ContainerEvents;
|
|
79
|
+
/**
|
|
80
|
+
* Returns the context of the container.
|
|
81
|
+
* @returns {string} the context of the container
|
|
82
|
+
*/
|
|
83
|
+
get context(): string | undefined;
|
|
77
84
|
/**
|
|
78
85
|
* Binds particular key to container in singleton scope. Multiple queries/injects of this
|
|
79
86
|
* service will always return the same instance.
|
|
@@ -210,7 +217,7 @@ declare namespace IOC {
|
|
|
210
217
|
* This is a key that we use to identify service. Symbol is new way of doing it, however we
|
|
211
218
|
* also keep standard/abstract classes for backward compatability.
|
|
212
219
|
*/
|
|
213
|
-
type ServiceKey<T = unknown> = symbol | Newable<T> | Abstract<T>;
|
|
220
|
+
type ServiceKey<T = unknown> = string | symbol | Newable<T> | Abstract<T>;
|
|
214
221
|
/**
|
|
215
222
|
* This type holds implementation that might be used in class.
|
|
216
223
|
*/
|
|
@@ -261,6 +268,7 @@ declare namespace IOC {
|
|
|
261
268
|
interface ContainerParams {
|
|
262
269
|
createLocked: boolean;
|
|
263
270
|
injectMethod?: IOC.InjectMethod;
|
|
271
|
+
context?: string;
|
|
264
272
|
}
|
|
265
273
|
/**
|
|
266
274
|
* Method for injecting dependencies.
|
package/dist/index.mjs
CHANGED
|
@@ -161,8 +161,12 @@ var ContainerEvents = class {
|
|
|
161
161
|
* simpler and (probably) more performant on larger scales.
|
|
162
162
|
*/
|
|
163
163
|
var Container = class Container {
|
|
164
|
+
fContext;
|
|
164
165
|
fLocked = false;
|
|
165
|
-
fDefaultParams = {
|
|
166
|
+
fDefaultParams = {
|
|
167
|
+
context: void 0,
|
|
168
|
+
createLocked: false
|
|
169
|
+
};
|
|
166
170
|
fServices = /* @__PURE__ */ new Map();
|
|
167
171
|
fNewQueue = /* @__PURE__ */ new Map();
|
|
168
172
|
fSingletonInstances = /* @__PURE__ */ new Map();
|
|
@@ -171,8 +175,10 @@ var Container = class Container {
|
|
|
171
175
|
/**
|
|
172
176
|
* Constructor for container.
|
|
173
177
|
* @param params initial params for container
|
|
178
|
+
* @param context the context of the container
|
|
174
179
|
*/
|
|
175
180
|
constructor(params) {
|
|
181
|
+
this.fContext = params?.context ?? "default";
|
|
176
182
|
this.fLocked = params?.createLocked ?? false;
|
|
177
183
|
this.fDefaultParams = Object.assign(this.fDefaultParams, params);
|
|
178
184
|
this.fInjectMethod = params?.injectMethod ?? IOC.InjectMethod.STATIC;
|
|
@@ -192,6 +198,13 @@ var Container = class Container {
|
|
|
192
198
|
return this.fContainerEvents;
|
|
193
199
|
}
|
|
194
200
|
/**
|
|
201
|
+
* Returns the context of the container.
|
|
202
|
+
* @returns {string} the context of the container
|
|
203
|
+
*/
|
|
204
|
+
get context() {
|
|
205
|
+
return this.fContext;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
195
208
|
* Binds particular key to container in singleton scope. Multiple queries/injects of this
|
|
196
209
|
* service will always return the same instance.
|
|
197
210
|
*
|
|
@@ -355,8 +368,7 @@ var Container = class Container {
|
|
|
355
368
|
const values = [...this.fNewQueue.values()];
|
|
356
369
|
for (const def of values) {
|
|
357
370
|
if (def.type !== IOC.ServiceFactoryType.CLASS_SINGLETON) continue;
|
|
358
|
-
|
|
359
|
-
initializeDecorators(instance, this);
|
|
371
|
+
initializeDecorators(this.internalResolve(def), this);
|
|
360
372
|
}
|
|
361
373
|
this.fNewQueue.clear();
|
|
362
374
|
}
|
|
@@ -502,8 +514,21 @@ let IOC;
|
|
|
502
514
|
* container (<1ms).
|
|
503
515
|
*/
|
|
504
516
|
const classMap = /* @__PURE__ */ new Map();
|
|
517
|
+
globalThis.__IOCClassMap = globalThis.__IOCClassMap ?? classMap;
|
|
505
518
|
const ROOT_PROTO = Object.getPrototypeOf({});
|
|
506
519
|
/**
|
|
520
|
+
* Retrieves the class dependency metadata map used for IOC injection purposes.
|
|
521
|
+
*
|
|
522
|
+
* This function checks for a global `classMap` property on `globalThis` (allowing for shared class metadata between
|
|
523
|
+
* multiple contexts or modules, e.g., in hot-reloading environments), and falls back to the internal `classMap` instance
|
|
524
|
+
* if one does not exist. The returned map associates class prototypes with their corresponding inject metadata entries.
|
|
525
|
+
*
|
|
526
|
+
* @returns {Map<IOC.Prototype, IClassMapEntry>} The map of class prototypes to injection metadata entries.
|
|
527
|
+
*/
|
|
528
|
+
function getMapper() {
|
|
529
|
+
return globalThis.__IOCClassMap;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
507
532
|
* This method registers @Inject() in particular class.
|
|
508
533
|
* @param prototype class prototype for which we register @Inject()
|
|
509
534
|
* @param propertyName name of property that is inejcted
|
|
@@ -511,10 +536,10 @@ const ROOT_PROTO = Object.getPrototypeOf({});
|
|
|
511
536
|
* @param type type of dependency (standard or optional dependency)
|
|
512
537
|
*/
|
|
513
538
|
function registerInject(prototype, propertyName, dependency, type) {
|
|
514
|
-
let entry =
|
|
539
|
+
let entry = getMapper().get(prototype);
|
|
515
540
|
if (!entry) {
|
|
516
541
|
entry = { deps: [] };
|
|
517
|
-
|
|
542
|
+
getMapper().set(prototype, entry);
|
|
518
543
|
}
|
|
519
544
|
const newDep = {
|
|
520
545
|
propertyName,
|
|
@@ -529,7 +554,7 @@ function registerInject(prototype, propertyName, dependency, type) {
|
|
|
529
554
|
* @returns class map entry or null if cannot be found
|
|
530
555
|
*/
|
|
531
556
|
function getEntryForClass(classType) {
|
|
532
|
-
const entry =
|
|
557
|
+
const entry = getMapper().get(classType.prototype);
|
|
533
558
|
return entry === void 0 ? null : entry;
|
|
534
559
|
}
|
|
535
560
|
/**
|
|
@@ -540,7 +565,7 @@ function getEntryForClass(classType) {
|
|
|
540
565
|
function getDeps(instance) {
|
|
541
566
|
const prototype = Object.getPrototypeOf(instance);
|
|
542
567
|
if (!prototype) return [];
|
|
543
|
-
const entry =
|
|
568
|
+
const entry = getMapper().get(prototype);
|
|
544
569
|
return entry !== void 0 && entry.deps !== void 0 ? entry.deps : [];
|
|
545
570
|
}
|
|
546
571
|
/**
|
|
@@ -560,7 +585,7 @@ function injectDeps(container, instance, method) {
|
|
|
560
585
|
* So we will use "do" loop to iterate full prototype chain.
|
|
561
586
|
*/
|
|
562
587
|
do {
|
|
563
|
-
const entry =
|
|
588
|
+
const entry = getMapper().get(prototype);
|
|
564
589
|
if (entry) for (const iter of entry.deps) {
|
|
565
590
|
const propertyName = iter.propertyName;
|
|
566
591
|
const dependency = iter.dependency;
|