h5:clean unused js context

This commit is contained in:
pengfei.zhou
2019-12-26 17:33:22 +08:00
committed by osborn
parent cd6024d7ac
commit b9a3d94cb5
6 changed files with 45 additions and 19 deletions

View File

@@ -4595,9 +4595,8 @@ return __module.exports;
registerViewNode('Image', DoricImageNode);
registerViewNode('Scroller', DoricScrollerNode);
let __scriptId__ = 0;
function getScriptId() {
return `script_${__scriptId__++}`;
function getScriptId(contextId) {
return `__doric_script_${contextId}`;
}
const originSetTimeout = window.setTimeout;
const originClearTimeout = window.clearTimeout;
@@ -4607,10 +4606,10 @@ return __module.exports;
function injectGlobalObject(name, value) {
Reflect.set(window, name, value, window);
}
function loadJS(script) {
function loadJS(contextId, script) {
const scriptElement = document.createElement('script');
scriptElement.text = script;
scriptElement.id = getScriptId();
scriptElement.id = getScriptId(contextId);
document.body.appendChild(scriptElement);
}
function packageModuleScript(name, content) {
@@ -4650,7 +4649,7 @@ ${content}
return false;
}
else {
loadJS(packageModuleScript(moduleName, packageModuleScript(name, bundle)));
loadJS(moduleName, packageModuleScript(moduleName, packageModuleScript(name, bundle)));
return true;
}
});
@@ -4718,7 +4717,14 @@ ${content}
});
}
function createContext(contextId, content) {
loadJS(packageCreateContext(contextId, content));
loadJS(contextId, packageCreateContext(contextId, content));
}
function destroyContext(contextId) {
sandbox.jsReleaseContext(contextId);
const scriptElement = document.getElementById(getScriptId(contextId));
if (scriptElement) {
document.body.removeChild(scriptElement);
}
}
initDoric();
@@ -4752,6 +4758,9 @@ ${content}
init(frame, extra) {
this.invokeEntityMethod("__init__", frame, extra ? JSON.stringify(extra) : undefined);
}
teardown() {
destroyContext(this.contextId);
}
}
class DoricElement extends HTMLElement {
@@ -4783,6 +4792,10 @@ ${content}
}
attributeChangedCallback() {
}
onDestroy() {
var _a;
(_a = this.context) === null || _a === void 0 ? void 0 : _a.teardown();
}
load(content) {
this.context = new DoricContext(content);
const divElement = document.createElement('div');
@@ -4825,6 +4838,7 @@ ${content}
const currentNode = this.currentNode;
if (lastElement && currentNode) {
this.replaceChild(lastElement, currentNode);
currentNode.onDestroy();
}
else {
window.history.back();

File diff suppressed because one or more lines are too long