Skip to content

Commit 3e96449

Browse files
author
jossonsmith
committed
Support *.js loading in bad network, which lots of 404 and timeout.
Firefox and IE 6 is fine in lots of tests. But Opera 9 seems fail to load *.js in bad network.
1 parent 802cb14 commit 3e96449

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

sources/net.sf.j2s.java.core/src/java/lang/Class.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,8 @@ Clazz.innerFunctions = {
15911591
/*-# decorateFunction -> dF #-*/
15921592
Clazz.decorateFunction = function (clazzFun, prefix, name) {
15931593
if (window["ClazzLoader"] != null) {
1594-
ClazzLoader.isInnerLoaded = true;
1594+
//alert ("decorate " + name);
1595+
ClazzLoader.checkInteractive ();
15951596
}
15961597
var qName = null;
15971598
if (prefix == null) {

sources/net.sf.j2s.java.core/src/java/lang/ClassLoader.js

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ ClazzLoader.jarClasspath = function (jar, clazzes) {
388388

389389
/* public */
390390
ClazzLoader.registerPackages = function (prefix, pkgs) {
391-
ClazzLoader.isInnerLoaded = true;
391+
//alert ("package " + pkgs);
392+
ClazzLoader.checkInteractive ();
392393
var base = ClazzLoader.getClasspathFor (prefix + ".*", true);
393394
for (var i = 0; i < pkgs.length; i++) {
394395
if (window["Clazz"] != null) {
@@ -646,7 +647,7 @@ ClazzLoader.failedScripts = new Object ();
646647
ClazzLoader.failedHandles = new Object ();
647648

648649
/* protected */
649-
ClazzLoader.takeAnotherTry = false;
650+
ClazzLoader.takeAnotherTry = true;
650651

651652
/**
652653
* Load *.js by adding script elements into head. Hook the onload event to
@@ -759,13 +760,15 @@ ClazzLoader.loadScript = function (file) {
759760
}
760761
this.onload = null;
761762
var path = arguments.callee.path;
762-
if (!ClazzLoader.isInnerLoaded
763+
if (!ClazzLoader.innerLoadedScripts[this.src]
763764
&& navigator.userAgent.indexOf("Opera") >= 0) {
764765
// Opera will not take another try.
765766
var fss = ClazzLoader.failedScripts;
766767
if (fss[path] == null && ClazzLoader.takeAnotherTry) {
767768
// silently take another try for bad network
769+
// alert ("re loading " + path + " ... ");
768770
fss[path] = 1;
771+
ClazzLoader.innerLoadedScripts[this.src] = false;
769772
ClazzLoader.loadedScripts[path] = false;
770773
ClazzLoader.loadScript (path);
771774
return;
@@ -796,6 +799,7 @@ ClazzLoader.loadScript = function (file) {
796799
var fss = ClazzLoader.failedScripts;
797800
if (fss[path] == null && ClazzLoader.takeAnotherTry) {
798801
// silently take another try for bad network
802+
// alert ("re loading " + path + " ...");
799803
fss[path] = 1;
800804
ClazzLoader.loadedScripts[path] = false;
801805
ClazzLoader.loadScript (path);
@@ -813,7 +817,11 @@ ClazzLoader.loadScript = function (file) {
813817
}
814818
};
815819
script.onerror.path = file;
820+
if (navigator.userAgent.indexOf("Opera") >= 0) {
821+
ClazzLoader.needOnloadCheck = true;
822+
}
816823
} else { // IE
824+
ClazzLoader.needOnloadCheck = true;
817825
script.defer = true;
818826
script.onreadystatechange = function () {
819827
var ee = arguments.callee;
@@ -876,7 +884,8 @@ ClazzLoader.loadScript = function (file) {
876884
window.clearTimeout (fhs[path]);
877885
fhs[path] = null;
878886
}
879-
if ((local || state == "loaded") && !ClazzLoader.isInnerLoaded) {
887+
if ((local || state == "loaded")
888+
&& !ClazzLoader.innerLoadedScripts[this.src]) {
880889
if (!local && (fss[path] == null || fss[path] == 0)
881890
&& ClazzLoader.takeAnotherTry) {
882891
// failed! count down
@@ -885,7 +894,7 @@ ClazzLoader.loadScript = function (file) {
885894
}
886895
// silently take another try for bad network
887896
fss[path] = 1;
888-
//log ("reloading ... " + path);
897+
// log ("reloading ... " + path);
889898
ClazzLoader.loadedScripts[path] = false;
890899
ClazzLoader.loadScript (path);
891900
return;
@@ -909,7 +918,6 @@ ClazzLoader.loadScript = function (file) {
909918
};
910919
script.onreadystatechange.path = file;
911920
}
912-
ClazzLoader.isInnerLoaded = false;
913921
ClazzLoader.inLoadingThreads++;
914922
//alert("threads:"+ClazzLoader.inLoadingThreads);
915923
// Add script DOM element to document tree
@@ -1124,6 +1132,7 @@ ClazzLoader.checkOptionalCycle = function (node) {
11241132
}
11251133
ts[i].parents = new Array ();
11261134
if (ts[i].optionalsLoaded != null) {
1135+
//window.setTimeout (ts[i].optionalsLoaded, 25);
11271136
ts[i].optionalsLoaded ();
11281137
ts[i].optionalsLoaded = null;
11291138
}
@@ -1206,6 +1215,7 @@ ClazzLoader.updateNode = function (node) {
12061215
}
12071216
for (var j = 0; j < nns.length; j++) {
12081217
if (nns[j].optionalsLoaded != null) {
1218+
//window.setTimeout (nns[j].optionalsLoaded, 25);
12091219
nns[j].optionalsLoaded ();
12101220
nns[j].optionalsLoaded = null;
12111221
}
@@ -1298,6 +1308,7 @@ ClazzLoader.updateNode = function (node) {
12981308
node.status = level;
12991309
ClazzLoader.scriptCompleted (node.path);
13001310
if (node.optionalsLoaded != null) {
1311+
//window.setTimeout (node.optionalsLoaded, 25);
13011312
node.optionalsLoaded ();
13021313
node.optionalsLoaded = null;
13031314
if (!ClazzLoader.keepOnLoading) {
@@ -1318,6 +1329,7 @@ ClazzLoader.updateNode = function (node) {
13181329
nn.declaration = null;
13191330
ClazzLoader.scriptCompleted (nn.path);
13201331
if (nn.optionalsLoaded != null) {
1332+
//window.setTimeout (nn.optionalsLoaded, 25);
13211333
nn.optionalsLoaded ();
13221334
nn.optionalsLoaded = null;
13231335
if (!ClazzLoader.keepOnLoading) {
@@ -1449,20 +1461,67 @@ ClazzLoader.searchClassArray = function (arr, rnd, status) {
14491461
};
14501462

14511463
/**
1452-
* This variable is used to mark that *.js is correctly loaded.
1464+
* This map variable is used to mark that *.js is correctly loaded.
14531465
* In IE, ClazzLoader has defects to detect whether a *.js is correctly
14541466
* loaded or not, so inner loading mark is used for detecting.
14551467
*/
14561468
/* private */
1457-
ClazzLoader.isInnerLoaded = false;
1469+
/*-# innerLoadedScripts -> ilss #-*/
1470+
ClazzLoader.innerLoadedScripts = new Object ();
1471+
1472+
/**
1473+
* This variable is used to keep the latest interactive SCRIPT element.
1474+
*/
1475+
/* private */
1476+
/*-# interactiveScript -> itst #-*/
1477+
ClazzLoader.interactiveScript = null;
1478+
1479+
/**
1480+
* IE and Firefox/Mozilla are different in using <SCRIPT> tag to load *.js.
1481+
*/
1482+
/* private */
1483+
ClazzLoader.needOnloadCheck = false;
1484+
1485+
/**
1486+
* Check the interactive status of SCRIPT elements to determine whether a
1487+
* *.js file is correctly loaded or not.
1488+
*
1489+
* Only make senses for IE.
1490+
*/
1491+
/* protected */
1492+
ClazzLoader.checkInteractive = function () {
1493+
//alert ("checking...");
1494+
if (!ClazzLoader.needOnloadCheck) {
1495+
return;
1496+
}
1497+
var is = ClazzLoader.interactiveScript;
1498+
if (is != null && is.readyState == "interactive") { // IE
1499+
return;
1500+
}
1501+
ClazzLoader.interactiveScript = null;
1502+
var ss = document.getElementsByTagName ("SCRIPT");
1503+
for (var i = 0; i < ss.length; i++) {
1504+
if (ss[i].readyState == "interactive"
1505+
&& ss[i].onreadystatechange != null) { // IE
1506+
ClazzLoader.interactiveScript = ss[i];
1507+
ClazzLoader.innerLoadedScripts[ss[i].src] = true;
1508+
} else if (navigator.userAgent.indexOf("Opera") >= 0) { // Opera
1509+
if (ss[i].readyState == "loaded"
1510+
&& ss[i].src != null && ss[i].src.length != 0) {
1511+
ClazzLoader.innerLoadedScripts[ss[i].src] = true;
1512+
}
1513+
}
1514+
}
1515+
}
14581516

14591517
/**
14601518
* This method will be called in almost every *.js generated by Java2Script
14611519
* compiler.
14621520
*/
14631521
/* protected */
14641522
ClazzLoader.load = function (musts, clazz, optionals, declaration) {
1465-
ClazzLoader.isInnerLoaded = true;
1523+
//alert ("Loading " + clazz + " ...");
1524+
ClazzLoader.checkInteractive ();
14661525
if (clazz instanceof Array) {
14671526
ClazzLoader.unwrapArray (clazz);
14681527
for (var i = 0; i < clazz.length; i++) {

0 commit comments

Comments
 (0)