Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine ZK-4485 (dynamic loading wpd) #3137

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions zk/src/main/java/org/zkoss/zk/ui/http/WpdExtendlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand Down Expand Up @@ -94,6 +95,7 @@ public class WpdExtendlet extends AbstractExtendlet<Object> {
private static final String SOURCE_MAP_DIVIDED_WPDS_NUMBER = "$zk$dividedWPDsNum";
private ConcurrentMap<String, List<Element>> _dividedWpds = new ConcurrentHashMap<>(1); // store xml node for later parsing (ex. zk1 -> <script> ...)
private ConcurrentMap<String, Integer> _dividedPackageCnt = new ConcurrentHashMap<>(1); // store package count for dependency (ex. zul.wgt -> 2)
private Set<String> _lastDynamicWpds = ConcurrentHashMap.newKeySet(1); // store last dynamic wpd for not setting loaded

public void init(ExtendletConfig config) {
init(config, new WpdLoader());
Expand Down Expand Up @@ -169,7 +171,7 @@ protected byte[] retrieve(HttpServletRequest request, HttpServletResponse respon
dividedElements = _dividedWpds.get(lastPart);
if (dividedElements == null) { // dynamic
try {
return processDynamicWpdWithSourceMapIfAny(request, response, path);
return processDynamicWpdWithSourceMapIfAny(request, response, pkgName, path);
} catch (Exception e) {
log.error("fail to process source for source map", e);
return new byte[]{};
Expand Down Expand Up @@ -489,13 +491,15 @@ private Object parse(RequestContext reqctx, InputStream is, String path) throws
}
if (depends != null) {
write(out, "});");
write(out, "zk.setLoaded('");
if (processingPartial && totalPartialCount > 1 && partialNum < totalPartialCount) {
write(out, name + partialNum);
} else {
write(out, name);
if (!processingPartial || !_lastDynamicWpds.contains(name + partialNum + ".wpd")) {
write(out, "zk.setLoaded('");
if (processingPartial && totalPartialCount > 1 && partialNum < totalPartialCount) {
write(out, name + partialNum);
} else {
write(out, name);
}
write(out, "',1);");
}
write(out, "',1);");
} else if (!processingPartial) {
write(out, "})();");
}
Expand Down Expand Up @@ -1023,10 +1027,11 @@ private String processWpdForSourcemapIfAny(List<String> paths, String originalPa
return name;
}

private byte[] processDynamicWpdWithSourceMapIfAny(HttpServletRequest request, HttpServletResponse response, String path) throws Exception {
private byte[] processDynamicWpdWithSourceMapIfAny(HttpServletRequest request, HttpServletResponse response, String pkgWpd, String path) throws Exception {
List<String> dividedPaths = splitSourceMapJsPathIfAny(path);
StringBuilder sb = new StringBuilder();
int index = 0;
String lastWpd = null;
for (String dividedPath : dividedPaths) {
String scriptVariableName = "script" + index++;
sb.append("var ").append(scriptVariableName).append("=document.createElement('script');");
Expand All @@ -1039,6 +1044,12 @@ private byte[] processDynamicWpdWithSourceMapIfAny(HttpServletRequest request, H
}
sb.append(scriptVariableName).append(".src='").append(url).append("';");
sb.append("\ndocument.getElementsByTagName('head')[0].appendChild(").append(scriptVariableName).append(");");
lastWpd = dividedPath.substring(dividedPath.lastIndexOf("/") + 1);
}
if (!Strings.isEmpty(lastWpd)) {
_lastDynamicWpds.add(lastWpd);
// mark loading
sb.append("\nzk.setLoaded('").append(pkgWpd.replace(".wpd", "")).append("',1);");
}
return sb.toString().getBytes();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class B90_ZK_4485Test extends WebDriverTestCase {
@Test
public void test() {
connect();
waitResponse();
click(jq("@button"));
waitResponse();
assertEquals("true", getZKLog());
Expand Down
Loading