@@ -443,8 +443,7 @@ ClazzLoader.unwrapArray = function (arr) {
443443/*-# classQueue -> cq #-*/
444444ClazzLoader . classQueue = new Array ( ) ;
445445
446- /* private */
447- /*-# classpathMap -> cm #-*/
446+ /* protected */
448447ClazzLoader . classpathMap = new Object ( ) ;
449448
450449/* public */
@@ -495,14 +494,18 @@ ClazzLoader.packageClasspath = function (pkg, base, index) {
495494 pkg = pkg . substring ( 0 , pkg . length - 2 ) ;
496495 }
497496 map [ "@" + pkg ] = base ;
498- if ( index == true && window [ pkg + ".registered" ] != true && ! isPkgDeclared ) {
499- ClazzLoader . pkgRefCount ++ ;
500- ClazzLoader . loadClass ( pkg + ".package" , function ( ) {
497+ if ( index == true && ! isPkgDeclared ) {
498+ if ( window [ pkg + ".registered" ] != true ) {
499+ ClazzLoader . pkgRefCount ++ ;
500+ ClazzLoader . loadClass ( pkg + ".package" , function ( ) {
501501 ClazzLoader . pkgRefCount -- ;
502502 if ( ClazzLoader . pkgRefCount == 0 ) {
503503 ClazzLoader . runtimeLoaded ( ) ;
504504 }
505505 } , true ) ;
506+ } else if ( window [ pkg + ".package.callback" ] != null ) {
507+ window [ pkg + ".package.callback" ] ( ) ;
508+ }
506509 }
507510} ;
508511
@@ -633,7 +636,8 @@ ClazzLoader.getClasspathFor = function (clazz, forRoot, ext) {
633636 }
634637 }
635638 }
636- } else {
639+ }
640+ if ( base == null ) {
637641 /*
638642 path = ClazzLoader.classpathMap["@" + clazz]; // package
639643 if (path != null) {
@@ -930,10 +934,10 @@ ClazzLoader.ieToLoadScriptAgain = function (path, local) {
930934 } ;
931935 // consider 30 seconds available after failing!
932936 /*
933- * Set 1s waiting in local file system. Is it 1s enough?
934- * What about big *.z.js need more than 1s to initialize?
937+ * Set 3s waiting in local file system. Is it 3s enough?
938+ * What about big *.z.js need more than 3s to initialize?
935939 */
936- var waitingTime = ( local ? 500 : 15000 ) ; // 0.5s : 15s
940+ var waitingTime = ( local ? 3000 : 30000 ) ; // 3s : 30s
937941 //alert ("waiting:" + waitingTime + " . " + path);
938942 return window . setTimeout ( fun , waitingTime ) ;
939943} ;
@@ -948,6 +952,14 @@ ClazzLoader.w3cFailedLoadingTest = function (script) {
948952 } , 500 ) ; // 0.5s for loading a local file is considered enough long
949953} ;
950954
955+ /* private */
956+ /*-# lazilyReloadScript -> lRlS #-*/
957+ ClazzLoader . lazilyReloadScript = function ( path ) {
958+ return window . setTimeout ( function ( ) {
959+ ClazzLoader . loadScript ( path ) ;
960+ } , 100 ) ; // 0.1s for script reloading interval
961+ } ;
962+
951963/* private */
952964/*-# generatingW3CScriptOnCallback -> gWSC #-*/
953965ClazzLoader . generatingW3CScriptOnCallback = function ( path , forError ) {
@@ -977,7 +989,7 @@ ClazzLoader.generatingW3CScriptOnCallback = function (path, forError) {
977989 ClazzLoader . innerLoadedScripts [ this . src ] = false ;
978990 }
979991 ClazzLoader . loadedScripts [ path ] = false ;
980- ClazzLoader . loadScript ( path ) ;
992+ ClazzLoader . lazilyReloadScript ( path ) ;
981993 ClazzLoader . removeScriptNode ( this ) ;
982994 return ;
983995 } else {
@@ -997,14 +1009,11 @@ ClazzLoader.generatingW3CScriptOnCallback = function (path, forError) {
9971009/* private */
9981010/*-# generatingIEScriptOnCallback -> gISC #-*/
9991011ClazzLoader . generatingIEScriptOnCallback = function ( path ) {
1000- return function ( ) {
1012+ var f = function ( ) {
10011013 var fhs = ClazzLoader . failedHandles ;
10021014 var fss = ClazzLoader . failedScripts ;
10031015 var state = "" + this . readyState ;
10041016
1005-
1006-
1007-
10081017 var local = state == "loading"
10091018 && ( this . src . indexOf ( "file:" ) == 0
10101019 || ( window . location . protocol == "file:"
@@ -1030,6 +1039,9 @@ ClazzLoader.generatingIEScriptOnCallback = function (path) {
10301039 if ( fss [ path ] == 1 ) { // above function will be executed?!
10311040 return ;
10321041 }
1042+ if ( state == "loading" && ! local ) {
1043+ return ; // continue to load script
1044+ }
10331045 }
10341046 if ( fhs [ path ] != null ) {
10351047 window . clearTimeout ( fhs [ path ] ) ;
@@ -1047,7 +1059,7 @@ ClazzLoader.generatingIEScriptOnCallback = function (path) {
10471059 fss [ path ] = 1 ;
10481060 // log ("reloading ... " + path);
10491061 ClazzLoader . loadedScripts [ path ] = false ;
1050- ClazzLoader . loadScript ( path ) ;
1062+ ClazzLoader . lazilyReloadScript ( path ) ;
10511063 ClazzLoader . removeScriptNode ( this ) ;
10521064 return ;
10531065 } else {
@@ -1062,7 +1074,25 @@ ClazzLoader.generatingIEScriptOnCallback = function (path) {
10621074 this . onreadystatechange = null ;
10631075 ClazzLoader . loadingNextByPath ( path ) ;
10641076 ClazzLoader . removeScriptNode ( this ) ;
1077+
1078+ /*
1079+ * In case this script loading takes too long, another script will be
1080+ * created to take another try. So remove other script nodes to avoid
1081+ * duplicate class loading and initializing.
1082+ */
1083+ var scripts = document . getElementsByTagName ( "SCRIPT" ) ;
1084+ if ( scripts != null && scripts . length > 0 ) {
1085+ for ( var i = 0 ; i < scripts . length ; i ++ ) {
1086+ var s = scripts [ i ] ;
1087+ if ( s . readyState == "loading" && s . onreadystatechange != null
1088+ && s . onreadystatechange . path == path && s !== this ) {
1089+ s . parentNode . removeChild ( s ) ;
1090+ }
1091+ }
1092+ }
10651093 } ;
1094+ f . path = path ;
1095+ return f ;
10661096} ;
10671097
10681098/*
@@ -1348,7 +1378,7 @@ ClazzLoader.tryToLoadNext = function (file) {
13481378 }
13491379 }
13501380 /*
1351- * Maybe in #optinalLoaded inside above ClazzLoader#updateNode calls,
1381+ * Maybe in #optionalsLoaded inside above ClazzLoader#updateNode calls,
13521382 * ClazzLoader.keepOnLoading is set false (Already loaded the wanted
13531383 * classes), so here check to stop.
13541384 */
@@ -1376,6 +1406,25 @@ ClazzLoader.tryToLoadNext = function (file) {
13761406 n = cq [ 0 ] ; // popup class from the queue
13771407 //alert ("load from queue");
13781408 //alert (cq.length + ":" + cq);
1409+ /*
1410+ var offset = 1;
1411+ for (var i = 0; i < cq.length - offset; i++) {
1412+ var next = cq[i + offset];
1413+ while (next.status == ClazzNode.STATUS_OPTIONALS_LOADED) {
1414+ offset++;
1415+ if (i + offset < cq.length) {
1416+ next = cq[i + offset];
1417+ } else {
1418+ next = null;
1419+ break;
1420+ }
1421+ }
1422+ if (next != null) {
1423+ cq[i] = next;
1424+ }
1425+ }
1426+ cq.length -= offset;
1427+ //*/
13791428 for ( var i = 0 ; i < cq . length - 1 ; i ++ ) {
13801429 cq [ i ] = cq [ i + 1 ] ;
13811430 }
@@ -2336,17 +2385,27 @@ ClazzLoader.loadClass = function (name, optionalsLoaded, forced, async) {
23362385 && ! ClazzLoader . isClassExcluded ( name ) ) {
23372386 var path = ClazzLoader . getClasspathFor ( name /*, true*/ ) ;
23382387 var existed = ClazzLoader . loadedScripts [ path ] ;
2388+ var existedItem = null ;
23392389 var qq = ClazzLoader . classQueue ;
23402390 if ( ! existed ) {
23412391 for ( var i = qq . length - 1 ; i >= 0 ; i -- ) {
23422392 if ( qq [ i ] . path == path || qq [ i ] . name == name ) {
23432393 existed = true ;
2394+ existedItem = qq [ i ] ;
23442395 }
23452396 }
23462397 }
2398+ var n = null ;
2399+ var checked = false ;
2400+ if ( existed && optionalsLoaded != null ) {
2401+ n = ClazzLoader . findClass ( name ) ;
2402+ checked = true ;
2403+ if ( n == null ) {
2404+ existed = false ; // another class in the same *.z.js path
2405+ }
2406+ }
23472407 if ( ! existed ) {
2348- var n = null ;
2349- if ( Clazz . unloadedClasses [ name ] != null ) {
2408+ if ( Clazz . unloadedClasses [ name ] != null && ! checked ) {
23502409 n = ClazzLoader . findClass ( name ) ;
23512410 }
23522411 if ( n == null ) {
@@ -2371,8 +2430,8 @@ ClazzLoader.loadClass = function (name, optionalsLoaded, forced, async) {
23712430 // push class to queue
23722431 var inserted = false ;
23732432 for ( var i = qq . length - 1 ; i >= 0 ; i -- ) {
2374- var name = qq [ i ] . name ;
2375- if ( name . lastIndexOf ( "package.js" ) == name . length - 10 ) {
2433+ var itemName = qq [ i ] . name ;
2434+ if ( itemName . lastIndexOf ( "package.js" ) == itemName . length - 10 ) {
23762435 qq [ i + 1 ] = n ;
23772436 inserted = true ;
23782437 break ;
@@ -2393,13 +2452,17 @@ ClazzLoader.loadClass = function (name, optionalsLoaded, forced, async) {
23932452 ClazzLoader . isLoadingEntryClass = true ;
23942453 }
23952454 ClazzLoader . addChildClassNode ( ClazzLoader . clazzTreeRoot , n , 1 ) ;
2396- ClazzLoader . loadScript ( n . path ) ;
2455+ if ( ! ClazzLoader . loadedScripts [ n . path ] ) {
2456+ ClazzLoader . loadScript ( n . path ) ;
2457+ }
23972458 if ( optionalsLoaded != null ) {
23982459 ClazzLoader . isLoadingEntryClass = bakEntryClassLoading ;
23992460 }
24002461 }
24012462 } else if ( optionalsLoaded != null ) {
2402- var n = ClazzLoader . findClass ( name ) ;
2463+ if ( ! checked ) {
2464+ n = ClazzLoader . findClass ( name ) ;
2465+ }
24032466 if ( n != null ) {
24042467 if ( n . optionalsLoaded == null ) {
24052468 n . optionalsLoaded = optionalsLoaded ;
@@ -2411,7 +2474,18 @@ ClazzLoader.loadClass = function (name, optionalsLoaded, forced, async) {
24112474 } ;
24122475 } ) ( n . optionalsLoaded , optionalsLoaded ) ;
24132476 }
2414- }
2477+ } else if ( existedItem != null ) {
2478+ if ( existedItem [ 1 ] == null ) {
2479+ existedItem [ 1 ] = optionalsLoaded ;
2480+ } else if ( optionalsLoaded != existedItem [ 1 ] ) {
2481+ existedItem [ 1 ] = ( function ( oF , nF ) {
2482+ return function ( ) {
2483+ oF ( ) ;
2484+ nF ( ) ;
2485+ } ;
2486+ } ) ( existedItem [ 1 ] , optionalsLoaded ) ;
2487+ }
2488+ } // else already be checked, should not reach here
24152489 }
24162490 } else if ( optionalsLoaded != null && ClazzLoader . isClassDefined ( name ) ) {
24172491 var nn = ClazzLoader . findClass ( name ) ;
0 commit comments