fixed recursive linking
parent
66cd4c7ba1
commit
69a0556424
|
@ -167,7 +167,10 @@ GameLib.System.Linking.prototype.link = function(component, data) {
|
|||
GameLib.System.Linking.prototype.resolveDependencies = function(component) {
|
||||
|
||||
if (!component.loaded) {
|
||||
return;
|
||||
/**
|
||||
* This component has not fully loaded - we should resolve its dependencies later
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
var parentComponents = this.dependencies[component.id];
|
||||
|
@ -181,48 +184,36 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) {
|
|||
* We don't know about components which depend on this component - but it could still load.
|
||||
* However, it is stored in the register and dependency list for later use
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
parentComponents.map(
|
||||
|
||||
parentComponents.map(
|
||||
function (parentComponent) {
|
||||
function (parentComponent) {
|
||||
|
||||
/**
|
||||
* Link the parent component to this component
|
||||
*/
|
||||
this.link(parentComponent, {component: component});
|
||||
|
||||
/**
|
||||
* We record that we linked a child component to a parent component
|
||||
*/
|
||||
if (this.resolved.indexOf(component) === -1) {
|
||||
this.resolved.push(component);
|
||||
}
|
||||
|
||||
/**
|
||||
* First check if the dependencies have already been met
|
||||
*/
|
||||
if (GameLib.Utils.UndefinedOrNull(parentComponent.dependencies)) {
|
||||
|
||||
/**
|
||||
* Link the parent component to this component
|
||||
* This means - a parent component instance could maybe have been delayed to be created
|
||||
* because the component constructor or linking system did not know at time of 'createInstance'
|
||||
* that it required another object to fully become active
|
||||
*/
|
||||
this.link(parentComponent, {component: component});
|
||||
|
||||
if (this.resolved.indexOf(component) === -1) {
|
||||
this.resolved.push(component);
|
||||
}
|
||||
|
||||
/**
|
||||
* First check if the dependencies have already been met
|
||||
*/
|
||||
if (GameLib.Utils.UndefinedOrNull(parentComponent.dependencies)) {
|
||||
/**
|
||||
* These dependencies have already been met - it is time to update the instance
|
||||
*/
|
||||
parentComponent.updateInstance();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the actual dependency
|
||||
*/
|
||||
var index = parentComponent.dependencies.indexOf(component.id);
|
||||
if (index === -1) {
|
||||
console.warn('dependency mismatch');
|
||||
} else {
|
||||
parentComponent.dependencies.splice(index, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* If we now managed to link the objects, and this object has no more dependencies
|
||||
*/
|
||||
if (parentComponent.dependencies.length === 0) {
|
||||
|
||||
delete parentComponent.dependencies;
|
||||
if (!parentComponent.instance) {
|
||||
|
||||
parentComponent.instance = parentComponent.createInstance();
|
||||
|
||||
|
@ -232,40 +223,107 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) {
|
|||
if (this.resolved.indexOf(parentComponent) === -1) {
|
||||
this.resolved.push(parentComponent);
|
||||
}
|
||||
|
||||
GameLib.Event.EmitInstanceEvents(parentComponent);
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* These dependencies have already been met - the parentComponent properties have changed.
|
||||
* It is time to 'update' this instance with this information (if any of it is relevant - depends
|
||||
* on the component)
|
||||
*/
|
||||
parentComponent.updateInstance();
|
||||
}
|
||||
|
||||
}.bind(this)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
delete this.dependencies[component.id];
|
||||
/**
|
||||
* Remove the actual dependency
|
||||
*/
|
||||
var index = parentComponent.dependencies.indexOf(component.id);
|
||||
if (index === -1) {
|
||||
console.warn('dependency mismatch');
|
||||
} else {
|
||||
parentComponent.dependencies.splice(index, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* If we now managed to link the objects, and this object has no more dependencies
|
||||
*/
|
||||
if (parentComponent.dependencies.length === 0) {
|
||||
|
||||
delete parentComponent.dependencies;
|
||||
|
||||
parentComponent.instance = parentComponent.createInstance();
|
||||
|
||||
if (parentComponent.instance) {
|
||||
parentComponent.loaded = true;
|
||||
parentComponent.buildIdToObject();
|
||||
if (this.resolved.indexOf(parentComponent) === -1) {
|
||||
this.resolved.push(parentComponent);
|
||||
}
|
||||
|
||||
GameLib.Event.EmitInstanceEvents(parentComponent);
|
||||
}
|
||||
}
|
||||
|
||||
}.bind(this)
|
||||
);
|
||||
|
||||
delete this.dependencies[component.id];
|
||||
|
||||
/**
|
||||
* For now this essentially only notifies the Editor - We have some more work to do however
|
||||
*/
|
||||
GameLib.Event.Emit(
|
||||
GameLib.Event.UNRESOLVED_DEPENDENCIES_UPDATE,
|
||||
{
|
||||
dependencies : this.dependencies
|
||||
}
|
||||
);
|
||||
|
||||
if (GameLib.Utils.IsEmpty(this.dependencies)) {
|
||||
|
||||
/**
|
||||
* This also only notifies the Editor - We still have some more work to here
|
||||
*/
|
||||
GameLib.Event.Emit(
|
||||
GameLib.Event.UNRESOLVED_DEPENDENCIES_UPDATE,
|
||||
GameLib.Event.COMPONENTS_LINKED,
|
||||
{
|
||||
dependencies : this.dependencies
|
||||
components: this.resolved.map(
|
||||
function(component) {
|
||||
return component;
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
if (GameLib.Utils.IsEmpty(this.dependencies)) {
|
||||
|
||||
GameLib.Event.Emit(
|
||||
GameLib.Event.COMPONENTS_LINKED,
|
||||
{
|
||||
components: this.resolved.map(
|
||||
function(component) {
|
||||
return component;
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
this.resolved = [];
|
||||
}
|
||||
this.resolved = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* And this is it - we need to check if the dependencies array contains any 'resolved' components -
|
||||
* If it is - resolve the dependencies of this newly 'resolved' component
|
||||
*/
|
||||
this.resolved.map(
|
||||
function(component) {
|
||||
for (var key in this.dependencies) {
|
||||
if (this.dependencies.hasOwnProperty(key)) {
|
||||
if (key === component.id) {
|
||||
/**
|
||||
* We found a resolved component - which is a dependency for another component.
|
||||
* Resolve the dependencies of this component - this is recursive and should be done carefully
|
||||
*/
|
||||
this.resolveDependencies(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
}.bind(this)
|
||||
);
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
};
|
||||
|
||||
GameLib.System.Linking.prototype.registerDependencies = function(component) {
|
||||
|
@ -358,7 +416,14 @@ GameLib.System.Linking.prototype.componentCreated = function(data) {
|
|||
/**
|
||||
* Resolve any dependencies to this component
|
||||
*/
|
||||
this.resolveDependencies(component);
|
||||
var resolved = this.resolveDependencies(component);
|
||||
|
||||
if (resolved) {
|
||||
/**
|
||||
* Some dependencies were resolved - we should check our list of other components with unresolved dependencies
|
||||
* and try to resolve them
|
||||
*/
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue