From 18b50be20386508a7774935a866c598e4db7b4d6 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Tue, 9 Nov 2021 11:39:53 +0800 Subject: [PATCH] Remove JavaScript Element when not needed --- .../java/pub/doric/engine/DoricJSEngine.java | 4 ++-- .../doric/engine/DoricWebShellJSEngine.java | 20 +++++++++++++++++++ .../doric/engine/DoricWebShellJSExecutor.java | 6 +++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index 6ae1157b..2807807f 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -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); } diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSEngine.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSEngine.java index f8aaff61..4d7abe82 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSEngine.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSEngine.java @@ -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 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); + } } diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSExecutor.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSExecutor.java index 81de0b60..cd4bcabf 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSExecutor.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebShellJSExecutor.java @@ -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