Comparing version 1.5.0 to 1.5.1
{ | ||
"name": "hekdi", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "Depedency injection framework for node integrated with koa.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,3 +16,2 @@ 'use strict'; | ||
DI.prototype.resolve = function(dependencyName) { | ||
@@ -19,0 +18,0 @@ return this.main.injector.resolve(dependencyName); |
@@ -13,3 +13,2 @@ 'use strict'; | ||
this.dependencies = new Map(); | ||
this.resolutionTrace = []; | ||
} | ||
@@ -22,12 +21,8 @@ | ||
Injector.prototype.resolve = function(dependencyName) { | ||
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)); | ||
} | ||
const trace = []; | ||
const config = this.getConfigOf(dependencyName); | ||
if (config === undefined) { | ||
throw new ReferenceError(errors.unmetDependency(this.belongTo.name, dependencyName)); | ||
} | ||
throw new ReferenceError(errors.unmetDependency(this.belongTo.name, dependencyName)); | ||
return config.resolver(trace); | ||
}; | ||
@@ -34,0 +29,0 @@ |
@@ -10,3 +10,3 @@ /** | ||
/** | ||
* @param app {Koa.application} | ||
* @param app {Koa.Application} | ||
* @param original {Function} | ||
@@ -13,0 +13,0 @@ * @returns {Function} |
@@ -6,3 +6,5 @@ 'use strict'; | ||
unmetDependency: (moduleName, dependencyName) => `Unmet dependency '${dependencyName}' in module '${moduleName}'`, | ||
circularDependency: (moduleName, dependencyName, message) => `Circular dependency found in module '${moduleName}' while resolving ${dependencyName}! ${message.moduleName}: ${message.resolutionTrace.join(' -> ')}`, | ||
circularDependency: (moduleName, dependencyName, trace) => ( | ||
`Circular dependency found in module '${moduleName}' while resolving ${dependencyName}! Full path: ${trace.join(' -> ')}` | ||
), | ||
incorrectResolutionStrategy: (strategy, strategies) => ( | ||
@@ -9,0 +11,0 @@ `strategy ${strategy} is incorrect. Allowable values are ${Object.keys(strategies).join(', ')}` |
@@ -8,23 +8,21 @@ /** | ||
const resolveHelper = function(dependencyName) { | ||
if (this.dependencies.has(dependencyName)) { | ||
return this.getConfigOf(dependencyName).resolver(); | ||
} | ||
throw new ReferenceError(errors.unmetDependency(this.belongTo.name, dependencyName)); | ||
}; | ||
const resolveDependency = function(dependencyName, strategy) { | ||
const resolveDependency = function(dependencyName, trace) { | ||
const config = this.getConfigOf(dependencyName); | ||
if (this.resolutionTrace.indexOf(dependencyName) !== -1) { | ||
this.resolutionTrace.push(dependencyName); | ||
throw { | ||
moduleName: this.belongTo.name, | ||
resolutionTrace: this.resolutionTrace | ||
}; | ||
if (config === undefined) { | ||
throw new ReferenceError(errors.unmetDependency(this.belongTo.name, dependencyName)); | ||
} else if (trace.includes(dependencyName)) { | ||
trace.push(dependencyName); | ||
throw new Error(errors.circularDependency(this.belongTo.name, dependencyName, trace)); | ||
} | ||
this.resolutionTrace.push(dependencyName); | ||
const deps = (config.value.$inject || []).map(name => resolveHelper.call(this, name)); | ||
const isFactory = strategy === 'factory'; | ||
const d = isFactory ? config.value(...deps) : new config.value(...deps); | ||
this.resolutionTrace.pop(); | ||
trace.push(dependencyName); | ||
const mayHaveDeps = ['service', 'factory', 'singleton'].includes(config.strategy); | ||
let d; | ||
if (mayHaveDeps) { | ||
const deps = (config.value.$inject || []).map(name => this.getConfigOf(name).resolver(trace)); | ||
const isFactory = config.strategy === 'factory'; | ||
d = isFactory ? config.value(...deps) : new config.value(...deps); | ||
} else { | ||
d = config.value; | ||
} | ||
trace.pop(); | ||
return d; | ||
@@ -39,4 +37,4 @@ }; | ||
*/ | ||
service: dependencyName => function() { | ||
return resolveDependency.call(this, dependencyName, 'service'); | ||
service: dependencyName => function(trace) { | ||
return resolveDependency.call(this, dependencyName, trace); | ||
}, | ||
@@ -47,4 +45,4 @@ /** | ||
*/ | ||
factory: dependencyName => function() { | ||
return resolveDependency.call(this, dependencyName, 'factory'); | ||
factory: dependencyName => function(trace) { | ||
return resolveDependency.call(this, dependencyName, trace); | ||
}, | ||
@@ -55,7 +53,7 @@ /** | ||
*/ | ||
singleton: dependencyName => { | ||
singleton: (dependencyName) => { | ||
let instance; | ||
return function() { | ||
return function(trace) { | ||
if (!instance) { | ||
instance = resolveDependency.call(this, dependencyName, 'singleton'); | ||
instance = resolveDependency.call(this, dependencyName, trace); | ||
} | ||
@@ -69,4 +67,4 @@ return instance; | ||
*/ | ||
value: dependencyName => function() { | ||
return this.getConfigOf(dependencyName).value; | ||
value: dependencyName => function(trace) { | ||
return resolveDependency.call(this, dependencyName, trace); | ||
}, | ||
@@ -77,4 +75,4 @@ /** | ||
*/ | ||
constant: dependencyName => function() { | ||
return this.getConfigOf(dependencyName).value; | ||
constant: dependencyName => function(trace) { | ||
return resolveDependency.call(this, dependencyName, trace); | ||
}, | ||
@@ -85,7 +83,6 @@ /** | ||
*/ | ||
alias: dependencyName => function() { | ||
alias: dependencyName => function(trace) { | ||
const { value } = this.getConfigOf(dependencyName); | ||
const { name } = this.getConfigOf(value); | ||
return resolveHelper.call(this, name); | ||
return resolveDependency.call(this, value, trace); | ||
} | ||
}; |
18189
318