From 6e8490e9a0827297be0a7ad502020e378216d1fe Mon Sep 17 00:00:00 2001 From: yuche Date: Tue, 8 Oct 2019 13:50:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(runtime):=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lerna.json | 3 ++- packages/taro-runtime/src/bom/document.ts | 9 ++++++-- .../taro-runtime/src/config/create_page.ts | 22 +++++++++---------- packages/taro-runtime/src/index.ts | 3 +-- packages/taro-runtime/src/render.ts | 11 ---------- 5 files changed, 20 insertions(+), 28 deletions(-) diff --git a/lerna.json b/lerna.json index 0f9e690e8a7c..fd27b32413a3 100644 --- a/lerna.json +++ b/lerna.json @@ -48,7 +48,8 @@ "packages/taroize", "packages/stylelint-taro-rn", "packages/stylelint-config-taro-rn", - "packages/css-to-react-native" + "packages/css-to-react-native", + "packages/runtime" ], "command": { "publish": { diff --git a/packages/taro-runtime/src/bom/document.ts b/packages/taro-runtime/src/bom/document.ts index ee2746550155..841f8ce4d495 100644 --- a/packages/taro-runtime/src/bom/document.ts +++ b/packages/taro-runtime/src/bom/document.ts @@ -5,6 +5,8 @@ import { TaroRootElement } from '../dom/root' import { eventSource } from 'src/dom/event' export class TaroDocument extends TaroElement { + private app = this.createElement('app') + public constructor () { super(NodeType.DOCUMENT_NODE, '#document') } @@ -20,8 +22,11 @@ export class TaroDocument extends TaroElement { return new TaroText(text) } - public getElementById (id: string) { - return eventSource.get(id) || null + public getElementById (id: string) { + if (id === 'app') { + return this.app + } + return eventSource.get(id) as T || null } } diff --git a/packages/taro-runtime/src/config/create_page.ts b/packages/taro-runtime/src/config/create_page.ts index 0ed02727b5ab..3fb96a16abe3 100644 --- a/packages/taro-runtime/src/config/create_page.ts +++ b/packages/taro-runtime/src/config/create_page.ts @@ -1,10 +1,9 @@ -import { eventSource, createEvent } from 'src/dom/event' -import { TaroElement } from 'src/dom/element' -import { isUndefined } from 'src/utils/is' +import { createEvent } from 'src/dom/event' import { Current } from 'src/current' import { document } from 'src/bom/document' import { TaroRootElement } from 'src/dom/root' import { ComponentClass, Component } from 'react' +import { MpInstance } from 'src/render' interface Props { children?: unknown; @@ -31,22 +30,21 @@ export function wrapReactPageComponent (h: Function, derivedIDfromCompiler: stri } export function createPageConfig (derivedIDfromCompiler: string) { - // 把 DOM 挂载在小程序实例上可能会有意料之外的错误 + // 把复杂的 JavaScript 对象挂载在小程序实例上可能会触发意料之外的错误 let page: TaroRootElement const config = { eh (event) { - const node = eventSource.get(event.currentTarget.id) as TaroElement - if (isUndefined(node)) { - return + const node = document.getElementById(event.currentTarget.id) + if (node !== null) { + node.dispatchEvent(createEvent(event)) } - node.dispatchEvent(createEvent(event)) }, - onLoad () { + onLoad (this: MpInstance) { Current.pages.add(derivedIDfromCompiler) const render = () => { - page = document.getElementById(derivedIDfromCompiler)! as TaroRootElement - if (isUndefined(page)) { + page = document.getElementById(derivedIDfromCompiler) as TaroRootElement + if (page === null) { return } @@ -59,7 +57,7 @@ export function createPageConfig (derivedIDfromCompiler: string) { }, onUnload () { Current.pages.delete(derivedIDfromCompiler) - page.ctx = null + Current.app!.forceUpdate(() => (page.ctx = null)) } } diff --git a/packages/taro-runtime/src/index.ts b/packages/taro-runtime/src/index.ts index 3ad2b5c3ebed..4dd19d87bf6d 100644 --- a/packages/taro-runtime/src/index.ts +++ b/packages/taro-runtime/src/index.ts @@ -1,8 +1,7 @@ export { createDocument, document } from './bom/document' export { window } from './bom/window' export { navigator } from './bom/navigator' -export { render } from './render' -export { eventSource, TaroEvent, createEvent } from './dom/event' +export { TaroEvent, createEvent } from './dom/event' export { createAppConfig } from './config/create_app' export { createPageConfig, wrapReactPageComponent } from './config/create_page' // eslint-disable-next-line @typescript-eslint/camelcase diff --git a/packages/taro-runtime/src/render.ts b/packages/taro-runtime/src/render.ts index a720186b13d1..432d720bb158 100644 --- a/packages/taro-runtime/src/render.ts +++ b/packages/taro-runtime/src/render.ts @@ -2,8 +2,6 @@ import { isText } from './utils/is' import { TaroElement } from './dom/element' import { TaroText } from './dom/text' import { TaroRootElement } from './dom/root' -import { Current } from './current' -import { document } from './bom/document' export interface MpInstance { dom: TaroRootElement; @@ -26,12 +24,3 @@ export function hydrate (node: TaroElement | TaroText) { uid: node.uid } } - -export function render (derivedIDfromCompiler: string, inst: MpInstance) { - Current.pages.add(derivedIDfromCompiler) - Current.activeId = derivedIDfromCompiler - const page = document.getElementById(Current.activeId)! as TaroRootElement - Current.root = page - page.ctx = inst - page.performUpdate() -}