Skip to content

Instantly share code, notes, and snippets.

@davestewart
Created August 17, 2011 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davestewart/1151958 to your computer and use it in GitHub Desktop.
Save davestewart/1151958 to your computer and use it in GitHub Desktop.
Dependency loader test
/**
* Dependancy loader test
*/
// --------------------------------------------------------------------------------
// functions
trace = fl.trace;
clear = fl.outputPanel.clear;
// --------------------------------------------------------------------------------
// classes object
classes =
{
loaded:[],
level:0,
load:function(name, contents)
{
// cancel if there's no content
if(name == '')
{
return false;
}
// load the class if not already loaded
if(this.loaded.indexOf(name) == -1)
{
// report
this.level++;
this.trace('LOADING "' + name + '" (requires "' +contents.join('", "')+ '")', 0);
this.loaded.push(name);
// requirements
for(var i = 0; i < contents.length; i++)
{
this.require(contents[i], name);
}
// report
this.trace('"' + name.toUpperCase() + '" COMPLETE!', 1);
trace('')
this.level--;
// return
return true;
}
// if it is loaded, cancel
else
{
this.level++;
this.trace('"' + name + '" is already loaded!');
this.level--;
return false;
}
},
require:function(name, parent)
{
this.level++;
if(this.loaded.indexOf(name) == -1)
{
this.load(name, files[name]);
}
else
{
this.trace('"' + name + '" is already loaded!');
}
this.level--;
},
trace:function(message, action)
{
action = action == undefined ? 2 : action;
var sign = [' => ', ' :) ', ' '][action];
trace(this.indent + sign + message);
},
get indent()
{
return new Array(this.level * 2).join(' ');
}
}
// --------------------------------------------------------------------------------
// code
// clear
clear();
// dependancies
var files =
{
A:['B', 'E'],
B:['A',],
C:['A', 'K', 'C'],
D:['I'],
E:['F'],
F:['B', 'E'],
G:['C'],
H:['K', 'D', 'A'],
I:['C', 'D', 'E'],
J:['E'],
K:['H', 'D', 'J']
}
// load
for(var f in files)
{
classes.load(f, files[f]);
}
// report
trace('\nLoad order\n---------------------------');
var names = xjsfl.utils.getKeys(files);
for each(var name in classes.loaded)
{
var index = names.indexOf(name) + 1
trace(name + '|' + new Array(index).join(' ') + index)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment