Comparing version 1.3.11 to 1.3.12
{ | ||
"name": "hekdi", | ||
"version": "1.3.11", | ||
"version": "1.3.12", | ||
"description": "Depedency injection framework for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,6 +9,3 @@ 'use strict'; | ||
DI.prototype.module = function(moduleConfig) { | ||
if (!(moduleConfig instanceof Module)) { | ||
return Module.createModule(moduleConfig); | ||
} | ||
return moduleConfig; | ||
return moduleConfig instanceof Module ? moduleConfig : Module.createModule(moduleConfig); | ||
}; | ||
@@ -15,0 +12,0 @@ |
'use strict'; | ||
const errors = require('./utils/errors'); | ||
const strategies = require('./utils/strategies'); | ||
const resolutionChecker = require('./utils/resolution_checker'); | ||
/** | ||
* | ||
* @param moduleName {string} | ||
* @param module {Module} | ||
* @constructor | ||
*/ | ||
function Injector(moduleName) { | ||
this.belongTo = moduleName; | ||
function Injector(module) { | ||
this.belongTo = module; | ||
this.dependencies = new Map(); | ||
this.resolutionTrace = []; | ||
} | ||
@@ -21,6 +21,12 @@ | ||
Injector.prototype.resolve = function(dependencyName) { | ||
if (this.dependencies.has(dependencyName) && resolutionChecker.call(this, null, dependencyName)) { | ||
return this.dependencies.get(dependencyName).resolver(); | ||
if (this.dependencies.has(dependencyName)) { | ||
try { | ||
const dependency = this.getConfigOf(dependencyName).resolver(); | ||
this.resolutionTrace = []; | ||
return dependency; | ||
} catch (message) { | ||
throw new Error(errors.circularDependency(this.belongTo.name, dependencyName, message)); | ||
} | ||
} | ||
throw new ReferenceError(errors.unmetDependency(this.belongTo, dependencyName)); | ||
throw new ReferenceError(errors.unmetDependency(this.belongTo.name, dependencyName)); | ||
}; | ||
@@ -33,2 +39,6 @@ | ||
dependencies.forEach((dependencyConfig, key) => { | ||
if (this.dependencies.has(key)) { | ||
console.warn(`Name collision happened while adding imports. ${key} is already exists in ${this.belongTo.name}`); | ||
return; | ||
} | ||
this.dependencies.set(key, dependencyConfig); | ||
@@ -35,0 +45,0 @@ }); |
@@ -17,3 +17,3 @@ 'use strict'; | ||
this.name = config.name; | ||
this.injector = new Injector(this.name); | ||
this.injector = new Injector(this); | ||
@@ -38,3 +38,3 @@ if (config.imports) { | ||
const dependencyConfig = this.injector.getConfigOf(dependencyName); | ||
if (dependencyConfig.belongTo === this.name) { | ||
if (dependencyConfig.belongTo.name === this.name) { | ||
this.exports.set(dependencyName, dependencyConfig); | ||
@@ -41,0 +41,0 @@ } |
@@ -6,3 +6,3 @@ 'use strict'; | ||
unmetDependency: (moduleName, dependencyName) => `Unmet dependency '${dependencyName}' in module '${moduleName}'`, | ||
circularDependency: (dep, list) => `Circular dependency found while resolving ${dep}: ${list}`, | ||
circularDependency: (moduleName, dependencyName, message) => `Circular dependency found in module '${moduleName}' while resolving ${dependencyName}! ${message.moduleName}: ${message.resolutionTrace.join(' -> ')}`, | ||
incorrectResolutionStrategy: (strategy, strategies) => ( | ||
@@ -9,0 +9,0 @@ `strategy ${strategy} is incorrect. Allowable values are ${Object.keys(strategies).join(', ')}` |
@@ -10,5 +10,5 @@ /** | ||
if (this.dependencies.has(dependencyName)) { | ||
return this.dependencies.get(dependencyName).resolver(); | ||
return this.getConfigOf(dependencyName).resolver(); | ||
} | ||
throw new ReferenceError(errors.unmetDependency(this.belongTo, dependencyName)); | ||
throw new ReferenceError(errors.unmetDependency(this.belongTo.name, dependencyName)); | ||
}; | ||
@@ -18,3 +18,13 @@ | ||
const config = this.getConfigOf(dependencyName); | ||
return new config.value(...(config.value.$inject || []).map(name => resolveHelper.call(this, name))); | ||
if (this.resolutionTrace.indexOf(dependencyName) !== -1) { | ||
this.resolutionTrace.push(dependencyName); | ||
throw { | ||
moduleName: this.belongTo.name, | ||
resolutionTrace: this.resolutionTrace | ||
}; | ||
} | ||
this.resolutionTrace.push(dependencyName); | ||
const d = new config.value(...(config.value.$inject || []).map(name => resolveHelper.call(this, name))); | ||
this.resolutionTrace.pop(); | ||
return d; | ||
}; | ||
@@ -21,0 +31,0 @@ |
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
19144
14
337