PageRenderTime 1005ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/JSI/src/main/org/xidea/jsi/require.js

http://jsi.googlecode.com/
JavaScript | 159 lines | 155 code | 1 blank | 3 comment | 27 complexity | e11c52656c20fb31d7b33b8ced55eaec MD5 | raw file
  1. var $export;
  2. var $JSI = function(){
  3. var exportMap = {}; //path=>exports//??define???????????script?????depengdenceMap????????????
  4. var cachedMap = {};//path=>[impl,{waiting path map}]
  5. var taskMap = {};//path=>[taskCount, [lazy task...]]
  6. function require(path){
  7. var entry = loaderMap[path];
  8. var cache = {};
  9. if(entry){
  10. return entry[0] || entry[1].call(this,function(path2){
  11. if(path2 in cache){
  12. return cache[path2];
  13. }
  14. return cache[path2] = require(normalizeURI(path2,path));
  15. },entry[0]={}),entry[0];
  16. }else{
  17. console.warn('script not defined:'+path);
  18. }
  19. }
  20. $export = function (path,target){
  21. var async = typeof target == 'function';
  22. var callback = async ?target : function(result){
  23. copy(result,target ||this);
  24. };
  25. if(typeof path == 'string'){
  26. _export(path,callback,async);
  27. }else{//list
  28. var i = 0;
  29. var end = path.length;
  30. var end2 = end;
  31. while(i<end){
  32. _export(path[i++],function(result){
  33. --end2 || callback(result);
  34. },async);
  35. }
  36. }
  37. }
  38. function _export(path,callback,async){
  39. if(path in exportMap){
  40. return callback(exportMap[path])
  41. }
  42. var index = async+1;
  43. var task = taskMap[path];
  44. var cached = cachedMap[path];
  45. if(!task){
  46. task = taskMap[path] = [];
  47. task[index] = []
  48. }
  49. task[index].push(callback);
  50. if(cached){
  51. for(var dep in cached[index]){
  52. return;//task ?? dependence ????????
  53. }
  54. onComplete(path,async);
  55. }else{
  56. load(path,async);
  57. }
  58. }
  59. function load(path,async){
  60. path = $JSI.realpath(path);
  61. if(async){
  62. var s = document.createElement('script');
  63. s.setAttribute('src',path);
  64. document.scripts[0].parentNode.appendElement(s);
  65. }else{
  66. document.write('<script src="'+path+'"><\/script>');
  67. }
  68. }
  69. function define(path,dependences,impl){
  70. var dependenceMap = {};
  71. var loader = loaderMap[path];
  72. var len = dependences.length;
  73. if(!loader){//js????????????????js???????????????????js???
  74. loader = loaderMap[path] = [{},impl,dependenceMap];//dependenceMap ??????????
  75. while(len--){
  76. var dep = normalizeURI(dependences[len],path);
  77. if(!loaderMap[dep]){
  78. var notifySet = notifyMap[dep];
  79. if(!notifySet){
  80. notifyMap[dep] =notifySet = {};
  81. }
  82. notifySet[path]=1;
  83. dependenceMap[dep] = 1;
  84. load(dep,path);
  85. }
  86. }
  87. onDefined(path)
  88. }
  89. }
  90. function onDefined(path){//??define ???????????????
  91. var notifySet = notifyMap[path];
  92. var dependenceMap = loaderMap[path][2];
  93. var dependenceCount=0;
  94. for(var p in dependenceMap){
  95. if(loaderMap[p]){
  96. delete dependenceMap[p];
  97. }else{
  98. dependenceCount++;
  99. }
  100. }
  101. outer:for(p in notifySet){
  102. var notifyDependenceMap = loaderMap[p][2];
  103. if(delete notifyDependenceMap[path]){//has and deleted
  104. if(dependenceCount){
  105. copy(dependenceMap,notifyDependenceMap)
  106. _moveNodify(dependenceMap,p)
  107. //add nodify
  108. }else{
  109. for(p in notifyDependenceMap){
  110. continue outer;
  111. }
  112. onComplete(p);
  113. }
  114. }
  115. }
  116. if(!dependenceCount){
  117. //notify
  118. onComplete(path);
  119. }
  120. }
  121. function _moveNodify(loadingMap,path){//??????notifySet??????????????
  122. for(var p in loadingMap){
  123. var notifySet = notifyMap[p];
  124. notifySet[path] = 1;
  125. }
  126. }
  127. function onComplete(path){
  128. var task = taskMap[path];
  129. if(task){
  130. var len = task.length;
  131. while(len--){
  132. task.pop().call(this,require(path))
  133. }
  134. }
  135. }
  136. //utils...
  137. function copy(src,dest){
  138. for(var p in src){
  139. dest[p] = src[p]
  140. }
  141. }
  142. function normalizeURI(url,base){
  143. var url = url.replace(/\\/g,'/');
  144. if(url.charAt(0) == '.'){
  145. url = base.replace(/[^\/]+$/,'')+url
  146. while(url != (url =url.replace( /[^\/]+\/\.\.\/|(\/)?\.\//,'$1')));
  147. }
  148. return url;
  149. }
  150. return {
  151. realpath:function(path){
  152. return '/scripts/'+path+'__define__.js';////scriptBase:/scripts/,
  153. },
  154. copy : copy,
  155. require : require,
  156. define : define // $JSI.define('path',['deps'],function(require,exports){...})
  157. }
  158. }({});