Compare commits
4 Commits
v0.1.0-alp
...
dev/v1.0.0
Author | SHA1 | Date | |
---|---|---|---|
16bc8e4187 | |||
b3de04e3f9 | |||
e0ab54d38b | |||
45bec44a2d |
@@ -1,9 +1,9 @@
|
|||||||
/* istanbul ignore file */
|
/* istanbul ignore file */
|
||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
import { Inject } from 'src/decorators/Inject';
|
import { Inject } from 'src/decorators/Inject';
|
||||||
import { DependencyResolutionError } from 'src/interfaces/Exceptions';
|
import { DependencyResolutionError } from '../interfaces/Exceptions';
|
||||||
import { ForceConstructor } from 'src/types/GenericContructor';
|
|
||||||
import { ITSinjex_, ITSinjex } from '../interfaces/ITSinjex';
|
import { ITSinjex_, ITSinjex } from '../interfaces/ITSinjex';
|
||||||
|
import { ForceConstructor } from '../types/GenericContructor';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the Inject decorator.
|
* Test the Inject decorator.
|
||||||
|
@@ -1,11 +1,11 @@
|
|||||||
|
import { TSinjex } from '../classes/TSinjex';
|
||||||
import {
|
import {
|
||||||
DependencyResolutionError,
|
DependencyResolutionError,
|
||||||
IdentifierRequiredError,
|
IdentifierRequiredError,
|
||||||
InitializationError,
|
InitializationError,
|
||||||
InjectorError,
|
InjectorError,
|
||||||
NoInstantiationMethodError,
|
NoInstantiationMethodError,
|
||||||
} from 'src/interfaces/Exceptions';
|
} from '../interfaces/Exceptions';
|
||||||
import { TSinjex } from '../classes/TSinjex';
|
|
||||||
import { Identifier } from '../types/Identifier';
|
import { Identifier } from '../types/Identifier';
|
||||||
import { InitDelegate } from '../types/InitDelegate';
|
import { InitDelegate } from '../types/InitDelegate';
|
||||||
|
|
||||||
@@ -48,10 +48,11 @@ export function Inject<TargetType, DependencyType, PropertyType>(
|
|||||||
) {
|
) {
|
||||||
return function (
|
return function (
|
||||||
constructor: undefined,
|
constructor: undefined,
|
||||||
context: ClassFieldDecoratorContext<TargetType> & {
|
context: ClassFieldDecoratorContext<TargetType>,
|
||||||
name: PropertyType;
|
): (
|
||||||
},
|
this: TargetType,
|
||||||
): void {
|
initialValue: PropertyType | undefined,
|
||||||
|
) => PropertyType {
|
||||||
const _identifier = identifier ?? context.name;
|
const _identifier = identifier ?? context.name;
|
||||||
|
|
||||||
if (_identifier == null && necessary === true)
|
if (_identifier == null && necessary === true)
|
||||||
@@ -69,9 +70,10 @@ export function Inject<TargetType, DependencyType, PropertyType>(
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
context.addInitializer(function (this: TargetType) {
|
return function (
|
||||||
Object.defineProperty(this, context.name, {
|
this: TargetType,
|
||||||
get() {
|
initialValue: PropertyType | undefined,
|
||||||
|
): PropertyType {
|
||||||
let instance: DependencyType | PropertyType | undefined;
|
let instance: DependencyType | PropertyType | undefined;
|
||||||
|
|
||||||
const dependency: DependencyType | undefined = tryAndCatch(
|
const dependency: DependencyType | undefined = tryAndCatch(
|
||||||
@@ -86,8 +88,7 @@ export function Inject<TargetType, DependencyType, PropertyType>(
|
|||||||
typeof init === 'function' && dependency != null
|
typeof init === 'function' && dependency != null
|
||||||
? (): PropertyType => init(dependency)
|
? (): PropertyType => init(dependency)
|
||||||
: init === true && hasConstructor(dependency)
|
: init === true && hasConstructor(dependency)
|
||||||
? (): PropertyType =>
|
? (): PropertyType => new dependency() as PropertyType
|
||||||
new dependency() as PropertyType
|
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
if (init == null) instance = dependency;
|
if (init == null) instance = dependency;
|
||||||
@@ -114,14 +115,9 @@ export function Inject<TargetType, DependencyType, PropertyType>(
|
|||||||
configurable: false,
|
configurable: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
return instance;
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
},
|
return instance as any;
|
||||||
/**
|
};
|
||||||
* Make the property configurable to allow replacing it
|
|
||||||
*/
|
|
||||||
configurable: true,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import { IdentifierRequiredError } from 'src/interfaces/Exceptions';
|
|
||||||
import { TSinjex } from '../classes/TSinjex';
|
import { TSinjex } from '../classes/TSinjex';
|
||||||
|
import { IdentifierRequiredError } from '../interfaces/Exceptions';
|
||||||
import { Identifier } from '../types/Identifier';
|
import { Identifier } from '../types/Identifier';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import { IdentifierRequiredError } from 'src/interfaces/Exceptions';
|
|
||||||
import { TSinjex } from '../classes/TSinjex';
|
import { TSinjex } from '../classes/TSinjex';
|
||||||
|
import { IdentifierRequiredError } from '../interfaces/Exceptions';
|
||||||
import { Identifier } from '../types/Identifier';
|
import { Identifier } from '../types/Identifier';
|
||||||
import { InitDelegate } from '../types/InitDelegate';
|
import { InitDelegate } from '../types/InitDelegate';
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user