fix: Try another api

This commit is contained in:
2024-08-28 15:46:39 +02:00
parent e0ab54d38b
commit b3de04e3f9

View File

@@ -49,7 +49,10 @@ export function Inject<TargetType, DependencyType, PropertyType>(
return function ( return function (
constructor: undefined, constructor: undefined,
context: ClassFieldDecoratorContext<TargetType>, context: ClassFieldDecoratorContext<TargetType>,
): void { ): (
this: TargetType,
initialValue: PropertyType | undefined,
) => PropertyType | undefined {
const _identifier = identifier ?? context.name; const _identifier = identifier ?? context.name;
if (_identifier == null && necessary === true) if (_identifier == null && necessary === true)
@@ -67,59 +70,54 @@ export function Inject<TargetType, DependencyType, PropertyType>(
); );
}; };
context.addInitializer(function (this: TargetType) { return function (
Object.defineProperty(this, context.name, { this: TargetType,
get() { initialValue: PropertyType | undefined,
let instance: DependencyType | PropertyType | undefined; ): PropertyType | undefined {
let instance: DependencyType | PropertyType | undefined;
const dependency: DependencyType | undefined = tryAndCatch( const dependency: DependencyType | undefined = tryAndCatch(
() => resolve(), () => 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, necessary,
_identifier, _identifier,
DependencyResolutionError, InitializationError,
); );
else if (necessary)
throw new NoInstantiationMethodError(_identifier);
} else if (necessary)
throw new DependencyResolutionError(_identifier);
if (dependency != null) { /**
const initFunction: (() => PropertyType) | undefined = * Replace itself with the resolved dependency
typeof init === 'function' && dependency != null * for performance reasons.
? (): PropertyType => init(dependency) */
: init === true && hasConstructor(dependency) Object.defineProperty(this, context.name, {
? (): PropertyType => value: instance,
new dependency() as PropertyType writable: false,
: undefined; enumerable: false,
configurable: false,
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,
}); });
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return instance as any;
};
}; };
} }