Permalink
Browse files

revise dependency system

  • Loading branch information...
1 parent a2daf8d commit d5ddac447c3d4c27d0c6e88a1cb1977add874384 Tony Issakov committed Feb 5, 2010
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,9 +1,4 @@
// $e = ($e) ? $e : {};
-console._log = console.log;
-console.log = function(){
- // document.body.innerHTML += Array.prototype.join.apply(arguments, [", "])+"<br/>";
- console._log.apply(window, arguments);
-}
$e.declare(
"$e.Dependency",
@@ -13,24 +8,24 @@ $e.declare(
satisfiedModules: null,
modName: "",
constructor:function(moduleName, requires, code){
- this.satisfiedModules = [];
- this.unsatisfiedModules = [];
- console.log("Creating for module: "+moduleName);
this.modName = moduleName;
+ this.satisfiedModules = [];
this.unsatisfiedModules = requires;
this.waitingCode = code;
},
satisfied: function(moduleName){
this.satisfiedModules.push(moduleName);
- console.log(moduleName+" satisfied for "+this.modName);
if(this.satisfiedModules.length == this.unsatisfiedModules.length){
this.executeCode();
}
},
executeCode: function(){
- console.log("Executing for "+this.modName);
- $p.ping();
- jQuery.globalEval(this.waitingCode);
+ if(typeof(this.waitingCode) == "string"){
+ jQuery.globalEval(this.waitingCode);
+ this.waitingCode = null;
+ } else {
+ this.waitingCode();
+ }
$e.dependencySatisfied(this.modName);
}
}
@@ -39,13 +34,22 @@ $e.declare(
$e.mixin($e,{
_dependenciesList: {},
_depFetchedList: {},
+ _depResolved: {},
+ asyncMode: false,
+ codeReady: function(moduleName, func){
+ $e.dependencyRegister(moduleName, (new $e.Dependency("CODE", [moduleName], func)));
+ },
dependencyRegister: function(moduleName, dependency){
+ if($e._depResolved[moduleName]){
+ // if already resolved, immediately execute
+ dependency.executeCode();
+ }
if(this._dependenciesList[moduleName]==null){ this._dependenciesList[moduleName] = []; }
this._dependenciesList[moduleName].push(dependency);
},
dependencySatisfied: function(moduleName){
- console.log("Satisfied: "+moduleName)
- if(this._dependenciesList[moduleName] && this._dependenciesList[moduleName].length>0){
+ $e._depResolved[moduleName] = true;
+ if(this._dependenciesList[moduleName] && this._dependenciesList[moduleName].length>0){
var items = this._dependenciesList[moduleName];
jQuery.each(items, function(index,item){
item.satisfied(moduleName);
@@ -69,10 +73,9 @@ $e.mixin($e,{
var moduleName = module;
$e._depFetchedList[module] = false;
};
- jQuery.ajax({ url: path, tyep:"get", async: true, context:this, success: sfunc, error: efunc, dataType: "text"});
+ jQuery.ajax({ url: path, tyep:"get", async: $e.asyncMode, context:this, success: sfunc, error: efunc, dataType: "text"});
},
_handleScriptFetch: function(moduleName, data){
- // this._depLoadedList[moduleName]=true;
// pull out provides and requires from commented space
var provides = data.match(/\/\/\*\*(.*)\*\*/g);
var requires = data.match(/\/\/\-\-(.*)\-\-/g);
@@ -85,7 +88,7 @@ $e.mixin($e,{
var requiresSet = $e.collect(requires, function(index, item){
return item.replace(/\/\//g,"").replace(/\-\-/g,"");
});
- console.log("Requires found for "+moduleName+": "+requiresSet.join(","))
+ //console.log("Requires found for "+moduleName+": "+requiresSet.join(","))
// the dependency for this current module
var dep = new $e.Dependency(moduleName, requiresSet, data);
jQuery.each(requiresSet, function(index, item){
@@ -96,7 +99,7 @@ $e.mixin($e,{
})
} else {
//do not require anything so execute the code and notify dependencies
- console.log("Executing for " + moduleName+ " (No Deps)");
+ //console.log("Executing for " + moduleName+ " (No Deps)");
jQuery.globalEval(data);
$e.dependencySatisfied(moduleName);
}
View
@@ -1,14 +1,20 @@
-//**dependency.level1**
-//--dependency.level2--
-//--dependency.level2_1--
-
+//*********
+//**dep.level1**
+//--dep.level2--
+//--dep.level2_1--
+//**********
widgets.level1 = function(){
}
-widgets.something.asd.a = {};
-widgets.something.bsd.a = {};
+widgets.something.asd.a = function(){
+ document.body.innerHTML += "ASD: "+ $p.ping();
+};
+
+for(var x=0;x<10000;x++){
+ var a = 1;
+}
// function pausecomp(millis)
@@ -0,0 +1,3 @@
+//**dep.level1a**
+//--dep.level2a--
+//--dep.level2_1a--
View
@@ -1,5 +1,5 @@
-//**dependency.level2**
-//--dependency.level3--
+//**dep.level2**
+//--dep.level3--
widgets.level2 = function(){}
@@ -1,5 +1,5 @@
-//**dependency.level2_1**
-//--dependency.level3--
+//**dep.level2_1**
+//--dep.level3--
widgets.level21 = function(){}
@@ -0,0 +1 @@
+//**dep.level2_1a**
@@ -0,0 +1 @@
+//**dep.level2a**
View
@@ -1,4 +1,4 @@
-//**dependency.level3**
+//**dep.level3**
widgets.level3= function(){}
View
@@ -0,0 +1,55 @@
+$e.mixin($e, {
+ _loaded: {},
+ _codeWaiting:[],
+ _addressing: [],
+ _moduleToPath: function(module){
+ return $e.basePath + module.replace(".","/") + ".js";
+ },
+ codeReady: function(moduleName, func){
+ if(this._loaded[moduleName]){
+ func()
+ } else {
+ $e._codeWaiting[moduleName] = ($e._codeWaiting[moduleName]) ? $e._codeWaiting[moduleName] : [];
+ $e._codeWaiting[moduleName].push(func);
+ }
+ },
+ require: function(moduleName){
+ if(this._loaded[moduleName]){return;}
+ var path = $e._moduleToPath(moduleName) + "?rand="+(new Date()-1);
+ var sfunc = function(data){
+ //tuck the module name into a scope for later usage
+ return $e._handleScriptFetch(moduleName, data);
+ };
+ jQuery.ajax({ url: path, tyep:"get", async: false, context:this, success: sfunc, dataType: "text"});
+ },
+ _handleScriptFetch: function(moduleName, data){
+ $e._addressing[moduleName] = true;
+ // this._depLoadedList[moduleName]=true;
+ // pull out provides and requires from commented space
+ var provides = data.match(/\/\/\*\*(.*)\*\*/g);
+ var requires = data.match(/\/\/\-\-(.*)\-\-/g);
+ if(provides && (provides.length > 0)){
+ // presume 1 provides line and clean off the commenting.
+ provides = provides[0].replace(/\/\//g,"").replace(/\*\*/g,"");
+ }
+ if(requires && (requires.length>0)){
+ var requiresSet = $e.collect(requires, function(index, item){
+ return item.replace(/\/\//g,"").replace(/\-\-/g,"");
+ });
+ jQuery.each(requiresSet, function(index, item){
+ if(!$e._addressing[item]){
+ $e.require(item)
+ }
+ })
+ }
+ console.log("Executing "+moduleName)
+ jQuery.globalEval(data);
+ this._loaded[moduleName] = true;
+ if($e._codeWaiting[moduleName]){
+ jQuery.each($e._codeWaiting[moduleName],function(index, item){
+ item.apply(this,[]);
+ })
+ }
+
+ }
+})
View
@@ -0,0 +1,62 @@
+$e.mixin($e, {
+ _loaded: {},
+ _codeWaiting:[],
+ _addressing: [],
+ _moduleToPath: function(module){
+ return $e.basePath + module.replace(".","/") + ".js";
+ },
+ codeReady: function(moduleName, func){
+ if(this._loaded[moduleName]){
+ func()
+ } else {
+ $e._codeWaiting[moduleName] = ($e._codeWaiting[moduleName]) ? $e._codeWaiting[moduleName] : [];
+ $e._codeWaiting[moduleName].push(func);
+ }
+ },
+ require: function(moduleName){
+ if(this._loaded[moduleName]){return;}
+ var path = $e._moduleToPath(moduleName) + "?rand="+(new Date()-1);
+ var sfunc = function(data){ return $e._handleScriptFetch(moduleName, data); };
+ this._makeRequest({ url: path, type:"get", async: false, context:this, success: sfunc, dataType: "text"});
+ },
+ _makeRequest: function(params){
+
+ xhr = new window.XMLHttpRequest();
+ xhr["onreadystatechange"] = function(req){
+ if (req["currentTarget"]["readyState"] === 4) {
+ params.success.apply(this, [req["currentTarget"].responseText])
+ }
+ };
+ xhr.open("GET",params.url);
+ xhr.send("");
+ },
+ _handleScriptFetch: function(moduleName, data){
+ // pull out provides and requires from commented space
+ var provides = data.match(/\/\/\*\*(.*)\*\*/g);
+ var requires = data.match(/\/\/\-\-(.*)\-\-/g);
+ if(provides && (provides.length > 0)){
+ // presume 1 provides line and clean off the commenting.
+ provides = provides[0].replace(/\/\//g,"").replace(/\*\*/g,"");
+ }
+ if(requires && (requires.length>0)){
+ var requiresSet = $e.collect(requires, function(index, item){
+ return item.replace(/\/\//g,"").replace(/\-\-/g,"");
+ });
+ jQuery.each(requiresSet, function(index, item){
+ if(!$e._addressing[item]){
+ $e._addressing[item] = true;
+ $e.require(item)
+ }
+ })
+ }
+ console.log("Executing "+moduleName)
+ jQuery.globalEval(data);
+ this._loaded[moduleName] = true;
+ if($e._codeWaiting[moduleName]){
+ jQuery.each($e._codeWaiting[moduleName],function(index, item){
+ item.apply(this,[]);
+ })
+ }
+
+ }
+})
View
@@ -1,40 +1,31 @@
-/* make sure we have a conf variable to work with */
-if(typeof($wConf) == "undefined" ){ window["$wConf"] = {}; }
-
/* if no firebug log to null */
if(typeof(console)=="undefined"){ window["console"] = { log:function(){}, error:function(){}, warn:function(){}}}
(function(){
- /* setup wConf */
- var found = null;
- $("script").each(function(index,item){ if(item.src.indexOf("extend") >0){ found = item}});
-
- if(found){
- $wConf.basePath = ($wConf.basePath) ? $wConf.basePath : found.src.replace("extend.js","");
- $wConf.templateBasePath = ($wConf.templateBasePath) ? $wConf.templateBasePath : ($wConf.basePath + "/widgets");
- $wConf.parseOnLoad = ($wConf.parseOnLoad) ? $wConf.parseOnLoad : (found.getAttribute("parseOnLoad")=="true");
- }
-
var subscriptions = [];
+
+ var found = srcBasePath = null;
+
+ $("script").each(function(index,item){ if(item.src.indexOf("extend") >0){ found = item}});
+ if(found!=null){ srcBasePath = found.src.replace("extend.js",""); }
+
$e = {
/* X,Y pair for all widgets to access current mouse position*/
mouse: {x:0, y:0},
- basePath: $wConf.basePath,
- templateBasePath: $wConf.templateBasePath,
- _requirePaths: [],
- require: function(){
- var paths = [];
- var bpath = this.basePath;
- jQuery.each(arguments,function(index, item){
- paths.push(bpath + item.replace(".","/") + ".js");
- });
- this._requirePaths = this._requirePaths.concat(paths);
- },
+ basePath: srcBasePath,
/* moves b content into a*/
mixin: function(a,b){
jQuery.extend(a,b);
return a
},
+ collect: function(array, func, scope){
+ if(!scope){ scope = window; }
+ var arr = [];
+ jQuery.each(array, function(index, item){
+ arr.push(func.apply(scope, [index, item]));
+ })
+ return arr;
+ },
extend: function(/*Object*/ constructor, /*Object...*/ props){
// summary:
// Adds all properties and methods of props to constructor's
@@ -46,7 +37,6 @@ if(typeof(console)=="undefined"){ window["console"] = { log:function(){}, error:
return constructor; // Object
},
-
_capitalise: function(word){
var letter = word.slice(0,1);
var remainder = word.slice(1,word.length);
@@ -408,4 +398,46 @@ $(document).ready(function(){
}
$e.trackMouse();
-});
+});
+
+(function(){
+ $e._requirePaths= [];
+ // $e.require=function(){
+ // var paths = [];
+ // var bpath = this.basePath;
+ // jQuery.each(arguments,function(index, item){
+ // paths.push(bpath + item.replace(".","/") + ".js");
+ // });
+ // this._requirePaths = this._requirePaths.concat(paths);
+ // }
+ $e._codeCache = {};
+ $e.require = function(moduleName){
+ var bpath = this.basePath;
+ var paths = $e.collect(arguments,function(index, item){
+ return (bpath + item.replace(".","/") + ".js");
+ });
+ jQuery.each(paths, function(index, item){
+ if($e._codeCache[moduleName]){
+ // module loaded
+ return true;
+ } else {
+ // jQuery.getScript(path, $e.hitch(this, function(data){ $e._codeCache[] }))
+ jQuery.ajax({
+ url: item,
+ type: "get",
+ dataType: "html",
+ async: false,
+ context:this,
+ complete: function( res, status ) {
+ // If successful, inject the HTML into all the matched elements
+ if ( status === "success" || status === "notmodified" ) {
+ $e._codeCache[moduleName] = true;
+ jQuery.globalEval(res.responseText);
+ }
+
+ }
+ });
+ }
+ })
+ }
+})();
Oops, something went wrong.

0 comments on commit d5ddac4

Please sign in to comment.