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

View File

@ -1,7 +1,7 @@
import { jsObtainContext, jsCallEntityMethod } from 'doric/src/runtime/sandbox'
import { Panel } from 'doric'
import { DoricPlugin } from "./DoricPlugin"
import { createContext } from "./DoricDriver"
import { createContext, destroyContext } from "./DoricDriver"
import { DoricStackNode } from './shader/DoricStackNode'
const doricContexts: Map<string, DoricContext> = new Map
@ -43,4 +43,7 @@ export class DoricContext {
this.invokeEntityMethod("__init__", frame, extra ? JSON.stringify(extra) : undefined)
}
teardown() {
destroyContext(this.contextId)
}
}

View File

@ -1,11 +1,10 @@
import { jsCallResolve, jsCallReject, jsCallbackTimer, jsCallEntityMethod } from 'doric/src/runtime/sandbox'
import { jsCallResolve, jsCallReject, jsCallbackTimer, jsCallEntityMethod, jsReleaseContext } from 'doric/src/runtime/sandbox'
import { acquireJSBundle, acquirePlugin } from './DoricRegistry'
import { getDoricContext } from './DoricContext'
import { DoricPlugin } from './DoricPlugin'
let __scriptId__ = 0
function getScriptId() {
return `script_${__scriptId__++}`
function getScriptId(contextId: string) {
return `__doric_script_${contextId}`
}
const originSetTimeout = window.setTimeout
@ -19,10 +18,10 @@ export function injectGlobalObject(name: string, value: any) {
Reflect.set(window, name, value, window)
}
export function loadJS(script: string) {
export function loadJS(contextId: string, script: string) {
const scriptElement = document.createElement('script')
scriptElement.text = script
scriptElement.id = getScriptId()
scriptElement.id = getScriptId(contextId)
document.body.appendChild(scriptElement)
}
@ -67,7 +66,7 @@ function initDoric() {
console.log(`Cannot require JS Bundle :${moduleName}`)
return false
} else {
loadJS(packageModuleScript(moduleName, packageModuleScript(name, bundle)))
loadJS(moduleName, packageModuleScript(moduleName, packageModuleScript(name, bundle)))
return true
}
})
@ -136,7 +135,13 @@ function initDoric() {
}
export function createContext(contextId: string, content: string) {
loadJS(packageCreateContext(contextId, content))
loadJS(contextId, packageCreateContext(contextId, content))
}
export function destroyContext(contextId: string) {
jsReleaseContext(contextId)
const scriptElement = document.getElementById(getScriptId(contextId))
if (scriptElement) {
document.body.removeChild(scriptElement)
}
}
initDoric()

View File

@ -30,7 +30,6 @@ export class DoricElement extends HTMLElement {
}
disconnectedCallback() {
}
adoptedCallback() {
@ -41,6 +40,10 @@ export class DoricElement extends HTMLElement {
}
onDestroy() {
this.context?.teardown()
}
load(content: string) {
this.context = new DoricContext(content)
const divElement = document.createElement('div')

View File

@ -28,6 +28,7 @@ export class NavigationElement extends HTMLElement {
const currentNode = this.currentNode
if (lastElement && currentNode) {
this.replaceChild(lastElement, currentNode)
currentNode.onDestroy()
} else {
window.history.back()
}