Compare commits

...

4 Commits

Author SHA1 Message Date
16bc8e4187 fix: Test 2024-08-28 15:48:45 +02:00
b3de04e3f9 fix: Try another api 2024-08-28 15:46:39 +02:00
e0ab54d38b fix: Fix import paths to relative 2024-08-28 15:12:10 +02:00
45bec44a2d fix: Fix typing in injector 2024-08-28 15:06:38 +02:00
4 changed files with 53 additions and 57 deletions

View File

@@ -1,9 +1,9 @@
/* istanbul ignore file */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Inject } from 'src/decorators/Inject';
import { DependencyResolutionError } from 'src/interfaces/Exceptions';
import { ForceConstructor } from 'src/types/GenericContructor';
import { DependencyResolutionError } from '../interfaces/Exceptions';
import { ITSinjex_, ITSinjex } from '../interfaces/ITSinjex';
import { ForceConstructor } from '../types/GenericContructor';
/**
* Test the Inject decorator.

View File

@@ -1,11 +1,11 @@
import { TSinjex } from '../classes/TSinjex';
import {
DependencyResolutionError,
IdentifierRequiredError,
InitializationError,
InjectorError,
NoInstantiationMethodError,
} from 'src/interfaces/Exceptions';
import { TSinjex } from '../classes/TSinjex';
} from '../interfaces/Exceptions';
import { Identifier } from '../types/Identifier';
import { InitDelegate } from '../types/InitDelegate';
@@ -48,10 +48,11 @@ export function Inject<TargetType, DependencyType, PropertyType>(
) {
return function (
constructor: undefined,
context: ClassFieldDecoratorContext<TargetType> & {
name: PropertyType;
},
): void {
context: ClassFieldDecoratorContext<TargetType>,
): (
this: TargetType,
initialValue: PropertyType | undefined,
) => PropertyType {
const _identifier = identifier ?? context.name;
if (_identifier == null && necessary === true)
@@ -69,59 +70,54 @@ export function Inject<TargetType, DependencyType, PropertyType>(
);
};
context.addInitializer(function (this: TargetType) {
Object.defineProperty(this, context.name, {
get() {
let instance: DependencyType | PropertyType | undefined;
return function (
this: TargetType,
initialValue: PropertyType | undefined,
): PropertyType {
let instance: DependencyType | PropertyType | undefined;
const dependency: DependencyType | undefined = tryAndCatch(
() => resolve(),
const dependency: DependencyType | undefined = tryAndCatch(
() => resolve(),
necessary,
_identifier,
DependencyResolutionError,
);
if (dependency != null) {
const initFunction: (() => PropertyType) | undefined =
typeof init === 'function' && dependency != null
? (): PropertyType => init(dependency)
: init === true && hasConstructor(dependency)
? (): PropertyType => new dependency() as PropertyType
: undefined;
if (init == null) instance = dependency;
else if (initFunction != null)
instance = tryAndCatch(
initFunction,
necessary,
_identifier,
DependencyResolutionError,
InitializationError,
);
else if (necessary)
throw new NoInstantiationMethodError(_identifier);
} else if (necessary)
throw new DependencyResolutionError(_identifier);
if (dependency != null) {
const initFunction: (() => PropertyType) | undefined =
typeof init === 'function' && dependency != null
? (): PropertyType => init(dependency)
: init === true && hasConstructor(dependency)
? (): PropertyType =>
new dependency() as PropertyType
: undefined;
if (init == null) instance = dependency;
else if (initFunction != null)
instance = tryAndCatch(
initFunction,
necessary,
_identifier,
InitializationError,
);
else if (necessary)
throw new NoInstantiationMethodError(_identifier);
} else if (necessary)
throw new DependencyResolutionError(_identifier);
/**
* Replace itself with the resolved dependency
* for performance reasons.
*/
Object.defineProperty(this, context.name, {
value: instance,
writable: false,
enumerable: false,
configurable: false,
});
return instance;
},
/**
* Make the property configurable to allow replacing it
*/
configurable: true,
/**
* Replace itself with the resolved dependency
* for performance reasons.
*/
Object.defineProperty(this, context.name, {
value: instance,
writable: false,
enumerable: false,
configurable: false,
});
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return instance as any;
};
};
}

View File

@@ -1,5 +1,5 @@
import { IdentifierRequiredError } from 'src/interfaces/Exceptions';
import { TSinjex } from '../classes/TSinjex';
import { IdentifierRequiredError } from '../interfaces/Exceptions';
import { Identifier } from '../types/Identifier';
/**

View File

@@ -1,5 +1,5 @@
import { IdentifierRequiredError } from 'src/interfaces/Exceptions';
import { TSinjex } from '../classes/TSinjex';
import { IdentifierRequiredError } from '../interfaces/Exceptions';
import { Identifier } from '../types/Identifier';
import { InitDelegate } from '../types/InitDelegate';