proxydi 0.0.12 → 0.0.13
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/CHANGELOG.md +10 -0
- package/dist/ProxyDiContainer.d.ts +1 -1
- package/dist/index.cjs +69 -48
- package/dist/index.js +69 -48
- package/dist/index.umd.js +69 -48
- package/dist/inject.d.ts +2 -2
- package/dist/middleware/MiddlewareListener.d.ts +3 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [[0.0.13](https://www.npmjs.com/package/proxydi/v/0.0.13)] - 2025-03-04
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- inject, resolve register by class [#15](https://github.com/proxy-di/proxydi/pull/15)
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- remove event requires dependency
|
|
17
|
+
|
|
8
18
|
## [[0.0.12](https://www.npmjs.com/package/proxydi/v/0.0.12)] - 2025-03-04
|
|
9
19
|
|
|
10
20
|
### Added
|
|
@@ -45,7 +45,7 @@ export declare class ProxyDiContainer implements IProxyDiContainer {
|
|
|
45
45
|
* @throws Error if dependency is already registered and rewriting is not allowed or if invalid dependency (not object) is provided and this it now allowed.
|
|
46
46
|
* @returns Dependency instance, registered in container
|
|
47
47
|
*/
|
|
48
|
-
register<T>(DependencyClass: DependencyClass<T>, dependencyId
|
|
48
|
+
register<T>(DependencyClass: DependencyClass<T>, dependencyId?: DependencyId): T & ContainerizedDependency;
|
|
49
49
|
register<T>(dependency: T extends new (...args: any[]) => any ? never : T, dependencyId: DependencyId): T & ContainerizedDependency;
|
|
50
50
|
private createInstance;
|
|
51
51
|
/**
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const injectableClasses = {};
|
|
4
|
+
const constructorInjections = {};
|
|
5
|
+
function injectable(dependencyOrDependencies, autoInjecions) {
|
|
6
|
+
return function (value, context) {
|
|
7
|
+
if ((context === null || context === undefined ? undefined : context.kind) !== 'class') {
|
|
8
|
+
throw new Error('@injectable decorator should decorate classes');
|
|
9
|
+
}
|
|
10
|
+
const name = dependencyOrDependencies
|
|
11
|
+
? Array.isArray(dependencyOrDependencies)
|
|
12
|
+
? context.name
|
|
13
|
+
: dependencyOrDependencies
|
|
14
|
+
: context.name;
|
|
15
|
+
const injectToConstructor = dependencyOrDependencies
|
|
16
|
+
? Array.isArray(dependencyOrDependencies)
|
|
17
|
+
? dependencyOrDependencies
|
|
18
|
+
: autoInjecions
|
|
19
|
+
: autoInjecions;
|
|
20
|
+
if (injectableClasses[name]) {
|
|
21
|
+
throw new Error(`ProxyDi has already regisered dependency ${String(name)} by @injectable`);
|
|
22
|
+
}
|
|
23
|
+
injectableClasses[name] = value;
|
|
24
|
+
if (injectToConstructor) {
|
|
25
|
+
constructorInjections[name] = injectToConstructor;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function findInjectableId(injectable) {
|
|
30
|
+
for (const [id, DependencyClass] of Object.entries(injectableClasses)) {
|
|
31
|
+
if (DependencyClass === injectable) {
|
|
32
|
+
return id;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
throw new Error(`Class is not @injectable: ${injectable.name}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
3
38
|
const INJECTIONS = Symbol('injections');
|
|
4
39
|
/**
|
|
5
40
|
* This symbol constant defines a property name.
|
|
@@ -28,7 +63,11 @@ const IS_INSTANCE_PROXY = Symbol('isInstanceProxy');
|
|
|
28
63
|
const inject = (dependencyId) => {
|
|
29
64
|
return function (_value, context) {
|
|
30
65
|
if ((context === null || context === undefined ? undefined : context.kind) === 'field') {
|
|
31
|
-
const id = dependencyId
|
|
66
|
+
const id = dependencyId
|
|
67
|
+
? typeof dependencyId === 'function'
|
|
68
|
+
? findInjectableId(dependencyId)
|
|
69
|
+
: dependencyId
|
|
70
|
+
: context.name;
|
|
32
71
|
const injection = {
|
|
33
72
|
property: context.name,
|
|
34
73
|
dependencyId: id,
|
|
@@ -47,41 +86,6 @@ const inject = (dependencyId) => {
|
|
|
47
86
|
};
|
|
48
87
|
};
|
|
49
88
|
|
|
50
|
-
const injectableClasses = {};
|
|
51
|
-
const constructorInjections = {};
|
|
52
|
-
function injectable(dependencyOrDependencies, autoInjecions) {
|
|
53
|
-
return function (value, context) {
|
|
54
|
-
if ((context === null || context === undefined ? undefined : context.kind) !== 'class') {
|
|
55
|
-
throw new Error('@injectable decorator should decorate classes');
|
|
56
|
-
}
|
|
57
|
-
const name = dependencyOrDependencies
|
|
58
|
-
? Array.isArray(dependencyOrDependencies)
|
|
59
|
-
? context.name
|
|
60
|
-
: dependencyOrDependencies
|
|
61
|
-
: context.name;
|
|
62
|
-
const injectToConstructor = dependencyOrDependencies
|
|
63
|
-
? Array.isArray(dependencyOrDependencies)
|
|
64
|
-
? dependencyOrDependencies
|
|
65
|
-
: autoInjecions
|
|
66
|
-
: autoInjecions;
|
|
67
|
-
if (injectableClasses[name]) {
|
|
68
|
-
throw new Error(`ProxyDi has already regisered dependency ${String(name)} by @injectable`);
|
|
69
|
-
}
|
|
70
|
-
injectableClasses[name] = value;
|
|
71
|
-
if (injectToConstructor) {
|
|
72
|
-
constructorInjections[name] = injectToConstructor;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
function findInjectableId(injectable) {
|
|
77
|
-
for (const [id, DependencyClass] of Object.entries(injectableClasses)) {
|
|
78
|
-
if (DependencyClass === injectable) {
|
|
79
|
-
return id;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
throw new Error(`Class is not @injectable: ${injectable.name}`);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
89
|
const DEFAULT_SETTINGS = {
|
|
86
90
|
allowRegisterAnything: false,
|
|
87
91
|
allowRewriteDependencies: false,
|
|
@@ -237,10 +241,10 @@ class MiddlewareListener {
|
|
|
237
241
|
this.listeners.register.forEach((listener) => listener(container, dependencyId, dependency));
|
|
238
242
|
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRegister(container, dependencyId, dependency);
|
|
239
243
|
}
|
|
240
|
-
onRemove(container, dependencyId) {
|
|
244
|
+
onRemove(container, dependencyId, dependency) {
|
|
241
245
|
var _a;
|
|
242
|
-
this.listeners.remove.forEach((listener) => listener(container, dependencyId));
|
|
243
|
-
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRemove(container, dependencyId);
|
|
246
|
+
this.listeners.remove.forEach((listener) => listener(container, dependencyId, dependency));
|
|
247
|
+
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRemove(container, dependencyId, dependency);
|
|
244
248
|
}
|
|
245
249
|
off(event, listener) {
|
|
246
250
|
const index = this.listeners[event].indexOf(listener);
|
|
@@ -283,17 +287,28 @@ class ProxyDiContainer {
|
|
|
283
287
|
}
|
|
284
288
|
this.settings = Object.assign(Object.assign({}, DEFAULT_SETTINGS), settings);
|
|
285
289
|
}
|
|
286
|
-
register(dependency,
|
|
290
|
+
register(dependency, dependecyId) {
|
|
287
291
|
var _a;
|
|
288
|
-
|
|
292
|
+
let id = dependecyId;
|
|
293
|
+
if (!id) {
|
|
294
|
+
if (typeof dependency === 'function') {
|
|
295
|
+
try {
|
|
296
|
+
id = findInjectableId(dependency);
|
|
297
|
+
}
|
|
298
|
+
catch (_b) {
|
|
299
|
+
id = dependency.name;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (this.dependencies[id]) {
|
|
289
304
|
if (!this.settings.allowRewriteDependencies) {
|
|
290
|
-
throw new Error(`ProxyDi already has dependency for ${String(
|
|
305
|
+
throw new Error(`ProxyDi already has dependency for ${String(id)}`);
|
|
291
306
|
}
|
|
292
307
|
}
|
|
293
308
|
let instance;
|
|
294
309
|
const isClass = typeof dependency === 'function';
|
|
295
310
|
if (isClass) {
|
|
296
|
-
instance = this.createInstance(dependency,
|
|
311
|
+
instance = this.createInstance(dependency, id);
|
|
297
312
|
}
|
|
298
313
|
else {
|
|
299
314
|
instance = dependency;
|
|
@@ -304,15 +319,15 @@ class ProxyDiContainer {
|
|
|
304
319
|
}
|
|
305
320
|
if (isObject) {
|
|
306
321
|
instance[PROXYDI_CONTAINER] = this;
|
|
307
|
-
instance[DEPENDENCY_ID] =
|
|
322
|
+
instance[DEPENDENCY_ID] = id;
|
|
308
323
|
}
|
|
309
324
|
this.injectDependenciesTo(instance);
|
|
310
|
-
this.dependencies[
|
|
325
|
+
this.dependencies[id] = instance;
|
|
311
326
|
const constructorName = (_a = instance.constructor) === null || _a === undefined ? undefined : _a.name;
|
|
312
327
|
if (constructorName && middlewaresClasses[constructorName]) {
|
|
313
328
|
this.middlewareListener.add(instance);
|
|
314
329
|
}
|
|
315
|
-
this.middlewareListener.onRegister(this,
|
|
330
|
+
this.middlewareListener.onRegister(this, id, instance);
|
|
316
331
|
return instance;
|
|
317
332
|
}
|
|
318
333
|
createInstance(Dependency, dependencyId) {
|
|
@@ -337,7 +352,13 @@ class ProxyDiContainer {
|
|
|
337
352
|
}
|
|
338
353
|
resolve(dependency) {
|
|
339
354
|
if (typeof dependency === 'function') {
|
|
340
|
-
|
|
355
|
+
let id;
|
|
356
|
+
try {
|
|
357
|
+
id = findInjectableId(dependency);
|
|
358
|
+
}
|
|
359
|
+
catch (_a) {
|
|
360
|
+
id = dependency.name;
|
|
361
|
+
}
|
|
341
362
|
return this.resolve(id);
|
|
342
363
|
}
|
|
343
364
|
if (!this.isKnown(dependency)) {
|
|
@@ -430,7 +451,7 @@ class ProxyDiContainer {
|
|
|
430
451
|
});
|
|
431
452
|
delete dependency[DEPENDENCY_ID];
|
|
432
453
|
delete this.dependencies[id];
|
|
433
|
-
this.middlewareListener.onRemove(this, id);
|
|
454
|
+
this.middlewareListener.onRemove(this, id, dependency);
|
|
434
455
|
}
|
|
435
456
|
}
|
|
436
457
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
+
const injectableClasses = {};
|
|
2
|
+
const constructorInjections = {};
|
|
3
|
+
function injectable(dependencyOrDependencies, autoInjecions) {
|
|
4
|
+
return function (value, context) {
|
|
5
|
+
if ((context === null || context === undefined ? undefined : context.kind) !== 'class') {
|
|
6
|
+
throw new Error('@injectable decorator should decorate classes');
|
|
7
|
+
}
|
|
8
|
+
const name = dependencyOrDependencies
|
|
9
|
+
? Array.isArray(dependencyOrDependencies)
|
|
10
|
+
? context.name
|
|
11
|
+
: dependencyOrDependencies
|
|
12
|
+
: context.name;
|
|
13
|
+
const injectToConstructor = dependencyOrDependencies
|
|
14
|
+
? Array.isArray(dependencyOrDependencies)
|
|
15
|
+
? dependencyOrDependencies
|
|
16
|
+
: autoInjecions
|
|
17
|
+
: autoInjecions;
|
|
18
|
+
if (injectableClasses[name]) {
|
|
19
|
+
throw new Error(`ProxyDi has already regisered dependency ${String(name)} by @injectable`);
|
|
20
|
+
}
|
|
21
|
+
injectableClasses[name] = value;
|
|
22
|
+
if (injectToConstructor) {
|
|
23
|
+
constructorInjections[name] = injectToConstructor;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function findInjectableId(injectable) {
|
|
28
|
+
for (const [id, DependencyClass] of Object.entries(injectableClasses)) {
|
|
29
|
+
if (DependencyClass === injectable) {
|
|
30
|
+
return id;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
throw new Error(`Class is not @injectable: ${injectable.name}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
1
36
|
const INJECTIONS = Symbol('injections');
|
|
2
37
|
/**
|
|
3
38
|
* This symbol constant defines a property name.
|
|
@@ -26,7 +61,11 @@ const IS_INSTANCE_PROXY = Symbol('isInstanceProxy');
|
|
|
26
61
|
const inject = (dependencyId) => {
|
|
27
62
|
return function (_value, context) {
|
|
28
63
|
if ((context === null || context === undefined ? undefined : context.kind) === 'field') {
|
|
29
|
-
const id = dependencyId
|
|
64
|
+
const id = dependencyId
|
|
65
|
+
? typeof dependencyId === 'function'
|
|
66
|
+
? findInjectableId(dependencyId)
|
|
67
|
+
: dependencyId
|
|
68
|
+
: context.name;
|
|
30
69
|
const injection = {
|
|
31
70
|
property: context.name,
|
|
32
71
|
dependencyId: id,
|
|
@@ -45,41 +84,6 @@ const inject = (dependencyId) => {
|
|
|
45
84
|
};
|
|
46
85
|
};
|
|
47
86
|
|
|
48
|
-
const injectableClasses = {};
|
|
49
|
-
const constructorInjections = {};
|
|
50
|
-
function injectable(dependencyOrDependencies, autoInjecions) {
|
|
51
|
-
return function (value, context) {
|
|
52
|
-
if ((context === null || context === undefined ? undefined : context.kind) !== 'class') {
|
|
53
|
-
throw new Error('@injectable decorator should decorate classes');
|
|
54
|
-
}
|
|
55
|
-
const name = dependencyOrDependencies
|
|
56
|
-
? Array.isArray(dependencyOrDependencies)
|
|
57
|
-
? context.name
|
|
58
|
-
: dependencyOrDependencies
|
|
59
|
-
: context.name;
|
|
60
|
-
const injectToConstructor = dependencyOrDependencies
|
|
61
|
-
? Array.isArray(dependencyOrDependencies)
|
|
62
|
-
? dependencyOrDependencies
|
|
63
|
-
: autoInjecions
|
|
64
|
-
: autoInjecions;
|
|
65
|
-
if (injectableClasses[name]) {
|
|
66
|
-
throw new Error(`ProxyDi has already regisered dependency ${String(name)} by @injectable`);
|
|
67
|
-
}
|
|
68
|
-
injectableClasses[name] = value;
|
|
69
|
-
if (injectToConstructor) {
|
|
70
|
-
constructorInjections[name] = injectToConstructor;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
function findInjectableId(injectable) {
|
|
75
|
-
for (const [id, DependencyClass] of Object.entries(injectableClasses)) {
|
|
76
|
-
if (DependencyClass === injectable) {
|
|
77
|
-
return id;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
throw new Error(`Class is not @injectable: ${injectable.name}`);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
87
|
const DEFAULT_SETTINGS = {
|
|
84
88
|
allowRegisterAnything: false,
|
|
85
89
|
allowRewriteDependencies: false,
|
|
@@ -235,10 +239,10 @@ class MiddlewareListener {
|
|
|
235
239
|
this.listeners.register.forEach((listener) => listener(container, dependencyId, dependency));
|
|
236
240
|
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRegister(container, dependencyId, dependency);
|
|
237
241
|
}
|
|
238
|
-
onRemove(container, dependencyId) {
|
|
242
|
+
onRemove(container, dependencyId, dependency) {
|
|
239
243
|
var _a;
|
|
240
|
-
this.listeners.remove.forEach((listener) => listener(container, dependencyId));
|
|
241
|
-
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRemove(container, dependencyId);
|
|
244
|
+
this.listeners.remove.forEach((listener) => listener(container, dependencyId, dependency));
|
|
245
|
+
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRemove(container, dependencyId, dependency);
|
|
242
246
|
}
|
|
243
247
|
off(event, listener) {
|
|
244
248
|
const index = this.listeners[event].indexOf(listener);
|
|
@@ -281,17 +285,28 @@ class ProxyDiContainer {
|
|
|
281
285
|
}
|
|
282
286
|
this.settings = Object.assign(Object.assign({}, DEFAULT_SETTINGS), settings);
|
|
283
287
|
}
|
|
284
|
-
register(dependency,
|
|
288
|
+
register(dependency, dependecyId) {
|
|
285
289
|
var _a;
|
|
286
|
-
|
|
290
|
+
let id = dependecyId;
|
|
291
|
+
if (!id) {
|
|
292
|
+
if (typeof dependency === 'function') {
|
|
293
|
+
try {
|
|
294
|
+
id = findInjectableId(dependency);
|
|
295
|
+
}
|
|
296
|
+
catch (_b) {
|
|
297
|
+
id = dependency.name;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (this.dependencies[id]) {
|
|
287
302
|
if (!this.settings.allowRewriteDependencies) {
|
|
288
|
-
throw new Error(`ProxyDi already has dependency for ${String(
|
|
303
|
+
throw new Error(`ProxyDi already has dependency for ${String(id)}`);
|
|
289
304
|
}
|
|
290
305
|
}
|
|
291
306
|
let instance;
|
|
292
307
|
const isClass = typeof dependency === 'function';
|
|
293
308
|
if (isClass) {
|
|
294
|
-
instance = this.createInstance(dependency,
|
|
309
|
+
instance = this.createInstance(dependency, id);
|
|
295
310
|
}
|
|
296
311
|
else {
|
|
297
312
|
instance = dependency;
|
|
@@ -302,15 +317,15 @@ class ProxyDiContainer {
|
|
|
302
317
|
}
|
|
303
318
|
if (isObject) {
|
|
304
319
|
instance[PROXYDI_CONTAINER] = this;
|
|
305
|
-
instance[DEPENDENCY_ID] =
|
|
320
|
+
instance[DEPENDENCY_ID] = id;
|
|
306
321
|
}
|
|
307
322
|
this.injectDependenciesTo(instance);
|
|
308
|
-
this.dependencies[
|
|
323
|
+
this.dependencies[id] = instance;
|
|
309
324
|
const constructorName = (_a = instance.constructor) === null || _a === undefined ? undefined : _a.name;
|
|
310
325
|
if (constructorName && middlewaresClasses[constructorName]) {
|
|
311
326
|
this.middlewareListener.add(instance);
|
|
312
327
|
}
|
|
313
|
-
this.middlewareListener.onRegister(this,
|
|
328
|
+
this.middlewareListener.onRegister(this, id, instance);
|
|
314
329
|
return instance;
|
|
315
330
|
}
|
|
316
331
|
createInstance(Dependency, dependencyId) {
|
|
@@ -335,7 +350,13 @@ class ProxyDiContainer {
|
|
|
335
350
|
}
|
|
336
351
|
resolve(dependency) {
|
|
337
352
|
if (typeof dependency === 'function') {
|
|
338
|
-
|
|
353
|
+
let id;
|
|
354
|
+
try {
|
|
355
|
+
id = findInjectableId(dependency);
|
|
356
|
+
}
|
|
357
|
+
catch (_a) {
|
|
358
|
+
id = dependency.name;
|
|
359
|
+
}
|
|
339
360
|
return this.resolve(id);
|
|
340
361
|
}
|
|
341
362
|
if (!this.isKnown(dependency)) {
|
|
@@ -428,7 +449,7 @@ class ProxyDiContainer {
|
|
|
428
449
|
});
|
|
429
450
|
delete dependency[DEPENDENCY_ID];
|
|
430
451
|
delete this.dependencies[id];
|
|
431
|
-
this.middlewareListener.onRemove(this, id);
|
|
452
|
+
this.middlewareListener.onRemove(this, id, dependency);
|
|
432
453
|
}
|
|
433
454
|
}
|
|
434
455
|
/**
|
package/dist/index.umd.js
CHANGED
|
@@ -4,6 +4,41 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.YourLibraryName = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
+
const injectableClasses = {};
|
|
8
|
+
const constructorInjections = {};
|
|
9
|
+
function injectable(dependencyOrDependencies, autoInjecions) {
|
|
10
|
+
return function (value, context) {
|
|
11
|
+
if ((context === null || context === undefined ? undefined : context.kind) !== 'class') {
|
|
12
|
+
throw new Error('@injectable decorator should decorate classes');
|
|
13
|
+
}
|
|
14
|
+
const name = dependencyOrDependencies
|
|
15
|
+
? Array.isArray(dependencyOrDependencies)
|
|
16
|
+
? context.name
|
|
17
|
+
: dependencyOrDependencies
|
|
18
|
+
: context.name;
|
|
19
|
+
const injectToConstructor = dependencyOrDependencies
|
|
20
|
+
? Array.isArray(dependencyOrDependencies)
|
|
21
|
+
? dependencyOrDependencies
|
|
22
|
+
: autoInjecions
|
|
23
|
+
: autoInjecions;
|
|
24
|
+
if (injectableClasses[name]) {
|
|
25
|
+
throw new Error(`ProxyDi has already regisered dependency ${String(name)} by @injectable`);
|
|
26
|
+
}
|
|
27
|
+
injectableClasses[name] = value;
|
|
28
|
+
if (injectToConstructor) {
|
|
29
|
+
constructorInjections[name] = injectToConstructor;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function findInjectableId(injectable) {
|
|
34
|
+
for (const [id, DependencyClass] of Object.entries(injectableClasses)) {
|
|
35
|
+
if (DependencyClass === injectable) {
|
|
36
|
+
return id;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new Error(`Class is not @injectable: ${injectable.name}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
7
42
|
const INJECTIONS = Symbol('injections');
|
|
8
43
|
/**
|
|
9
44
|
* This symbol constant defines a property name.
|
|
@@ -32,7 +67,11 @@
|
|
|
32
67
|
const inject = (dependencyId) => {
|
|
33
68
|
return function (_value, context) {
|
|
34
69
|
if ((context === null || context === undefined ? undefined : context.kind) === 'field') {
|
|
35
|
-
const id = dependencyId
|
|
70
|
+
const id = dependencyId
|
|
71
|
+
? typeof dependencyId === 'function'
|
|
72
|
+
? findInjectableId(dependencyId)
|
|
73
|
+
: dependencyId
|
|
74
|
+
: context.name;
|
|
36
75
|
const injection = {
|
|
37
76
|
property: context.name,
|
|
38
77
|
dependencyId: id,
|
|
@@ -51,41 +90,6 @@
|
|
|
51
90
|
};
|
|
52
91
|
};
|
|
53
92
|
|
|
54
|
-
const injectableClasses = {};
|
|
55
|
-
const constructorInjections = {};
|
|
56
|
-
function injectable(dependencyOrDependencies, autoInjecions) {
|
|
57
|
-
return function (value, context) {
|
|
58
|
-
if ((context === null || context === undefined ? undefined : context.kind) !== 'class') {
|
|
59
|
-
throw new Error('@injectable decorator should decorate classes');
|
|
60
|
-
}
|
|
61
|
-
const name = dependencyOrDependencies
|
|
62
|
-
? Array.isArray(dependencyOrDependencies)
|
|
63
|
-
? context.name
|
|
64
|
-
: dependencyOrDependencies
|
|
65
|
-
: context.name;
|
|
66
|
-
const injectToConstructor = dependencyOrDependencies
|
|
67
|
-
? Array.isArray(dependencyOrDependencies)
|
|
68
|
-
? dependencyOrDependencies
|
|
69
|
-
: autoInjecions
|
|
70
|
-
: autoInjecions;
|
|
71
|
-
if (injectableClasses[name]) {
|
|
72
|
-
throw new Error(`ProxyDi has already regisered dependency ${String(name)} by @injectable`);
|
|
73
|
-
}
|
|
74
|
-
injectableClasses[name] = value;
|
|
75
|
-
if (injectToConstructor) {
|
|
76
|
-
constructorInjections[name] = injectToConstructor;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
function findInjectableId(injectable) {
|
|
81
|
-
for (const [id, DependencyClass] of Object.entries(injectableClasses)) {
|
|
82
|
-
if (DependencyClass === injectable) {
|
|
83
|
-
return id;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
throw new Error(`Class is not @injectable: ${injectable.name}`);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
93
|
const DEFAULT_SETTINGS = {
|
|
90
94
|
allowRegisterAnything: false,
|
|
91
95
|
allowRewriteDependencies: false,
|
|
@@ -241,10 +245,10 @@
|
|
|
241
245
|
this.listeners.register.forEach((listener) => listener(container, dependencyId, dependency));
|
|
242
246
|
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRegister(container, dependencyId, dependency);
|
|
243
247
|
}
|
|
244
|
-
onRemove(container, dependencyId) {
|
|
248
|
+
onRemove(container, dependencyId, dependency) {
|
|
245
249
|
var _a;
|
|
246
|
-
this.listeners.remove.forEach((listener) => listener(container, dependencyId));
|
|
247
|
-
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRemove(container, dependencyId);
|
|
250
|
+
this.listeners.remove.forEach((listener) => listener(container, dependencyId, dependency));
|
|
251
|
+
(_a = this.parent) === null || _a === undefined ? undefined : _a.onRemove(container, dependencyId, dependency);
|
|
248
252
|
}
|
|
249
253
|
off(event, listener) {
|
|
250
254
|
const index = this.listeners[event].indexOf(listener);
|
|
@@ -287,17 +291,28 @@
|
|
|
287
291
|
}
|
|
288
292
|
this.settings = Object.assign(Object.assign({}, DEFAULT_SETTINGS), settings);
|
|
289
293
|
}
|
|
290
|
-
register(dependency,
|
|
294
|
+
register(dependency, dependecyId) {
|
|
291
295
|
var _a;
|
|
292
|
-
|
|
296
|
+
let id = dependecyId;
|
|
297
|
+
if (!id) {
|
|
298
|
+
if (typeof dependency === 'function') {
|
|
299
|
+
try {
|
|
300
|
+
id = findInjectableId(dependency);
|
|
301
|
+
}
|
|
302
|
+
catch (_b) {
|
|
303
|
+
id = dependency.name;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (this.dependencies[id]) {
|
|
293
308
|
if (!this.settings.allowRewriteDependencies) {
|
|
294
|
-
throw new Error(`ProxyDi already has dependency for ${String(
|
|
309
|
+
throw new Error(`ProxyDi already has dependency for ${String(id)}`);
|
|
295
310
|
}
|
|
296
311
|
}
|
|
297
312
|
let instance;
|
|
298
313
|
const isClass = typeof dependency === 'function';
|
|
299
314
|
if (isClass) {
|
|
300
|
-
instance = this.createInstance(dependency,
|
|
315
|
+
instance = this.createInstance(dependency, id);
|
|
301
316
|
}
|
|
302
317
|
else {
|
|
303
318
|
instance = dependency;
|
|
@@ -308,15 +323,15 @@
|
|
|
308
323
|
}
|
|
309
324
|
if (isObject) {
|
|
310
325
|
instance[PROXYDI_CONTAINER] = this;
|
|
311
|
-
instance[DEPENDENCY_ID] =
|
|
326
|
+
instance[DEPENDENCY_ID] = id;
|
|
312
327
|
}
|
|
313
328
|
this.injectDependenciesTo(instance);
|
|
314
|
-
this.dependencies[
|
|
329
|
+
this.dependencies[id] = instance;
|
|
315
330
|
const constructorName = (_a = instance.constructor) === null || _a === undefined ? undefined : _a.name;
|
|
316
331
|
if (constructorName && middlewaresClasses[constructorName]) {
|
|
317
332
|
this.middlewareListener.add(instance);
|
|
318
333
|
}
|
|
319
|
-
this.middlewareListener.onRegister(this,
|
|
334
|
+
this.middlewareListener.onRegister(this, id, instance);
|
|
320
335
|
return instance;
|
|
321
336
|
}
|
|
322
337
|
createInstance(Dependency, dependencyId) {
|
|
@@ -341,7 +356,13 @@
|
|
|
341
356
|
}
|
|
342
357
|
resolve(dependency) {
|
|
343
358
|
if (typeof dependency === 'function') {
|
|
344
|
-
|
|
359
|
+
let id;
|
|
360
|
+
try {
|
|
361
|
+
id = findInjectableId(dependency);
|
|
362
|
+
}
|
|
363
|
+
catch (_a) {
|
|
364
|
+
id = dependency.name;
|
|
365
|
+
}
|
|
345
366
|
return this.resolve(id);
|
|
346
367
|
}
|
|
347
368
|
if (!this.isKnown(dependency)) {
|
|
@@ -434,7 +455,7 @@
|
|
|
434
455
|
});
|
|
435
456
|
delete dependency[DEPENDENCY_ID];
|
|
436
457
|
delete this.dependencies[id];
|
|
437
|
-
this.middlewareListener.onRemove(this, id);
|
|
458
|
+
this.middlewareListener.onRemove(this, id, dependency);
|
|
438
459
|
}
|
|
439
460
|
}
|
|
440
461
|
/**
|
package/dist/inject.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DependencyId } from './types';
|
|
1
|
+
import { DependencyId, DependencyClass } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Registers an injection for dependency injection.
|
|
4
4
|
*
|
|
@@ -7,4 +7,4 @@ import { DependencyId } from './types';
|
|
|
7
7
|
*
|
|
8
8
|
* The decorated field will receive its dependency from the same container as the injection owner.
|
|
9
9
|
*/
|
|
10
|
-
export declare const inject: (dependencyId?: DependencyId) => (_value: unknown, context: ClassFieldDecoratorContext) => void;
|
|
10
|
+
export declare const inject: (dependencyId?: DependencyId | DependencyClass<any>) => (_value: unknown, context: ClassFieldDecoratorContext) => void;
|
|
@@ -2,7 +2,7 @@ import { ProxyDiContainer } from '../ProxyDiContainer';
|
|
|
2
2
|
import { DependencyId } from '../types';
|
|
3
3
|
export interface MiddlewareListenerEvent {
|
|
4
4
|
register: (container: ProxyDiContainer, dependencyId: DependencyId, dependency: any) => void;
|
|
5
|
-
remove: (container: ProxyDiContainer, dependencyId: DependencyId) => void;
|
|
5
|
+
remove: (container: ProxyDiContainer, dependencyId: DependencyId, dependency: any) => void;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
8
|
* Describe the middleware that able to listen to the registering of a dependency in containers hierarchy
|
|
@@ -14,7 +14,7 @@ export interface MiddlewareRegisteringListener {
|
|
|
14
14
|
* Describe the middleware that able to listen to the removing of a dependency in containers hierarchy
|
|
15
15
|
*/
|
|
16
16
|
export interface MiddlewareRemovingListener {
|
|
17
|
-
onRemove(container: ProxyDiContainer, dependencyId: DependencyId): void;
|
|
17
|
+
onRemove(container: ProxyDiContainer, dependencyId: DependencyId, dependency: any): void;
|
|
18
18
|
}
|
|
19
19
|
export declare class MiddlewareListener {
|
|
20
20
|
private parent?;
|
|
@@ -24,6 +24,6 @@ export declare class MiddlewareListener {
|
|
|
24
24
|
remove(middleware: any): void;
|
|
25
25
|
private on;
|
|
26
26
|
onRegister(container: ProxyDiContainer, dependencyId: DependencyId, dependency: any): void;
|
|
27
|
-
onRemove(container: ProxyDiContainer, dependencyId: DependencyId): void;
|
|
27
|
+
onRemove(container: ProxyDiContainer, dependencyId: DependencyId, dependency: any): void;
|
|
28
28
|
private off;
|
|
29
29
|
}
|