@vercube/di 0.0.1-beta.7 → 0.0.2-beta.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/Types/IOCTypes.d.ts +3 -3
- package/dist/index.cjs +89 -0
- package/dist/index.mjs +89 -1
- package/package.json +1 -1
package/dist/Types/IOCTypes.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare namespace IOC {
|
|
|
13
13
|
enum ServiceFactoryType {
|
|
14
14
|
CLASS = "CLASS",
|
|
15
15
|
CLASS_SINGLETON = "CLASS_SINGLETON",
|
|
16
|
-
INSTANCE = "INSTANCE"
|
|
16
|
+
INSTANCE = "INSTANCE"
|
|
17
17
|
}
|
|
18
18
|
interface Newable<T> {
|
|
19
19
|
new (...args: unknown[]): T;
|
|
@@ -30,7 +30,7 @@ export declare namespace IOC {
|
|
|
30
30
|
}
|
|
31
31
|
enum InjectMethod {
|
|
32
32
|
LAZY = "LAZY",
|
|
33
|
-
STATIC = "STATIC"
|
|
33
|
+
STATIC = "STATIC"
|
|
34
34
|
}
|
|
35
35
|
type Identity = symbol;
|
|
36
36
|
interface ServiceMap {
|
|
@@ -43,6 +43,6 @@ export declare namespace IOC {
|
|
|
43
43
|
type Stack = (ServiceKey | Instance)[];
|
|
44
44
|
enum DependencyType {
|
|
45
45
|
STANDARD = 0,
|
|
46
|
-
OPTIONAL = 1
|
|
46
|
+
OPTIONAL = 1
|
|
47
47
|
}
|
|
48
48
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
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
|
+
*/
|
|
4
8
|
var BaseDecorator = class {
|
|
5
9
|
/** Holds options object that is passed as 2nd argument in createDecorator() factory */
|
|
6
10
|
options;
|
|
@@ -156,6 +160,11 @@ const IOCEngine = {
|
|
|
156
160
|
|
|
157
161
|
//#endregion
|
|
158
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
|
+
*/
|
|
159
168
|
function Inject(key) {
|
|
160
169
|
return (target, propertyName) => {
|
|
161
170
|
IOCEngine.registerInject(target, propertyName, key, IOC.DependencyType.STANDARD);
|
|
@@ -164,6 +173,11 @@ function Inject(key) {
|
|
|
164
173
|
|
|
165
174
|
//#endregion
|
|
166
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
|
+
*/
|
|
167
181
|
function InjectOptional(key) {
|
|
168
182
|
return (target, propertyName) => {
|
|
169
183
|
IOCEngine.registerInject(target, propertyName, key, IOC.DependencyType.OPTIONAL);
|
|
@@ -172,6 +186,10 @@ function InjectOptional(key) {
|
|
|
172
186
|
|
|
173
187
|
//#endregion
|
|
174
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
|
+
*/
|
|
175
193
|
var InitDecorator = class extends BaseDecorator {
|
|
176
194
|
/**
|
|
177
195
|
* Called when decorator is initialized.
|
|
@@ -180,12 +198,22 @@ var InitDecorator = class extends BaseDecorator {
|
|
|
180
198
|
if (typeof this.instance[this.propertyName] === "function") this.instance[this.propertyName]();
|
|
181
199
|
}
|
|
182
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
|
+
*/
|
|
183
206
|
function Init() {
|
|
184
207
|
return createDecorator(InitDecorator, {});
|
|
185
208
|
}
|
|
186
209
|
|
|
187
210
|
//#endregion
|
|
188
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
|
+
*/
|
|
189
217
|
var DestroyDecorator = class extends BaseDecorator {
|
|
190
218
|
/**
|
|
191
219
|
* Called when decorator is destroyed. Executes the decorated method if it exists
|
|
@@ -196,12 +224,31 @@ var DestroyDecorator = class extends BaseDecorator {
|
|
|
196
224
|
if (typeof this.instance[this.propertyName] === "function") this.instance[this.propertyName]();
|
|
197
225
|
}
|
|
198
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
|
+
*/
|
|
199
243
|
function Destroy() {
|
|
200
244
|
return createDecorator(DestroyDecorator, {});
|
|
201
245
|
}
|
|
202
246
|
|
|
203
247
|
//#endregion
|
|
204
248
|
//#region packages/di/src/Domain/ContainerEvents.ts
|
|
249
|
+
/**
|
|
250
|
+
* This class allows for container to listen on various IOC events.
|
|
251
|
+
*/
|
|
205
252
|
var ContainerEvents = class {
|
|
206
253
|
fOnExpanded = [];
|
|
207
254
|
/**
|
|
@@ -222,9 +269,24 @@ var ContainerEvents = class {
|
|
|
222
269
|
|
|
223
270
|
//#endregion
|
|
224
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
|
+
*/
|
|
225
280
|
function Identity(name) {
|
|
226
281
|
return Symbol(name);
|
|
227
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
|
+
*/
|
|
228
290
|
function createDecorator(decoratorClass, params) {
|
|
229
291
|
return function internalDecorator(target, propertyName, descriptor) {
|
|
230
292
|
if (!target.__decorators) target.__decorators = [];
|
|
@@ -252,6 +314,12 @@ function getContainerMetadata(container) {
|
|
|
252
314
|
if (!containerMap.has(container)) containerMap.set(container, { decoratedInstances: new Map() });
|
|
253
315
|
return containerMap.get(container);
|
|
254
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
|
+
*/
|
|
255
323
|
function initializeDecorators(target, container) {
|
|
256
324
|
const prototype = Object.getPrototypeOf(target);
|
|
257
325
|
if (prototype.__decorators) for (const entry of prototype.__decorators) {
|
|
@@ -271,15 +339,32 @@ function initializeDecorators(target, container) {
|
|
|
271
339
|
decoratedInstances.set(target, instanceList);
|
|
272
340
|
}
|
|
273
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
|
+
*/
|
|
274
349
|
function destroyDecorators(target, container) {
|
|
275
350
|
const { decoratedInstances } = getContainerMetadata(container);
|
|
276
351
|
const instanceList = decoratedInstances.get(target);
|
|
277
352
|
if (instanceList) for (const instance of instanceList) instance.destroyed();
|
|
278
353
|
decoratedInstances.delete(target);
|
|
279
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
|
+
*/
|
|
280
360
|
function initializeContainer(container) {
|
|
281
361
|
container.flushQueue();
|
|
282
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
|
+
*/
|
|
283
368
|
function destroyContainer(container) {
|
|
284
369
|
container.getAllServices().forEach((service) => destroyDecorators(service, container));
|
|
285
370
|
containerMap.delete(container);
|
|
@@ -287,6 +372,10 @@ function destroyContainer(container) {
|
|
|
287
372
|
|
|
288
373
|
//#endregion
|
|
289
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
|
+
*/
|
|
290
379
|
var Container = class Container {
|
|
291
380
|
fLocked = false;
|
|
292
381
|
fDefaultParams = { createLocked: false };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
1
|
//#region packages/di/src/Common/BaseDecorators.ts
|
|
2
|
+
/**
|
|
3
|
+
* This is base class for all property decorators used in application. Decorator class must extend this one.
|
|
4
|
+
* This class instance is created using IOC container so you can @Inject() things here.
|
|
5
|
+
*/
|
|
3
6
|
var BaseDecorator = class {
|
|
4
7
|
/** Holds options object that is passed as 2nd argument in createDecorator() factory */
|
|
5
8
|
options;
|
|
@@ -155,6 +158,11 @@ const IOCEngine = {
|
|
|
155
158
|
|
|
156
159
|
//#endregion
|
|
157
160
|
//#region packages/di/src/Decorators/Inject.ts
|
|
161
|
+
/**
|
|
162
|
+
* Injects a dependency with particular key to service.
|
|
163
|
+
* @param key key to inject
|
|
164
|
+
* @returns decorator
|
|
165
|
+
*/
|
|
158
166
|
function Inject(key) {
|
|
159
167
|
return (target, propertyName) => {
|
|
160
168
|
IOCEngine.registerInject(target, propertyName, key, IOC.DependencyType.STANDARD);
|
|
@@ -163,6 +171,11 @@ function Inject(key) {
|
|
|
163
171
|
|
|
164
172
|
//#endregion
|
|
165
173
|
//#region packages/di/src/Decorators/InjectOptional.ts
|
|
174
|
+
/**
|
|
175
|
+
* Injects a dependency with particular key to service.
|
|
176
|
+
* @param key key to inject
|
|
177
|
+
* @returns decorator
|
|
178
|
+
*/
|
|
166
179
|
function InjectOptional(key) {
|
|
167
180
|
return (target, propertyName) => {
|
|
168
181
|
IOCEngine.registerInject(target, propertyName, key, IOC.DependencyType.OPTIONAL);
|
|
@@ -171,6 +184,10 @@ function InjectOptional(key) {
|
|
|
171
184
|
|
|
172
185
|
//#endregion
|
|
173
186
|
//#region packages/di/src/Decorators/Init.ts
|
|
187
|
+
/**
|
|
188
|
+
* This decorator fires the decorated function as soon as decorators are injected. You
|
|
189
|
+
* can use this for initialization logic in runtime-loaded container deps.
|
|
190
|
+
*/
|
|
174
191
|
var InitDecorator = class extends BaseDecorator {
|
|
175
192
|
/**
|
|
176
193
|
* Called when decorator is initialized.
|
|
@@ -179,12 +196,22 @@ var InitDecorator = class extends BaseDecorator {
|
|
|
179
196
|
if (typeof this.instance[this.propertyName] === "function") this.instance[this.propertyName]();
|
|
180
197
|
}
|
|
181
198
|
};
|
|
199
|
+
/**
|
|
200
|
+
* Decorator that automatically executes the decorated method when dependencies are injected.
|
|
201
|
+
* Use this decorator to run initialization logic for runtime-loaded container dependencies.
|
|
202
|
+
* @returns {Function} Decorator function that creates an InitDecorator instance
|
|
203
|
+
*/
|
|
182
204
|
function Init() {
|
|
183
205
|
return createDecorator(InitDecorator, {});
|
|
184
206
|
}
|
|
185
207
|
|
|
186
208
|
//#endregion
|
|
187
209
|
//#region packages/di/src/Decorators/Destroy.ts
|
|
210
|
+
/**
|
|
211
|
+
* Decorator that handles cleanup tasks when a service or component is destroyed.
|
|
212
|
+
* This decorator class extends BaseDecorator and executes the decorated method
|
|
213
|
+
* during the destruction phase.
|
|
214
|
+
*/
|
|
188
215
|
var DestroyDecorator = class extends BaseDecorator {
|
|
189
216
|
/**
|
|
190
217
|
* Called when decorator is destroyed. Executes the decorated method if it exists
|
|
@@ -195,12 +222,31 @@ var DestroyDecorator = class extends BaseDecorator {
|
|
|
195
222
|
if (typeof this.instance[this.propertyName] === "function") this.instance[this.propertyName]();
|
|
196
223
|
}
|
|
197
224
|
};
|
|
225
|
+
/**
|
|
226
|
+
* Decorator that automatically executes the decorated method when the service or component
|
|
227
|
+
* is destroyed. Use this decorator to run cleanup logic like unregistering event listeners
|
|
228
|
+
* or clearing timers.
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* class MyService {
|
|
233
|
+
* @Destroy()
|
|
234
|
+
* cleanup() {
|
|
235
|
+
* // Cleanup logic here
|
|
236
|
+
* }
|
|
237
|
+
* }
|
|
238
|
+
* ```
|
|
239
|
+
* @returns {Function} Decorator function that creates a DestroyDecorator instance
|
|
240
|
+
*/
|
|
198
241
|
function Destroy() {
|
|
199
242
|
return createDecorator(DestroyDecorator, {});
|
|
200
243
|
}
|
|
201
244
|
|
|
202
245
|
//#endregion
|
|
203
246
|
//#region packages/di/src/Domain/ContainerEvents.ts
|
|
247
|
+
/**
|
|
248
|
+
* This class allows for container to listen on various IOC events.
|
|
249
|
+
*/
|
|
204
250
|
var ContainerEvents = class {
|
|
205
251
|
fOnExpanded = [];
|
|
206
252
|
/**
|
|
@@ -221,9 +267,24 @@ var ContainerEvents = class {
|
|
|
221
267
|
|
|
222
268
|
//#endregion
|
|
223
269
|
//#region packages/di/src/Utils/Utils.ts
|
|
270
|
+
/**
|
|
271
|
+
* This function generates new service key for particular service. Providing
|
|
272
|
+
* name is mandatory because, unlike classes, those unique symbols do not infer
|
|
273
|
+
* their names from code.
|
|
274
|
+
*
|
|
275
|
+
* @param name name of the service
|
|
276
|
+
* @returns unique service identity
|
|
277
|
+
*/
|
|
224
278
|
function Identity(name) {
|
|
225
279
|
return Symbol(name);
|
|
226
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* This function creates ES6 decorator based on class that was passed in argument.
|
|
283
|
+
*
|
|
284
|
+
* @param decoratorClass class of decorator that will be used
|
|
285
|
+
* @param params custom options object that will be availalbe in "options" property of decorator class
|
|
286
|
+
* @return ES6 decorator function
|
|
287
|
+
*/
|
|
227
288
|
function createDecorator(decoratorClass, params) {
|
|
228
289
|
return function internalDecorator(target, propertyName, descriptor) {
|
|
229
290
|
if (!target.__decorators) target.__decorators = [];
|
|
@@ -251,6 +312,12 @@ function getContainerMetadata(container) {
|
|
|
251
312
|
if (!containerMap.has(container)) containerMap.set(container, { decoratedInstances: new Map() });
|
|
252
313
|
return containerMap.get(container);
|
|
253
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* This function initializes all registered decorators on particular instance. It must be called in order
|
|
317
|
+
* for decorator code work in particular class.
|
|
318
|
+
* @param target class instance to sue
|
|
319
|
+
* @param container IOC container for context
|
|
320
|
+
*/
|
|
254
321
|
function initializeDecorators(target, container) {
|
|
255
322
|
const prototype = Object.getPrototypeOf(target);
|
|
256
323
|
if (prototype.__decorators) for (const entry of prototype.__decorators) {
|
|
@@ -270,15 +337,32 @@ function initializeDecorators(target, container) {
|
|
|
270
337
|
decoratedInstances.set(target, instanceList);
|
|
271
338
|
}
|
|
272
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* This function releases all decorators applied to target instance by calling their .destroyed()
|
|
342
|
+
* method. Its used to provide a way for cleanup for decorators. @see BaseDecorator.destroy()
|
|
343
|
+
*
|
|
344
|
+
* @param target instance of class that should have decorators cleaned up
|
|
345
|
+
* @param container ioc container
|
|
346
|
+
*/
|
|
273
347
|
function destroyDecorators(target, container) {
|
|
274
348
|
const { decoratedInstances } = getContainerMetadata(container);
|
|
275
349
|
const instanceList = decoratedInstances.get(target);
|
|
276
350
|
if (instanceList) for (const instance of instanceList) instance.destroyed();
|
|
277
351
|
decoratedInstances.delete(target);
|
|
278
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* This function is responsible for preparing IOC container to work with all decorators. It simply
|
|
355
|
+
* initializes all decorators on all services registered.
|
|
356
|
+
* @param container IOC container
|
|
357
|
+
*/
|
|
279
358
|
function initializeContainer(container) {
|
|
280
359
|
container.flushQueue();
|
|
281
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* This function is responsible for preparing IOC container to work with all decorators. It simply
|
|
363
|
+
* initializes all decorators on all services registered.
|
|
364
|
+
* @param container IOC container
|
|
365
|
+
*/
|
|
282
366
|
function destroyContainer(container) {
|
|
283
367
|
container.getAllServices().forEach((service) => destroyDecorators(service, container));
|
|
284
368
|
containerMap.delete(container);
|
|
@@ -286,6 +370,10 @@ function destroyContainer(container) {
|
|
|
286
370
|
|
|
287
371
|
//#endregion
|
|
288
372
|
//#region packages/di/src/Domain/Container.ts
|
|
373
|
+
/**
|
|
374
|
+
* This is new implementation of IOC Container. It mimics Inversify.js container a little bit but its
|
|
375
|
+
* simpler and (probably) more performant on larger scales.
|
|
376
|
+
*/
|
|
289
377
|
var Container = class Container {
|
|
290
378
|
fLocked = false;
|
|
291
379
|
fDefaultParams = { createLocked: false };
|