Remove JavaScript Element when not needed

This commit is contained in:
pengfei.zhou 2021-11-09 11:39:53 +08:00 committed by osborn
parent ce892a31bf
commit 18b50be203
3 changed files with 27 additions and 3 deletions

View File

@ -287,11 +287,11 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
mDoricJSE.loadJS(String.format(DoricConstant.TEMPLATE_CONTEXT_DESTROY, contextId), "_Context://" + contextId);
}
private String packageContextScript(String contextId, String content) {
protected String packageContextScript(String contextId, String content) {
return String.format(DoricConstant.TEMPLATE_CONTEXT_CREATE, content, contextId, contextId);
}
private String packageModuleScript(String moduleName, String content) {
protected String packageModuleScript(String moduleName, String content) {
return String.format(DoricConstant.TEMPLATE_MODULE, moduleName, content);
}

View File

@ -15,7 +15,11 @@
*/
package pub.doric.engine;
import java.util.HashMap;
import java.util.Map;
import pub.doric.Doric;
import pub.doric.utils.DoricConstant;
/**
* @Description: This uses DoricWebShellJSExecutor directly
@ -23,8 +27,24 @@ import pub.doric.Doric;
* @CreateDate: 2021/11/9
*/
public class DoricWebShellJSEngine extends DoricJSEngine {
private final Map<String, String> scriptIdMap = new HashMap<>();
@Override
protected void initJSEngine() {
mDoricJSE = new DoricWebShellJSExecutor(Doric.application());
}
@Override
public void prepareContext(String contextId, String script, String source) {
String scriptId = mDoricJSE.loadJS(packageContextScript(contextId, script), "Context://" + source);
scriptIdMap.put(contextId, scriptId);
}
@Override
public void destroyContext(String contextId) {
String scriptId = mDoricJSE.loadJS(String.format(DoricConstant.TEMPLATE_CONTEXT_DESTROY, contextId), "_Context://" + contextId);
((DoricWebShellJSExecutor) mDoricJSE).removeScript(scriptId);
scriptId = scriptIdMap.get(contextId);
((DoricWebShellJSExecutor) mDoricJSE).removeScript(scriptId);
}
}

View File

@ -324,13 +324,17 @@ public class DoricWebShellJSExecutor implements IDoricJSE {
});
}
public void removeScript(String scriptId) {
execJS(String.format("javascript:removeScriptElement('%s')", scriptId));
}
@Override
public String loadJS(final String script, String source) {
String uniqueId = String.valueOf(scriptId.incrementAndGet());
String url = shellUrl + "script/" + uniqueId;
loadingScripts.put(url, script);
execJS(String.format("javascript:addScriptElement('%s','%s')", uniqueId, url));
return null;
return uniqueId;
}
@Override