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

@@ -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()
}