@vercube/di 0.0.25 → 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 +32 -6
- 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
|
*
|
|
@@ -501,8 +514,21 @@ let IOC;
|
|
|
501
514
|
* container (<1ms).
|
|
502
515
|
*/
|
|
503
516
|
const classMap = /* @__PURE__ */ new Map();
|
|
517
|
+
globalThis.__IOCClassMap = globalThis.__IOCClassMap ?? classMap;
|
|
504
518
|
const ROOT_PROTO = Object.getPrototypeOf({});
|
|
505
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
|
+
/**
|
|
506
532
|
* This method registers @Inject() in particular class.
|
|
507
533
|
* @param prototype class prototype for which we register @Inject()
|
|
508
534
|
* @param propertyName name of property that is inejcted
|
|
@@ -510,10 +536,10 @@ const ROOT_PROTO = Object.getPrototypeOf({});
|
|
|
510
536
|
* @param type type of dependency (standard or optional dependency)
|
|
511
537
|
*/
|
|
512
538
|
function registerInject(prototype, propertyName, dependency, type) {
|
|
513
|
-
let entry =
|
|
539
|
+
let entry = getMapper().get(prototype);
|
|
514
540
|
if (!entry) {
|
|
515
541
|
entry = { deps: [] };
|
|
516
|
-
|
|
542
|
+
getMapper().set(prototype, entry);
|
|
517
543
|
}
|
|
518
544
|
const newDep = {
|
|
519
545
|
propertyName,
|
|
@@ -528,7 +554,7 @@ function registerInject(prototype, propertyName, dependency, type) {
|
|
|
528
554
|
* @returns class map entry or null if cannot be found
|
|
529
555
|
*/
|
|
530
556
|
function getEntryForClass(classType) {
|
|
531
|
-
const entry =
|
|
557
|
+
const entry = getMapper().get(classType.prototype);
|
|
532
558
|
return entry === void 0 ? null : entry;
|
|
533
559
|
}
|
|
534
560
|
/**
|
|
@@ -539,7 +565,7 @@ function getEntryForClass(classType) {
|
|
|
539
565
|
function getDeps(instance) {
|
|
540
566
|
const prototype = Object.getPrototypeOf(instance);
|
|
541
567
|
if (!prototype) return [];
|
|
542
|
-
const entry =
|
|
568
|
+
const entry = getMapper().get(prototype);
|
|
543
569
|
return entry !== void 0 && entry.deps !== void 0 ? entry.deps : [];
|
|
544
570
|
}
|
|
545
571
|
/**
|
|
@@ -559,7 +585,7 @@ function injectDeps(container, instance, method) {
|
|
|
559
585
|
* So we will use "do" loop to iterate full prototype chain.
|
|
560
586
|
*/
|
|
561
587
|
do {
|
|
562
|
-
const entry =
|
|
588
|
+
const entry = getMapper().get(prototype);
|
|
563
589
|
if (entry) for (const iter of entry.deps) {
|
|
564
590
|
const propertyName = iter.propertyName;
|
|
565
591
|
const dependency = iter.dependency;
|