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('Image', DoricImageNode);
registerViewNode('Scroller', DoricScrollerNode); registerViewNode('Scroller', DoricScrollerNode);
let __scriptId__ = 0; function getScriptId(contextId) {
function getScriptId() { return `__doric_script_${contextId}`;
return `script_${__scriptId__++}`;
} }
const originSetTimeout = window.setTimeout; const originSetTimeout = window.setTimeout;
const originClearTimeout = window.clearTimeout; const originClearTimeout = window.clearTimeout;
@ -4607,10 +4606,10 @@ return __module.exports;
function injectGlobalObject(name, value) { function injectGlobalObject(name, value) {
Reflect.set(window, name, value, window); Reflect.set(window, name, value, window);
} }
function loadJS(script) { function loadJS(contextId, script) {
const scriptElement = document.createElement('script'); const scriptElement = document.createElement('script');
scriptElement.text = script; scriptElement.text = script;
scriptElement.id = getScriptId(); scriptElement.id = getScriptId(contextId);
document.body.appendChild(scriptElement); document.body.appendChild(scriptElement);
} }
function packageModuleScript(name, content) { function packageModuleScript(name, content) {
@ -4650,7 +4649,7 @@ ${content}
return false; return false;
} }
else { else {
loadJS(packageModuleScript(moduleName, packageModuleScript(name, bundle))); loadJS(moduleName, packageModuleScript(moduleName, packageModuleScript(name, bundle)));
return true; return true;
} }
}); });
@ -4718,7 +4717,14 @@ ${content}
}); });
} }
function createContext(contextId, 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(); initDoric();
@ -4752,6 +4758,9 @@ ${content}
init(frame, extra) { init(frame, extra) {
this.invokeEntityMethod("__init__", frame, extra ? JSON.stringify(extra) : undefined); this.invokeEntityMethod("__init__", frame, extra ? JSON.stringify(extra) : undefined);
} }
teardown() {
destroyContext(this.contextId);
}
} }
class DoricElement extends HTMLElement { class DoricElement extends HTMLElement {
@ -4783,6 +4792,10 @@ ${content}
} }
attributeChangedCallback() { attributeChangedCallback() {
} }
onDestroy() {
var _a;
(_a = this.context) === null || _a === void 0 ? void 0 : _a.teardown();
}
load(content) { load(content) {
this.context = new DoricContext(content); this.context = new DoricContext(content);
const divElement = document.createElement('div'); const divElement = document.createElement('div');
@ -4825,6 +4838,7 @@ ${content}
const currentNode = this.currentNode; const currentNode = this.currentNode;
if (lastElement && currentNode) { if (lastElement && currentNode) {
this.replaceChild(lastElement, currentNode); this.replaceChild(lastElement, currentNode);
currentNode.onDestroy();
} }
else { else {
window.history.back(); 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 { jsObtainContext, jsCallEntityMethod } from 'doric/src/runtime/sandbox'
import { Panel } from 'doric' import { Panel } from 'doric'
import { DoricPlugin } from "./DoricPlugin" import { DoricPlugin } from "./DoricPlugin"
import { createContext } from "./DoricDriver" import { createContext, destroyContext } from "./DoricDriver"
import { DoricStackNode } from './shader/DoricStackNode' import { DoricStackNode } from './shader/DoricStackNode'
const doricContexts: Map<string, DoricContext> = new Map const doricContexts: Map<string, DoricContext> = new Map
@ -43,4 +43,7 @@ export class DoricContext {
this.invokeEntityMethod("__init__", frame, extra ? JSON.stringify(extra) : undefined) 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 { acquireJSBundle, acquirePlugin } from './DoricRegistry'
import { getDoricContext } from './DoricContext' import { getDoricContext } from './DoricContext'
import { DoricPlugin } from './DoricPlugin' import { DoricPlugin } from './DoricPlugin'
let __scriptId__ = 0 function getScriptId(contextId: string) {
function getScriptId() { return `__doric_script_${contextId}`
return `script_${__scriptId__++}`
} }
const originSetTimeout = window.setTimeout const originSetTimeout = window.setTimeout
@ -19,10 +18,10 @@ export function injectGlobalObject(name: string, value: any) {
Reflect.set(window, name, value, window) Reflect.set(window, name, value, window)
} }
export function loadJS(script: string) { export function loadJS(contextId: string, script: string) {
const scriptElement = document.createElement('script') const scriptElement = document.createElement('script')
scriptElement.text = script scriptElement.text = script
scriptElement.id = getScriptId() scriptElement.id = getScriptId(contextId)
document.body.appendChild(scriptElement) document.body.appendChild(scriptElement)
} }
@ -67,7 +66,7 @@ function initDoric() {
console.log(`Cannot require JS Bundle :${moduleName}`) console.log(`Cannot require JS Bundle :${moduleName}`)
return false return false
} else { } else {
loadJS(packageModuleScript(moduleName, packageModuleScript(name, bundle))) loadJS(moduleName, packageModuleScript(moduleName, packageModuleScript(name, bundle)))
return true return true
} }
}) })
@ -136,7 +135,13 @@ function initDoric() {
} }
export function createContext(contextId: string, content: string) { 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() initDoric()

View File

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

View File

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