mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 21:22:48 +01:00
di: Removed unused code. Implemented circular references detection
This commit is contained in:
@@ -45,7 +45,6 @@ app.compose(function() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
app.di.resolveProperties(h);
|
|
||||||
handlers.push(h);
|
handlers.push(h);
|
||||||
},
|
},
|
||||||
createHandler: function(cmd, handler) {
|
createHandler: function(cmd, handler) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
(function(is){
|
(function(is){
|
||||||
function Container(store) {
|
function Container(store) {
|
||||||
this.store = {};
|
this.store = {};
|
||||||
this.resolutionPath = [];
|
this.resolutionStack = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
Container.prototype.register = function(name, def) {
|
Container.prototype.register = function(name, def) {
|
||||||
@@ -25,14 +25,17 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
Container.prototype.resolve = function(name) {
|
Container.prototype.resolve = function(name) {
|
||||||
|
return resolveInternal.call(this, name);
|
||||||
|
};
|
||||||
|
|
||||||
if(contains(this.resolutionPath, name)) {
|
function resolveInternal(name) {
|
||||||
throw new Error("Failed to resolve service: " + name + ". Circular reference: " + this.resolutionPath.join(' < '));
|
if(contains(this.resolutionStack, name)) {
|
||||||
|
throw new Error("Failed to resolve service: " + name + ". Circular reference: " + this.resolutionStack.join(' < '));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resolutionPath.unshift(name);
|
this.resolutionStack.unshift(name);
|
||||||
|
|
||||||
console.log('resolution path:' + this.resolutionPath.join(' < '));
|
console.log('\tresolution path:' + this.resolutionStack.join(' < '));
|
||||||
|
|
||||||
var reg = this.store[name];
|
var reg = this.store[name];
|
||||||
if(reg == null) {
|
if(reg == null) {
|
||||||
@@ -43,35 +46,13 @@
|
|||||||
reg.createInstance();
|
reg.createInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resolutionPath.shift(name);
|
this.resolutionStack.shift();
|
||||||
|
|
||||||
|
console.log('\tresolution path:' + this.resolutionStack.join(' < '));
|
||||||
|
|
||||||
return reg.resolved;
|
return reg.resolved;
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.resolveProperties = function (instance) {
|
|
||||||
for(var prop in instance) {
|
|
||||||
if(!instance.hasOwnProperty(prop)) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(prop[0] == '$' && instance[prop] == null) {
|
|
||||||
var key = prop.substr(1, prop.length - 1);
|
|
||||||
instance[prop] = this.resolve(key);
|
|
||||||
|
|
||||||
if(instance[prop] == null) {
|
|
||||||
console.log('"' + prop + '" property couldn\'t be resolved')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Container.prototype.resolveMany = function (arr) {
|
|
||||||
var resolved = [], i = 0;
|
|
||||||
for(;i<0;i++) {
|
|
||||||
resolved.push(this.resolve(arr[i]));
|
|
||||||
}
|
|
||||||
return resolved;
|
|
||||||
};
|
|
||||||
|
|
||||||
function Registration(definition) {
|
function Registration(definition) {
|
||||||
this.def = definition;
|
this.def = definition;
|
||||||
@@ -81,14 +62,8 @@
|
|||||||
Registration.prototype.createInstance = function() {
|
Registration.prototype.createInstance = function() {
|
||||||
var def = this.def;
|
var def = this.def;
|
||||||
if(typeof def == "function") {
|
if(typeof def == "function") {
|
||||||
|
|
||||||
if(is.array(def.inject)) {
|
|
||||||
this.resolved = def.apply(window, this.resolveMany(def.inject))
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.resolved = def();
|
this.resolved = def();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
// this.resolveProperties(inst);
|
// this.resolveProperties(inst);
|
||||||
this.resolved = def;
|
this.resolved = def;
|
||||||
|
|||||||
Reference in New Issue
Block a user