Skip to content

Commit

Permalink
feat(runtime): 普通组件可以通过 eventCenter 触发页面组件的 onReady 事件
Browse files Browse the repository at this point in the history
close #5806, close #6106
  • Loading branch information
yuche authored and Chen-jj committed May 7, 2020
1 parent a3d007f commit 00f073a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/taro-runtime/src/current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { AppInstance, PageInstance } from './dsl/instance'

interface Router {
params: Record<string, unknown>
path: string
path: string,
onReady: string
}

interface Current {
Expand Down
31 changes: 23 additions & 8 deletions packages/taro-runtime/src/dsl/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { incrementId } from '../utils'
import { perf } from '../perf'
import { PAGE_INIT } from '../constants'
import { isBrowser } from '../env'
import { eventCenter } from '../emitter/emitter'
import { raf } from '../bom/raf'

const instances = new Map<string, Instance>()

Expand Down Expand Up @@ -80,22 +82,27 @@ export function getPath (id: string, options?: Record<string, unknown>): string
return path
}

export function getOnReadyEventKey (path: string) {
return path + '.' + 'onReady'
}

export function createPageConfig (component: React.ComponentClass, pageName?: string, data?: Record<string, unknown>) {
const id = pageName ?? `taro_page_${pageId()}`
// 小程序 Page 构造器是一个傲娇小公主,不能把复杂的对象挂载到参数上
let pageElement: TaroRootElement | null = null

const config: PageInstance = {
onLoad (this: MpInstance, options, cb?: Function) {
Current.router = {
params: options,
path: addLeadingSlash(this.route || this.__route__)
}

perf.start(PAGE_INIT)

const path = getPath(id, options)

Current.router = {
params: options,
path: addLeadingSlash(this.route || this.__route__),
onReady: getOnReadyEventKey(path)
}

Current.app!.mount!(component, path, () => {
pageElement = document.getElementById<TaroRootElement>(path)

Expand All @@ -109,6 +116,11 @@ export function createPageConfig (component: React.ComponentClass, pageName?: st
},
onReady () {
const path = getPath(id, this.options)

raf(() => {
eventCenter.trigger(getOnReadyEventKey(path))
})

safeExecute(path, 'onReady')
},
onUnload () {
Expand All @@ -120,12 +132,15 @@ export function createPageConfig (component: React.ComponentClass, pageName?: st
})
},
onShow () {
Current.page = this as any
const path = getPath(id, this.options)

Current.router = {
params: this.options,
path: addLeadingSlash(this.route || this.__route__)
path: addLeadingSlash(this.route || this.__route__),
onReady: getOnReadyEventKey(path)
}
Current.page = this as any
const path = getPath(id, this.options)

safeExecute(path, 'onShow')
},
onHide () {
Expand Down
3 changes: 2 additions & 1 deletion packages/taro/types/taro.extend.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ declare namespace Taro {
const Current: {
app: AppInstance | null,
router: RouterInfo | null,
page: PageInstance | null
page: PageInstance | null,
onReady: string
}
}

0 comments on commit 00f073a

Please sign in to comment.