@vercube/di 0.0.2 → 0.0.4
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 +426 -0
- package/dist/index.mjs +17 -17
- package/package.json +3 -10
- package/dist/Common/BaseDecorators.d.ts +0 -31
- package/dist/Decorators/Destroy.d.ts +0 -17
- package/dist/Decorators/Init.d.ts +0 -6
- package/dist/Decorators/Inject.d.ts +0 -7
- package/dist/Decorators/InjectOptional.d.ts +0 -7
- package/dist/Domain/Container.d.ts +0 -154
- package/dist/Domain/ContainerEvents.d.ts +0 -18
- package/dist/Domain/Engine.d.ts +0 -55
- package/dist/Types/IOCTypes.d.ts +0 -48
- package/dist/Utils/Utils.d.ts +0 -79
- package/dist/index.cjs +0 -711
- package/dist/index.d.ts +0 -8
package/dist/index.cjs
DELETED
|
@@ -1,711 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
//#region packages/di/src/Common/BaseDecorators.ts
|
|
4
|
-
/**
|
|
5
|
-
* This is base class for all property decorators used in application. Decorator class must extend this one.
|
|
6
|
-
* This class instance is created using IOC container so you can @Inject() things here.
|
|
7
|
-
*/
|
|
8
|
-
var BaseDecorator = class {
|
|
9
|
-
/** Holds options object that is passed as 2nd argument in createDecorator() factory */
|
|
10
|
-
options;
|
|
11
|
-
/** Holds class instance that is decorated */
|
|
12
|
-
instance;
|
|
13
|
-
/** Holds class prototype that is decorated */
|
|
14
|
-
prototype;
|
|
15
|
-
/** Holds property name that was decorated */
|
|
16
|
-
propertyName;
|
|
17
|
-
/** Holds property descriptor that was decorated */
|
|
18
|
-
descriptor;
|
|
19
|
-
/** Holds property index if decorators if for Method Property */
|
|
20
|
-
propertyIndex;
|
|
21
|
-
/**
|
|
22
|
-
* This method is called when decorator is created and ready to be used.
|
|
23
|
-
*/
|
|
24
|
-
created() {}
|
|
25
|
-
/**
|
|
26
|
-
* This method is called when decorator is destroyed for cleanup tasks (like unregistering listeners, clearing timers).
|
|
27
|
-
* For standard services it is called at the end of SSR requests and for Vue components it is called when component is
|
|
28
|
-
* destroyed.
|
|
29
|
-
*/
|
|
30
|
-
destroyed() {}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region packages/di/src/Types/IOCTypes.ts
|
|
35
|
-
let IOC;
|
|
36
|
-
(function(_IOC) {
|
|
37
|
-
let ServiceFactoryType = /* @__PURE__ */ function(ServiceFactoryType$1) {
|
|
38
|
-
ServiceFactoryType$1["CLASS"] = "CLASS";
|
|
39
|
-
ServiceFactoryType$1["CLASS_SINGLETON"] = "CLASS_SINGLETON";
|
|
40
|
-
ServiceFactoryType$1["INSTANCE"] = "INSTANCE";
|
|
41
|
-
return ServiceFactoryType$1;
|
|
42
|
-
}({});
|
|
43
|
-
_IOC.ServiceFactoryType = ServiceFactoryType;
|
|
44
|
-
let InjectMethod = /* @__PURE__ */ function(InjectMethod$1) {
|
|
45
|
-
InjectMethod$1["LAZY"] = "LAZY";
|
|
46
|
-
InjectMethod$1["STATIC"] = "STATIC";
|
|
47
|
-
return InjectMethod$1;
|
|
48
|
-
}({});
|
|
49
|
-
_IOC.InjectMethod = InjectMethod;
|
|
50
|
-
let DependencyType = /* @__PURE__ */ function(DependencyType$1) {
|
|
51
|
-
DependencyType$1[DependencyType$1["STANDARD"] = 0] = "STANDARD";
|
|
52
|
-
DependencyType$1[DependencyType$1["OPTIONAL"] = 1] = "OPTIONAL";
|
|
53
|
-
return DependencyType$1;
|
|
54
|
-
}({});
|
|
55
|
-
_IOC.DependencyType = DependencyType;
|
|
56
|
-
})(IOC || (IOC = {}));
|
|
57
|
-
|
|
58
|
-
//#endregion
|
|
59
|
-
//#region packages/di/src/Domain/Engine.ts
|
|
60
|
-
/**
|
|
61
|
-
* This map holds metadata for ALL classes in system along with their dependencies. Original idea was
|
|
62
|
-
* to store those informations in object prototype, but accessing this map is blazing fast with Map
|
|
63
|
-
* container (<1ms).
|
|
64
|
-
*/
|
|
65
|
-
const classMap = new Map();
|
|
66
|
-
const ROOT_PROTO = Object.getPrototypeOf({});
|
|
67
|
-
/**
|
|
68
|
-
* This method registers @Inject() in particular class.
|
|
69
|
-
* @param prototype class prototype for which we register @Inject()
|
|
70
|
-
* @param propertyName name of property that is inejcted
|
|
71
|
-
* @param dependency what we should inject there
|
|
72
|
-
* @param type type of dependency (standard or optional dependency)
|
|
73
|
-
*/
|
|
74
|
-
function registerInject(prototype, propertyName, dependency, type) {
|
|
75
|
-
let entry = classMap.get(prototype);
|
|
76
|
-
if (!entry) {
|
|
77
|
-
const newEntry = { deps: [] };
|
|
78
|
-
entry = newEntry;
|
|
79
|
-
classMap.set(prototype, entry);
|
|
80
|
-
}
|
|
81
|
-
const newDep = {
|
|
82
|
-
propertyName,
|
|
83
|
-
dependency,
|
|
84
|
-
type
|
|
85
|
-
};
|
|
86
|
-
entry.deps.push(newDep);
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Returns classmap entry for particular class. It holds information about @Injects for this particular class.
|
|
90
|
-
* @param classType type of class to check
|
|
91
|
-
* @returns class map entry or null if cannot be found
|
|
92
|
-
*/
|
|
93
|
-
function getEntryForClass(classType) {
|
|
94
|
-
const entry = classMap.get(classType.prototype);
|
|
95
|
-
return entry === void 0 ? null : entry;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Returns array of dependencies for particular class instance.
|
|
99
|
-
* @param instance class instance
|
|
100
|
-
* @returns array of @Inject dependencies defined for this class
|
|
101
|
-
*/
|
|
102
|
-
function getDeps(instance) {
|
|
103
|
-
const prototype = Object.getPrototypeOf(instance);
|
|
104
|
-
if (!prototype) return [];
|
|
105
|
-
const entry = classMap.get(prototype);
|
|
106
|
-
return entry !== void 0 && entry.deps !== void 0 ? entry.deps : [];
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* This method injects stuff into class, using container passed as first argument. We need container as
|
|
110
|
-
* inject execution is always context based.
|
|
111
|
-
* @param container container instance that is providing dependencies
|
|
112
|
-
* @param instance class instance to inject
|
|
113
|
-
* @param method inject method, "lazy" queries dep during property access while "static" injects during class creation
|
|
114
|
-
*/
|
|
115
|
-
function injectDeps(container, instance, method) {
|
|
116
|
-
let prototype = Object.getPrototypeOf(instance);
|
|
117
|
-
if (!prototype) return;
|
|
118
|
-
/**
|
|
119
|
-
* Here we will traverse through prototype chain until we hit dead end (ROOT_PROTO). This is because we
|
|
120
|
-
* must process inject for current class and also for every base class.
|
|
121
|
-
*
|
|
122
|
-
* So we will use "do" loop to iterate full prototype chain.
|
|
123
|
-
*/
|
|
124
|
-
do {
|
|
125
|
-
const entry = classMap.get(prototype);
|
|
126
|
-
if (entry) for (const iter of entry.deps) {
|
|
127
|
-
const propertyName = iter.propertyName;
|
|
128
|
-
const dependency = iter.dependency;
|
|
129
|
-
const type = iter.type;
|
|
130
|
-
if (Object.prototype.hasOwnProperty.call(instance, propertyName) && instance[propertyName] !== void 0) continue;
|
|
131
|
-
if (type === IOC.DependencyType.OPTIONAL) {
|
|
132
|
-
Object.defineProperty(instance, propertyName, { get: function() {
|
|
133
|
-
return container.getOptional(dependency);
|
|
134
|
-
} });
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
switch (method) {
|
|
138
|
-
case IOC.InjectMethod.LAZY: {
|
|
139
|
-
Object.defineProperty(instance, propertyName, { get: function() {
|
|
140
|
-
return container.get(dependency);
|
|
141
|
-
} });
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
case IOC.InjectMethod.STATIC: {
|
|
145
|
-
instance[propertyName] = container.get(dependency);
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
default: throw new Error(`IOCEngine.injectDeps() - invalid inject method ${method}`);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
prototype = Object.getPrototypeOf(prototype);
|
|
152
|
-
} while (prototype && prototype !== ROOT_PROTO);
|
|
153
|
-
}
|
|
154
|
-
const IOCEngine = {
|
|
155
|
-
registerInject,
|
|
156
|
-
getEntryForClass,
|
|
157
|
-
injectDeps,
|
|
158
|
-
getDeps
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
//#endregion
|
|
162
|
-
//#region packages/di/src/Decorators/Inject.ts
|
|
163
|
-
/**
|
|
164
|
-
* Injects a dependency with particular key to service.
|
|
165
|
-
* @param key key to inject
|
|
166
|
-
* @returns decorator
|
|
167
|
-
*/
|
|
168
|
-
function Inject(key) {
|
|
169
|
-
return (target, propertyName) => {
|
|
170
|
-
IOCEngine.registerInject(target, propertyName, key, IOC.DependencyType.STANDARD);
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
//#endregion
|
|
175
|
-
//#region packages/di/src/Decorators/InjectOptional.ts
|
|
176
|
-
/**
|
|
177
|
-
* Injects a dependency with particular key to service.
|
|
178
|
-
* @param key key to inject
|
|
179
|
-
* @returns decorator
|
|
180
|
-
*/
|
|
181
|
-
function InjectOptional(key) {
|
|
182
|
-
return (target, propertyName) => {
|
|
183
|
-
IOCEngine.registerInject(target, propertyName, key, IOC.DependencyType.OPTIONAL);
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
//#endregion
|
|
188
|
-
//#region packages/di/src/Decorators/Init.ts
|
|
189
|
-
/**
|
|
190
|
-
* This decorator fires the decorated function as soon as decorators are injected. You
|
|
191
|
-
* can use this for initialization logic in runtime-loaded container deps.
|
|
192
|
-
*/
|
|
193
|
-
var InitDecorator = class extends BaseDecorator {
|
|
194
|
-
/**
|
|
195
|
-
* Called when decorator is initialized.
|
|
196
|
-
*/
|
|
197
|
-
created() {
|
|
198
|
-
if (typeof this.instance[this.propertyName] === "function") this.instance[this.propertyName]();
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
/**
|
|
202
|
-
* Decorator that automatically executes the decorated method when dependencies are injected.
|
|
203
|
-
* Use this decorator to run initialization logic for runtime-loaded container dependencies.
|
|
204
|
-
* @returns {Function} Decorator function that creates an InitDecorator instance
|
|
205
|
-
*/
|
|
206
|
-
function Init() {
|
|
207
|
-
return createDecorator(InitDecorator, {});
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
//#endregion
|
|
211
|
-
//#region packages/di/src/Decorators/Destroy.ts
|
|
212
|
-
/**
|
|
213
|
-
* Decorator that handles cleanup tasks when a service or component is destroyed.
|
|
214
|
-
* This decorator class extends BaseDecorator and executes the decorated method
|
|
215
|
-
* during the destruction phase.
|
|
216
|
-
*/
|
|
217
|
-
var DestroyDecorator = class extends BaseDecorator {
|
|
218
|
-
/**
|
|
219
|
-
* Called when decorator is destroyed. Executes the decorated method if it exists
|
|
220
|
-
* as a function on the instance. Used for cleanup tasks like unregistering
|
|
221
|
-
* listeners or clearing timers.
|
|
222
|
-
*/
|
|
223
|
-
destroyed() {
|
|
224
|
-
if (typeof this.instance[this.propertyName] === "function") this.instance[this.propertyName]();
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
/**
|
|
228
|
-
* Decorator that automatically executes the decorated method when the service or component
|
|
229
|
-
* is destroyed. Use this decorator to run cleanup logic like unregistering event listeners
|
|
230
|
-
* or clearing timers.
|
|
231
|
-
*
|
|
232
|
-
* @example
|
|
233
|
-
* ```typescript
|
|
234
|
-
* class MyService {
|
|
235
|
-
* @Destroy()
|
|
236
|
-
* cleanup() {
|
|
237
|
-
* // Cleanup logic here
|
|
238
|
-
* }
|
|
239
|
-
* }
|
|
240
|
-
* ```
|
|
241
|
-
* @returns {Function} Decorator function that creates a DestroyDecorator instance
|
|
242
|
-
*/
|
|
243
|
-
function Destroy() {
|
|
244
|
-
return createDecorator(DestroyDecorator, {});
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
//#endregion
|
|
248
|
-
//#region packages/di/src/Domain/ContainerEvents.ts
|
|
249
|
-
/**
|
|
250
|
-
* This class allows for container to listen on various IOC events.
|
|
251
|
-
*/
|
|
252
|
-
var ContainerEvents = class {
|
|
253
|
-
fOnExpanded = [];
|
|
254
|
-
/**
|
|
255
|
-
* Registers to container "onExpanded" event.
|
|
256
|
-
* @param handler event handler
|
|
257
|
-
*/
|
|
258
|
-
onExpanded(handler) {
|
|
259
|
-
this.fOnExpanded.push(handler);
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Calls on expanded event.
|
|
263
|
-
* @param serviceKeys key of service we have installed
|
|
264
|
-
*/
|
|
265
|
-
callOnExpanded(serviceKeys) {
|
|
266
|
-
for (const handler of this.fOnExpanded) handler(serviceKeys);
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
//#endregion
|
|
271
|
-
//#region packages/di/src/Utils/Utils.ts
|
|
272
|
-
/**
|
|
273
|
-
* This function generates new service key for particular service. Providing
|
|
274
|
-
* name is mandatory because, unlike classes, those unique symbols do not infer
|
|
275
|
-
* their names from code.
|
|
276
|
-
*
|
|
277
|
-
* @param name name of the service
|
|
278
|
-
* @returns unique service identity
|
|
279
|
-
*/
|
|
280
|
-
function Identity(name) {
|
|
281
|
-
return Symbol(name);
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* This function creates ES6 decorator based on class that was passed in argument.
|
|
285
|
-
*
|
|
286
|
-
* @param decoratorClass class of decorator that will be used
|
|
287
|
-
* @param params custom options object that will be availalbe in "options" property of decorator class
|
|
288
|
-
* @return ES6 decorator function
|
|
289
|
-
*/
|
|
290
|
-
function createDecorator(decoratorClass, params) {
|
|
291
|
-
return function internalDecorator(target, propertyName, descriptor) {
|
|
292
|
-
if (!target.__decorators) target.__decorators = [];
|
|
293
|
-
target.__decorators.push({
|
|
294
|
-
classType: decoratorClass,
|
|
295
|
-
params,
|
|
296
|
-
target,
|
|
297
|
-
propertyName,
|
|
298
|
-
descriptor
|
|
299
|
-
});
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* This map holds map of Container:DecoratorMetadata values. For every container created,
|
|
304
|
-
* we must hold array of decorated instances, we realize it by holding map where key is
|
|
305
|
-
* container (for easier removal later) and value is IContainerDecoratorMetadataObject.
|
|
306
|
-
*/
|
|
307
|
-
const containerMap = new Map();
|
|
308
|
-
/**
|
|
309
|
-
* Helper function to query data from container
|
|
310
|
-
* @param container container to get metadata fro
|
|
311
|
-
* @return metadata object
|
|
312
|
-
*/
|
|
313
|
-
function getContainerMetadata(container) {
|
|
314
|
-
if (!containerMap.has(container)) containerMap.set(container, { decoratedInstances: new Map() });
|
|
315
|
-
return containerMap.get(container);
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* This function initializes all registered decorators on particular instance. It must be called in order
|
|
319
|
-
* for decorator code work in particular class.
|
|
320
|
-
* @param target class instance to sue
|
|
321
|
-
* @param container IOC container for context
|
|
322
|
-
*/
|
|
323
|
-
function initializeDecorators(target, container) {
|
|
324
|
-
const prototype = Object.getPrototypeOf(target);
|
|
325
|
-
if (prototype.__decorators) for (const entry of prototype.__decorators) {
|
|
326
|
-
const instance = container.resolve(entry.classType);
|
|
327
|
-
if (instance) {
|
|
328
|
-
instance.options = entry.params;
|
|
329
|
-
instance.instance = target;
|
|
330
|
-
instance.prototype = prototype;
|
|
331
|
-
instance.propertyName = entry.propertyName;
|
|
332
|
-
instance.descriptor = entry.descriptor;
|
|
333
|
-
instance.propertyIndex = typeof entry.descriptor === "number" ? entry.descriptor : -1;
|
|
334
|
-
instance.created();
|
|
335
|
-
}
|
|
336
|
-
const { decoratedInstances } = getContainerMetadata(container);
|
|
337
|
-
const instanceList = decoratedInstances.get(target) ?? [];
|
|
338
|
-
instanceList.push(instance);
|
|
339
|
-
decoratedInstances.set(target, instanceList);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* This function releases all decorators applied to target instance by calling their .destroyed()
|
|
344
|
-
* method. Its used to provide a way for cleanup for decorators. @see BaseDecorator.destroy()
|
|
345
|
-
*
|
|
346
|
-
* @param target instance of class that should have decorators cleaned up
|
|
347
|
-
* @param container ioc container
|
|
348
|
-
*/
|
|
349
|
-
function destroyDecorators(target, container) {
|
|
350
|
-
const { decoratedInstances } = getContainerMetadata(container);
|
|
351
|
-
const instanceList = decoratedInstances.get(target);
|
|
352
|
-
if (instanceList) for (const instance of instanceList) instance.destroyed();
|
|
353
|
-
decoratedInstances.delete(target);
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* This function is responsible for preparing IOC container to work with all decorators. It simply
|
|
357
|
-
* initializes all decorators on all services registered.
|
|
358
|
-
* @param container IOC container
|
|
359
|
-
*/
|
|
360
|
-
function initializeContainer(container) {
|
|
361
|
-
container.flushQueue();
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* This function is responsible for preparing IOC container to work with all decorators. It simply
|
|
365
|
-
* initializes all decorators on all services registered.
|
|
366
|
-
* @param container IOC container
|
|
367
|
-
*/
|
|
368
|
-
function destroyContainer(container) {
|
|
369
|
-
container.getAllServices().forEach((service) => destroyDecorators(service, container));
|
|
370
|
-
containerMap.delete(container);
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
//#endregion
|
|
374
|
-
//#region packages/di/src/Domain/Container.ts
|
|
375
|
-
/**
|
|
376
|
-
* This is new implementation of IOC Container. It mimics Inversify.js container a little bit but its
|
|
377
|
-
* simpler and (probably) more performant on larger scales.
|
|
378
|
-
*/
|
|
379
|
-
var Container = class Container {
|
|
380
|
-
fLocked = false;
|
|
381
|
-
fDefaultParams = { createLocked: false };
|
|
382
|
-
fServices = new Map();
|
|
383
|
-
fNewQueue = new Map();
|
|
384
|
-
fSingletonInstances = new Map();
|
|
385
|
-
fInjectMethod = IOC.InjectMethod.STATIC;
|
|
386
|
-
fContainerEvents = new ContainerEvents();
|
|
387
|
-
/**
|
|
388
|
-
* Constructor for container.
|
|
389
|
-
* @param params initial params for container
|
|
390
|
-
*/
|
|
391
|
-
constructor(params) {
|
|
392
|
-
this.fLocked = params?.createLocked ?? false;
|
|
393
|
-
this.fDefaultParams = Object.assign(this.fDefaultParams, params);
|
|
394
|
-
this.fInjectMethod = params?.injectMethod ?? IOC.InjectMethod.STATIC;
|
|
395
|
-
this.bindInstance(Container, this);
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* Returns array of all service keys. This basically returns keys from all .bindXXX calls.
|
|
399
|
-
* @returns {Array} array of service keys
|
|
400
|
-
*/
|
|
401
|
-
get servicesKeys() {
|
|
402
|
-
return [...this.fServices.keys()];
|
|
403
|
-
}
|
|
404
|
-
/**
|
|
405
|
-
* Returns events handler.
|
|
406
|
-
*/
|
|
407
|
-
get events() {
|
|
408
|
-
return this.fContainerEvents;
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Binds particular key to container in singleton scope. Multiple queries/injects of this
|
|
412
|
-
* service will always return the same instance.
|
|
413
|
-
*
|
|
414
|
-
* @param key key of service, preferably class or abstract class
|
|
415
|
-
* @param value implementation
|
|
416
|
-
*/
|
|
417
|
-
bind(key, value) {
|
|
418
|
-
const newDef = {
|
|
419
|
-
serviceKey: key,
|
|
420
|
-
serviceValue: value ?? key,
|
|
421
|
-
type: IOC.ServiceFactoryType.CLASS_SINGLETON
|
|
422
|
-
};
|
|
423
|
-
if (typeof key === "symbol" && !value) throw new Error("Container - provide implementation for binds with symbols.");
|
|
424
|
-
const existingServiceDef = this.fServices.get(key);
|
|
425
|
-
if (existingServiceDef) {
|
|
426
|
-
this.internalDispose(existingServiceDef);
|
|
427
|
-
this.fServices.delete(key);
|
|
428
|
-
}
|
|
429
|
-
this.fServices.set(key, newDef);
|
|
430
|
-
this.fNewQueue.set(key, newDef);
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Binds particular key to container in transient scope. Every query/@Inject of this service
|
|
434
|
-
* will have totally brand-new instance of class.
|
|
435
|
-
* @param key key of service, preferably class or abstract class
|
|
436
|
-
* @param value implementation
|
|
437
|
-
*/
|
|
438
|
-
bindTransient(key, value) {
|
|
439
|
-
const newDef = {
|
|
440
|
-
serviceKey: key,
|
|
441
|
-
serviceValue: value ?? key,
|
|
442
|
-
type: IOC.ServiceFactoryType.CLASS
|
|
443
|
-
};
|
|
444
|
-
const existingServiceDef = this.fServices.get(key);
|
|
445
|
-
if (existingServiceDef) this.internalDispose(existingServiceDef);
|
|
446
|
-
this.fServices.set(key, newDef);
|
|
447
|
-
this.fNewQueue.set(key, newDef);
|
|
448
|
-
}
|
|
449
|
-
/**
|
|
450
|
-
* Binds particular key class to an existing class instance. If you use this method,
|
|
451
|
-
* class wont be instantiated automatically. The common use case is to
|
|
452
|
-
* share single class instance between two or more containers.
|
|
453
|
-
*
|
|
454
|
-
* @param key key of service, preferably class or abstract class
|
|
455
|
-
* @param value instance of class to be used as resolution
|
|
456
|
-
*/
|
|
457
|
-
bindInstance(key, value) {
|
|
458
|
-
const newDef = {
|
|
459
|
-
serviceKey: key,
|
|
460
|
-
serviceValue: value,
|
|
461
|
-
type: IOC.ServiceFactoryType.INSTANCE
|
|
462
|
-
};
|
|
463
|
-
const existingServiceDef = this.fServices.get(key);
|
|
464
|
-
if (existingServiceDef) this.internalDispose(existingServiceDef);
|
|
465
|
-
this.fServices.set(key, newDef);
|
|
466
|
-
this.fNewQueue.set(key, newDef);
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* Binds mocked instance to a particular service ID. Its designed to be used in unit tests, where you can quickly
|
|
470
|
-
* replace real IOC implementation with a partial stub. Please note, you are responsible to provide enough data
|
|
471
|
-
* for test to pass, TypeScript wont check it.
|
|
472
|
-
*
|
|
473
|
-
* Example:
|
|
474
|
-
*
|
|
475
|
-
* container.bind(HttpServer, {
|
|
476
|
-
* listen: jest.fn(),
|
|
477
|
-
* });
|
|
478
|
-
*
|
|
479
|
-
* @param key service to be replaced
|
|
480
|
-
* @param mockInstance mock instance
|
|
481
|
-
*/
|
|
482
|
-
bindMock(key, mockInstance) {
|
|
483
|
-
const newDef = {
|
|
484
|
-
serviceKey: key,
|
|
485
|
-
serviceValue: mockInstance,
|
|
486
|
-
type: IOC.ServiceFactoryType.INSTANCE
|
|
487
|
-
};
|
|
488
|
-
const existingServiceDef = this.fServices.get(key);
|
|
489
|
-
if (existingServiceDef) this.internalDispose(existingServiceDef);
|
|
490
|
-
this.fServices.set(key, newDef);
|
|
491
|
-
this.fNewQueue.set(key, newDef);
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* Returns implementation for a particular key class. This is the same as @Inject,
|
|
495
|
-
* but triggered programitically.
|
|
496
|
-
* @param key key used in .bind() function to bind key class to implementation class
|
|
497
|
-
* @returns service for identifier
|
|
498
|
-
*/
|
|
499
|
-
get(key) {
|
|
500
|
-
return this.internalGet(key);
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* Returns implementation for a particular key class. This is the same as @Inject,
|
|
504
|
-
* but triggered programitically.
|
|
505
|
-
* @param key key used in .bind() function to bind key class to implementation class
|
|
506
|
-
* @returns service for identifier
|
|
507
|
-
*/
|
|
508
|
-
getOptional(key) {
|
|
509
|
-
return this.internalGetOptional(key);
|
|
510
|
-
}
|
|
511
|
-
/**
|
|
512
|
-
* Uses the container provider to register multiple things in IOC container at once.
|
|
513
|
-
* @param provider provider that will register new services into IOC container
|
|
514
|
-
*/
|
|
515
|
-
use(provider) {
|
|
516
|
-
provider(this);
|
|
517
|
-
}
|
|
518
|
-
/**
|
|
519
|
-
* Expands container during runtime, adding new services to it.
|
|
520
|
-
* @param providers functor that is used to expand the container
|
|
521
|
-
* @param flush whether container should be flushed now or not
|
|
522
|
-
*/
|
|
523
|
-
expand(providers, flush = true) {
|
|
524
|
-
const preLockState = this.fLocked;
|
|
525
|
-
const allProviders = Array.isArray(providers) ? providers : [providers];
|
|
526
|
-
try {
|
|
527
|
-
this.fLocked = false;
|
|
528
|
-
for (const provider of allProviders) this.use(provider);
|
|
529
|
-
const newKeys = [...this.fNewQueue.keys()].filter((k) => !this.fSingletonInstances.has(k));
|
|
530
|
-
this.fContainerEvents.callOnExpanded(newKeys);
|
|
531
|
-
if (flush) this.flushQueue();
|
|
532
|
-
} finally {
|
|
533
|
-
this.fLocked = preLockState;
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
/**
|
|
537
|
-
* Creates instance of particular class using, respecting all @Injects inside created class.
|
|
538
|
-
* @param classType type of class to instantiate
|
|
539
|
-
* @param method (optional) inject method
|
|
540
|
-
* @returns new class instance with dependencies
|
|
541
|
-
*/
|
|
542
|
-
resolve(classType, method = IOC.InjectMethod.LAZY) {
|
|
543
|
-
const newInstance = new classType();
|
|
544
|
-
this.internalProcessInjects(newInstance, method);
|
|
545
|
-
return newInstance;
|
|
546
|
-
}
|
|
547
|
-
/**
|
|
548
|
-
* Returns all IOC services registered in container.
|
|
549
|
-
* @returns array with all registered services
|
|
550
|
-
*/
|
|
551
|
-
getAllServices() {
|
|
552
|
-
return this.servicesKeys.map((k) => this.get(k));
|
|
553
|
-
}
|
|
554
|
-
/**
|
|
555
|
-
* Unlocks the container, allowing things to be retrieved and used.
|
|
556
|
-
*/
|
|
557
|
-
unlock() {
|
|
558
|
-
this.fLocked = false;
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* Locks the container, disabling to add new services here.
|
|
562
|
-
*/
|
|
563
|
-
lock() {
|
|
564
|
-
this.fLocked = true;
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Flushes new services queue, registering them into container.
|
|
568
|
-
*/
|
|
569
|
-
flushQueue() {
|
|
570
|
-
if (this.fNewQueue.size === 0) return;
|
|
571
|
-
const values = [...this.fNewQueue.values()];
|
|
572
|
-
for (const def of values) {
|
|
573
|
-
if (def.type !== IOC.ServiceFactoryType.CLASS_SINGLETON) continue;
|
|
574
|
-
const instance = this.internalResolve(def);
|
|
575
|
-
initializeDecorators(instance, this);
|
|
576
|
-
}
|
|
577
|
-
this.fNewQueue.clear();
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* Internally retrieve dependency from container.
|
|
581
|
-
* @param key key to get
|
|
582
|
-
* @param parent parent (for debugging purposes)
|
|
583
|
-
* @returns queried instance
|
|
584
|
-
*/
|
|
585
|
-
internalGet(key, parent) {
|
|
586
|
-
const serviceDef = this.fServices.get(key);
|
|
587
|
-
if (!serviceDef) throw new Error(`Unresolved dependency for [${this.getKeyDescription(key)}]`);
|
|
588
|
-
return this.internalResolve(serviceDef);
|
|
589
|
-
}
|
|
590
|
-
/**
|
|
591
|
-
* Internally retrieve dependency from container.
|
|
592
|
-
* @param key key to get
|
|
593
|
-
* @param parent parent
|
|
594
|
-
* @returns queried instance
|
|
595
|
-
*/
|
|
596
|
-
internalGetOptional(key) {
|
|
597
|
-
const serviceDef = this.fServices.get(key);
|
|
598
|
-
if (!serviceDef) return null;
|
|
599
|
-
return this.internalResolve(serviceDef);
|
|
600
|
-
}
|
|
601
|
-
/**
|
|
602
|
-
* Internally resolves service def, turning it into class instance with deps injected.
|
|
603
|
-
* @param serviceDef service def to resolve
|
|
604
|
-
* @returns class instance
|
|
605
|
-
*/
|
|
606
|
-
internalResolve(serviceDef) {
|
|
607
|
-
switch (serviceDef.type) {
|
|
608
|
-
case IOC.ServiceFactoryType.INSTANCE: return serviceDef.serviceValue;
|
|
609
|
-
case IOC.ServiceFactoryType.CLASS_SINGLETON: {
|
|
610
|
-
if (!this.fSingletonInstances.has(serviceDef.serviceKey)) {
|
|
611
|
-
const constructor = serviceDef.serviceValue;
|
|
612
|
-
const instance = new constructor();
|
|
613
|
-
this.fSingletonInstances.set(serviceDef.serviceKey, instance);
|
|
614
|
-
this.internalProcessInjects(instance, this.fInjectMethod);
|
|
615
|
-
return instance;
|
|
616
|
-
}
|
|
617
|
-
return this.fSingletonInstances.get(serviceDef.serviceKey);
|
|
618
|
-
}
|
|
619
|
-
case IOC.ServiceFactoryType.CLASS: {
|
|
620
|
-
const constructor = serviceDef.serviceValue;
|
|
621
|
-
const instance = new constructor();
|
|
622
|
-
this.internalProcessInjects(instance, this.fInjectMethod);
|
|
623
|
-
return instance;
|
|
624
|
-
}
|
|
625
|
-
default: throw new Error(`Container - invalid factory type: ${serviceDef.type}`);
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
/**
|
|
629
|
-
* Internally inject deps for particular class..
|
|
630
|
-
* @param instance instance to inject
|
|
631
|
-
* @param method method for injecting dependencies, either lazy or static
|
|
632
|
-
*/
|
|
633
|
-
internalProcessInjects(instance, method) {
|
|
634
|
-
if (method === IOC.InjectMethod.LAZY) {
|
|
635
|
-
IOCEngine.injectDeps(this, instance, IOC.InjectMethod.LAZY);
|
|
636
|
-
return;
|
|
637
|
-
}
|
|
638
|
-
const processQueue = [];
|
|
639
|
-
const elementSet = new Set();
|
|
640
|
-
const toProcessElements = [];
|
|
641
|
-
processQueue.push(instance);
|
|
642
|
-
toProcessElements.push(instance);
|
|
643
|
-
while (processQueue.length > 0) {
|
|
644
|
-
const element = processQueue.pop();
|
|
645
|
-
const deps = IOCEngine.getDeps(element);
|
|
646
|
-
for (const inj of deps) if (!elementSet.has(inj.dependency)) {
|
|
647
|
-
const isOptional = inj.type === IOC.DependencyType.OPTIONAL;
|
|
648
|
-
const childInstance = isOptional ? this.internalGetOptional(inj.dependency) : this.internalGet(inj.dependency, instance);
|
|
649
|
-
elementSet.add(inj.dependency);
|
|
650
|
-
if (!isOptional) {
|
|
651
|
-
processQueue.push(childInstance);
|
|
652
|
-
toProcessElements.push(childInstance);
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
for (const el of toProcessElements) IOCEngine.injectDeps(this, el, IOC.InjectMethod.STATIC);
|
|
657
|
-
}
|
|
658
|
-
/**
|
|
659
|
-
* Disposes a module, clearing everything allocated to it.
|
|
660
|
-
* @param def def that should be disposed
|
|
661
|
-
*/
|
|
662
|
-
internalDispose(def) {
|
|
663
|
-
switch (def.type) {
|
|
664
|
-
case IOC.ServiceFactoryType.INSTANCE: {
|
|
665
|
-
destroyDecorators(def.serviceValue, this);
|
|
666
|
-
break;
|
|
667
|
-
}
|
|
668
|
-
case IOC.ServiceFactoryType.CLASS_SINGLETON: {
|
|
669
|
-
const existingInstance = this.fSingletonInstances.get(def.serviceKey);
|
|
670
|
-
if (existingInstance) {
|
|
671
|
-
destroyDecorators(existingInstance, this);
|
|
672
|
-
this.fSingletonInstances.delete(def.serviceKey);
|
|
673
|
-
}
|
|
674
|
-
break;
|
|
675
|
-
}
|
|
676
|
-
case IOC.ServiceFactoryType.CLASS: break;
|
|
677
|
-
default: throw new Error(`Container::internalDispose() - invalid def type: ${def.type}`);
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
/**
|
|
681
|
-
* Describes particular key for better error messaging.
|
|
682
|
-
* @param key service key
|
|
683
|
-
* @returns string representation of service key
|
|
684
|
-
*/
|
|
685
|
-
getKeyDescription(key) {
|
|
686
|
-
if (typeof key === "symbol") return key.description;
|
|
687
|
-
else if (typeof key === "function") return key.name;
|
|
688
|
-
else if (typeof key === "object") return key.constructor?.name ?? "Unknown object";
|
|
689
|
-
return "Unknown";
|
|
690
|
-
}
|
|
691
|
-
};
|
|
692
|
-
|
|
693
|
-
//#endregion
|
|
694
|
-
exports.BaseDecorator = BaseDecorator
|
|
695
|
-
exports.Container = Container
|
|
696
|
-
exports.Destroy = Destroy
|
|
697
|
-
Object.defineProperty(exports, 'IOC', {
|
|
698
|
-
enumerable: true,
|
|
699
|
-
get: function () {
|
|
700
|
-
return IOC;
|
|
701
|
-
}
|
|
702
|
-
});
|
|
703
|
-
exports.Identity = Identity
|
|
704
|
-
exports.Init = Init
|
|
705
|
-
exports.Inject = Inject
|
|
706
|
-
exports.InjectOptional = InjectOptional
|
|
707
|
-
exports.createDecorator = createDecorator
|
|
708
|
-
exports.destroyContainer = destroyContainer
|
|
709
|
-
exports.destroyDecorators = destroyDecorators
|
|
710
|
-
exports.initializeContainer = initializeContainer
|
|
711
|
-
exports.initializeDecorators = initializeDecorators
|