Comparing version 0.1.4 to 0.1.5
@@ -5,13 +5,2 @@ "use strict"; | ||
const dependencies = new Map(); | ||
function makeInstance(instanceType, args) { | ||
const implementation = dependencies.get(instanceType); | ||
if (typeof implementation !== "function") { | ||
return implementation; | ||
} | ||
const injectionParams = implementation.__injectionParams || []; | ||
return new implementation(...[ | ||
...injectionParams, | ||
...args, | ||
]); | ||
} | ||
const container = { | ||
@@ -44,15 +33,20 @@ registerType: function (implementationType) { | ||
resolve: function (type, ...args) { | ||
let implementation = type; | ||
if (dependencies.has(type)) { | ||
return makeInstance(type, args); | ||
implementation = dependencies.get(type); | ||
} | ||
if (!type.__constructorParams || !Array.isArray(type.__constructorParams)) { | ||
throw new Error(`The ${type.name} cannot be resolved by Dependency Injection`); | ||
if (typeof implementation !== 'function') { | ||
return implementation; | ||
} | ||
const dependencyArguments = []; | ||
type.__constructorParams.forEach((type) => { | ||
const instance = container.resolve(type); | ||
dependencyArguments.push(instance); | ||
}); | ||
return new type(...[ | ||
if (implementation.__constructorParams) { | ||
implementation.__constructorParams.forEach((type) => { | ||
const instance = container.resolve(type); | ||
dependencyArguments.push(instance); | ||
}); | ||
} | ||
const injectionParams = implementation.__injectionParams || []; | ||
return new implementation(...[ | ||
...dependencyArguments, | ||
...injectionParams, | ||
...args, | ||
@@ -59,0 +53,0 @@ ]); |
{ | ||
"name": "cheap-di", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "JavaScript dependency injection like Autofac in .Net", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
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
11737
110