Skip to content

Commit b6e7765

Browse files
author
zhourenjian
committed
Remove loaded *.js so that "Refresh" action won't fetch a new version of *.js
1 parent 83f4e3f commit b6e7765

File tree

1 file changed

+85
-17
lines changed

1 file changed

+85
-17
lines changed

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

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,37 @@ ClazzLoader.failedHandles = new Object ();
754754
/* protected */
755755
ClazzLoader.takeAnotherTry = true;
756756

757+
/*
758+
* Dynamically SCRIPT elements are removed after they are parsed into memory.
759+
* And removed *.js may not be fetched again by "Refresh" action. And it will
760+
* save loading time for Java2Script applications.
761+
*
762+
* This may disturb debugging tools such as Firebug. Setting
763+
* window["j2s.script.debugging"] = true;
764+
* will ignore removing SCRIPT elements requests.
765+
*/
766+
/* private */
767+
/*-# removeScriptNode -> RsN #-*/
768+
ClazzLoader.removeScriptNode = function (n) {
769+
// lazily remove script nodes.
770+
window.setTimeout ((function (node) {
771+
return function () {
772+
if (!window["j2s.script.debugging"] && node.readyState != "interactive") {
773+
try {
774+
if (node.parentNode != null) {
775+
node.parentNode.removeChild (node);
776+
}
777+
} catch (e) { }
778+
}
779+
};
780+
}) (n), 1);
781+
};
782+
783+
/*
784+
* There is another thread trying to remove j2slib.z.js or similar SCRIPT.
785+
* See the end of this file.
786+
*/
787+
757788
/**
758789
* Load *.js by adding script elements into head. Hook the onload event to
759790
* load the next class in dependency tree.
@@ -887,6 +918,8 @@ ClazzLoader.loadScript = function (file) {
887918
ClazzLoader.innerLoadedScripts[this.src] = false;
888919
ClazzLoader.loadedScripts[path] = false;
889920
ClazzLoader.loadScript (path);
921+
922+
ClazzLoader.removeScriptNode (this);
890923
return;
891924
} else {
892925
alert ("[Java2Script] Error in loading " + path + "!");
@@ -901,6 +934,8 @@ ClazzLoader.loadScript = function (file) {
901934
} else {
902935
ClazzLoader.tryToLoadNext (path);
903936
}
937+
938+
ClazzLoader.removeScriptNode (this);
904939
};
905940
script.onload.path = file;
906941
/*
@@ -919,6 +954,8 @@ ClazzLoader.loadScript = function (file) {
919954
fss[path] = 1;
920955
ClazzLoader.loadedScripts[path] = false;
921956
ClazzLoader.loadScript (path);
957+
958+
ClazzLoader.removeScriptNode (this);
922959
return;
923960
} else {
924961
alert ("[Java2Script] Error in loading " + path + "!");
@@ -931,6 +968,8 @@ ClazzLoader.loadScript = function (file) {
931968
} else {
932969
ClazzLoader.tryToLoadNext (path);
933970
}
971+
972+
ClazzLoader.removeScriptNode (this);
934973
};
935974
script.onerror.path = file;
936975
if (navigator.userAgent.indexOf("Opera") >= 0) {
@@ -1013,6 +1052,8 @@ ClazzLoader.loadScript = function (file) {
10131052
// log ("reloading ... " + path);
10141053
ClazzLoader.loadedScripts[path] = false;
10151054
ClazzLoader.loadScript (path);
1055+
1056+
ClazzLoader.removeScriptNode (this);
10161057
return;
10171058
} else {
10181059
alert ("[Java2Script] Error in loading " + path + "!");
@@ -1031,6 +1072,8 @@ ClazzLoader.loadScript = function (file) {
10311072
} else {
10321073
ClazzLoader.tryToLoadNext (path);
10331074
}
1075+
1076+
ClazzLoader.removeScriptNode (this);
10341077
};
10351078
script.onreadystatechange.path = file;
10361079
}
@@ -1395,7 +1438,7 @@ $_L(["$wt.widgets.Widget","$wt.graphics.Drawable"],"$wt.widgets.Control",
13951438
if (ClazzLoader.isClassDefined (n.name)) {
13961439
var nns = new Array (); // for optional loaded events!
13971440
n.status = ClazzNode.STATUS_OPTIONALS_LOADED;
1398-
ClazzLoader.updateNode (n);
1441+
ClazzLoader.updateNode (n); // musts may be changed
13991442
/*
14001443
* For those classes within one *.js file, update
14011444
* them synchronously.
@@ -1429,26 +1472,20 @@ $_L(["$wt.widgets.Widget","$wt.graphics.Drawable"],"$wt.widgets.Control",
14291472
}
14301473
} else { // why not break? -Zhou Renjian @ Nov 28, 2006
14311474
if (n.status == ClazzNode.STATUS_CONTENT_LOADED) {
1432-
// may be lazy loading script!
1433-
/*
1434-
window.setTimeout ((function (node) {
1435-
return function () {
1436-
ClazzLoader.updateNode (node);
1437-
};
1438-
}) (n), 1);
1439-
// */
1440-
ClazzLoader.updateNode (n); // fix above strange bug
1441-
if (node.musts.length != mustLength) {
1442-
// length changed!
1443-
mustLength = node.musts.length;
1444-
i = mustLength; // -1
1445-
isMustsOK = true;
1446-
}
1475+
// lazy loading script doesn't work! - 2/26/2007
1476+
ClazzLoader.updateNode (n); // musts may be changed
14471477
}
14481478
if (n.status < ClazzNode.STATUS_DECLARED) {
14491479
isMustsOK = false;
14501480
}
14511481
}
1482+
// fix the above strange bug
1483+
if (node.musts.length != mustLength) {
1484+
// length changed!
1485+
mustLength = node.musts.length;
1486+
i = mustLength; // -1
1487+
isMustsOK = true;
1488+
}
14521489
}
14531490
}
14541491
}
@@ -2023,6 +2060,10 @@ ClazzLoader.getJ2SLibBase = function () {
20232060
if (idx != -1) {
20242061
return src.substring (0, idx);
20252062
}
2063+
idx = src.indexOf ("j2slib.core.js"); // consider it as key string!
2064+
if (idx != -1) {
2065+
return src.substring (0, idx);
2066+
}
20262067
var base = ClazzLoader.classpathMap["@java"];
20272068
if (base != null) {
20282069
return base;
@@ -2035,6 +2076,17 @@ ClazzLoader.getJ2SLibBase = function () {
20352076
return null;
20362077
};
20372078

2079+
/* private static */
2080+
/*-# J2SLibBase -> JLB #-*/
2081+
ClazzLoader.J2SLibBase = null;
2082+
/*-# fastGetJ2SLibBase -> fgLB #-*/
2083+
ClazzLoader.fastGetJ2SLibBase = function () {
2084+
if (ClazzLoader.J2SLibBase == null) {
2085+
ClazzLoader.J2SLibBase = ClazzLoader.getJ2SLibBase ();
2086+
}
2087+
return ClazzLoader.J2SLibBase;
2088+
};
2089+
20382090
/*
20392091
* Check whether given package's classpath is setup or not.
20402092
* Only "java" and "org.eclipse.swt" are accepted in argument.
@@ -2045,7 +2097,7 @@ ClazzLoader.assurePackageClasspath = function (pkg) {
20452097
var r = window[pkg + ".registered"];
20462098
if (r != false && r != true && ClazzLoader.classpathMap["@" + pkg] == null) {
20472099
window[pkg + ".registered"] = false;
2048-
var base = ClazzLoader.getJ2SLibBase ();
2100+
var base = ClazzLoader.fastGetJ2SLibBase ();
20492101
if (base == null) {
20502102
base = "http://archive.java2script.org/1.0.0/"; // only after 1.0.0
20512103
}
@@ -2315,5 +2367,21 @@ ClazzLoader.destroyClassNode = function (node) {
23152367
}
23162368
};
23172369

2370+
/*
2371+
* Remove j2slib.z.js, j2slib.core.js or Class/Ext/Loader/.js.
2372+
*/
2373+
window.setTimeout (function () {
2374+
var ss = document.getElementsByTagName ("SCRIPT");
2375+
for (var i = 0; i < ss.length; i++) {
2376+
var src = ss[i].src;
2377+
if ((src.indexOf ("j2slib.z.js") != -1)
2378+
|| (src.indexOf ("j2slib.core.js") != -1)) {
2379+
ClazzLoader.getJ2SLibBase (); // cached ...
2380+
ClazzLoader.removeScriptNode (ss[i]);
2381+
break;
2382+
}
2383+
}
2384+
}, 324); // 0.324 seconds is considered as enough before refresh action
2385+
23182386
ClassLoader = ClazzLoader;
23192387

0 commit comments

Comments
 (0)