Comparing version 4.0.0-rc.2 to 4.0.0-rc-3
{ | ||
"name": "cheap-di", | ||
"description": "TypeScript dependency injection like Autofac in .Net", | ||
"version": "4.0.0-rc.2", | ||
"version": "4.0.0-rc-3", | ||
"scripts": { | ||
@@ -18,2 +18,5 @@ "compile": "tsc --build tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json", | ||
"rimraf": "5.0.5", | ||
"@types/jest": "29.5.8", | ||
"jest": "29.7.0", | ||
"ts-jest": "29.1.1", | ||
"ts-node": "10.9.1" | ||
@@ -20,0 +23,0 @@ }, |
@@ -21,3 +21,3 @@ import { CircularDependencyError } from './CircularDependencyError.js'; | ||
singletons: Map<ImplementationType<any>, object>; | ||
instances: Map<RegistrationType<any>, any>; | ||
instances: Map<RegistrationType<any>, object>; | ||
dependencies: Map<RegistrationType<any>, ImplementationType<any> | object>; | ||
@@ -32,3 +32,3 @@ | ||
/** register implementation class */ | ||
registerImplementation<TInstance>(implementationType: ImplementationType<TInstance>) { | ||
registerImplementation<TInstance>(implementationType: Constructor<TInstance>) { | ||
const inject = (...injectionParams: any[]) => { | ||
@@ -167,7 +167,7 @@ modifyConstructor(implementationType, (settings) => { | ||
if (dependencies?.length) { | ||
const injectableDependencies = dependencies.filter((dependency) => dependency !== 'unknown') as Dependency[]; | ||
// const length = injectableDependencies.length + injectionParams.length; | ||
const resolvedDependencies: any[] = []; | ||
const definedDependencies = dependencies.filter((dependency) => dependency !== 'unknown') as Dependency[]; | ||
while (true) { | ||
const dependencyType = injectableDependencies[index]; | ||
const dependencyType = definedDependencies[index]; | ||
if (dependencyType) { | ||
@@ -177,14 +177,17 @@ trace.addTrace(dependencyType.name); | ||
const instance = this.internalResolve(dependencyType, trace.trace!); | ||
const isNewInstance = | ||
!this.isRegisteredInstance(dependencyType, instance) && !this.isRegisteredType(dependencyType, instance); | ||
if ( | ||
!(this.isRegisteredInstance(dependencyType, instance) || this.isRegisteredType(dependencyType, instance)) && | ||
// todo: can't remember why is it, have to add comment ... | ||
isNewInstance && | ||
injectionParams[injectionParamsIndex] instanceof Object && | ||
injectionParams[injectionParamsIndex].constructor === dependencyType | ||
) { | ||
injectableArguments[index] = injectionParams[injectionParamsIndex]; | ||
resolvedDependencies[index] = injectionParams[injectionParamsIndex]; | ||
injectionParamsIndex++; | ||
} else { | ||
injectableArguments[index] = instance; | ||
resolvedDependencies[index] = instance; | ||
} | ||
} else if (injectionParams[injectionParamsIndex]) { | ||
injectableArguments[index] = injectionParams[injectionParamsIndex]; | ||
resolvedDependencies[index] = injectionParams[injectionParamsIndex]; | ||
injectionParamsIndex++; | ||
@@ -195,7 +198,67 @@ } | ||
// if (index >= length) { | ||
if (index >= injectableDependencies.length) { | ||
if (index >= definedDependencies.length) { | ||
break; | ||
} | ||
} | ||
// put resolved dependencies and injection params in the defined order | ||
let dependencyIndex = 0; | ||
let paramsIndex = 0; | ||
for (let index = 0; index < dependencies.length; index++) { | ||
const dependency = dependencies[index]; | ||
if (dependency === 'unknown') { | ||
const injectionParam = injectionParams[paramsIndex]; | ||
paramsIndex++; | ||
injectableArguments[index] = injectionParam; | ||
} else { | ||
const resolvedDependency = resolvedDependencies[dependencyIndex]; | ||
dependencyIndex++; | ||
injectableArguments[index] = resolvedDependency; | ||
} | ||
} | ||
// pass rest arguments to the end | ||
if (paramsIndex < injectionParams.length) { | ||
const restParams = injectionParams.slice(paramsIndex); | ||
const notReplacedParams: any[] = []; | ||
/** | ||
* @example | ||
* class MyService { | ||
* constructor(public some: string) {} | ||
* } | ||
* | ||
* @inject(MyService) | ||
* class Some { | ||
* constructor(public service: MyService) {} | ||
* } | ||
* | ||
* container.registerImplementation(Some).inject(new MyService('123')); | ||
* | ||
* container.resolve(Some); // here you would like to get Some with MyService with '123', and not auto-resolved MyService | ||
* */ | ||
restParams.forEach((restParameter) => { | ||
if (!restParameter) { | ||
notReplacedParams.push(restParameter); | ||
return; | ||
} | ||
if (typeof restParameter !== 'object') { | ||
notReplacedParams.push(restParameter); | ||
return; | ||
} | ||
const replaceableIndex = injectableArguments.findIndex( | ||
(argument) => | ||
argument != null && typeof argument === 'object' && argument instanceof restParameter.constructor | ||
); | ||
if (replaceableIndex === -1) { | ||
notReplacedParams.push(restParameter); | ||
return; | ||
} | ||
injectableArguments.splice(replaceableIndex, 1, restParameter); | ||
}); | ||
injectableArguments.push(...restParams); | ||
} | ||
} else { | ||
@@ -202,0 +265,0 @@ injectableArguments = injectionParams; |
@@ -33,3 +33,3 @@ import { cheapDiSymbol } from './cheapDiSymbol.js'; | ||
/** register implementation class */ | ||
registerImplementation: <TClass>(implementationType: ImplementationType<TClass>) => { | ||
registerImplementation: <TClass>(implementationType: Constructor<TClass>) => { | ||
/** as super class */ | ||
@@ -36,0 +36,0 @@ as: <TBase extends Partial<TClass>>(type: RegistrationType<TBase>) => WithInjectionParams; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43978
21
1190
6