Skip to content

Commit

Permalink
refactor(runtime): 调整导出的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Nov 19, 2019
1 parent 30f4d48 commit 6e8490e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 7 additions & 2 deletions packages/taro-runtime/src/bom/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -20,8 +22,11 @@ export class TaroDocument extends TaroElement {
return new TaroText(text)
}

public getElementById (id: string) {
return eventSource.get(id) || null
public getElementById<T extends TaroElement> (id: string) {
if (id === 'app') {
return this.app
}
return eventSource.get(id) as T || null
}
}

Expand Down
22 changes: 10 additions & 12 deletions packages/taro-runtime/src/config/create_page.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
}

Expand All @@ -59,7 +57,7 @@ export function createPageConfig (derivedIDfromCompiler: string) {
},
onUnload () {
Current.pages.delete(derivedIDfromCompiler)
page.ctx = null
Current.app!.forceUpdate(() => (page.ctx = null))
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/taro-runtime/src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 0 additions & 11 deletions packages/taro-runtime/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
}

0 comments on commit 6e8490e

Please sign in to comment.