fixed recursive linking

beta.r3js.org
-=yb4f310 2017-09-02 12:54:03 +02:00
parent 66cd4c7ba1
commit 69a0556424
1 changed files with 125 additions and 60 deletions

View File

@ -167,7 +167,10 @@ GameLib.System.Linking.prototype.link = function(component, data) {
GameLib.System.Linking.prototype.resolveDependencies = function(component) { GameLib.System.Linking.prototype.resolveDependencies = function(component) {
if (!component.loaded) { if (!component.loaded) {
return; /**
* This component has not fully loaded - we should resolve its dependencies later
*/
return false;
} }
var parentComponents = this.dependencies[component.id]; var parentComponents = this.dependencies[component.id];
@ -181,10 +184,11 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) {
* We don't know about components which depend on this component - but it could still load. * 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 * However, it is stored in the register and dependency list for later use
*/ */
return false;
} else { }
parentComponents.map( parentComponents.map(
function (parentComponent) { function (parentComponent) {
/** /**
@ -192,6 +196,9 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) {
*/ */
this.link(parentComponent, {component: component}); this.link(parentComponent, {component: component});
/**
* We record that we linked a child component to a parent component
*/
if (this.resolved.indexOf(component) === -1) { if (this.resolved.indexOf(component) === -1) {
this.resolved.push(component); this.resolved.push(component);
} }
@ -200,10 +207,33 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) {
* First check if the dependencies have already been met * First check if the dependencies have already been met
*/ */
if (GameLib.Utils.UndefinedOrNull(parentComponent.dependencies)) { if (GameLib.Utils.UndefinedOrNull(parentComponent.dependencies)) {
/** /**
* These dependencies have already been met - it is time to update the instance * 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
*/
if (!parentComponent.instance) {
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);
}
} 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(); parentComponent.updateInstance();
}
return; return;
} }
@ -242,6 +272,9 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) {
delete this.dependencies[component.id]; 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.Emit(
GameLib.Event.UNRESOLVED_DEPENDENCIES_UPDATE, GameLib.Event.UNRESOLVED_DEPENDENCIES_UPDATE,
{ {
@ -251,6 +284,9 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) {
if (GameLib.Utils.IsEmpty(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.Emit(
GameLib.Event.COMPONENTS_LINKED, GameLib.Event.COMPONENTS_LINKED,
{ {
@ -264,7 +300,29 @@ GameLib.System.Linking.prototype.resolveDependencies = function(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;
}; };
@ -358,7 +416,14 @@ GameLib.System.Linking.prototype.componentCreated = function(data) {
/** /**
* Resolve any dependencies to this component * 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
*/
}
}; };