From 00f073a5be4b0a071505fdccd4bc4459f3c8e21f Mon Sep 17 00:00:00 2001 From: yuche Date: Wed, 6 May 2020 16:44:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(runtime):=20=E6=99=AE=E9=80=9A=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=8F=AF=E4=BB=A5=E9=80=9A=E8=BF=87=20eventCenter=20?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E9=A1=B5=E9=9D=A2=E7=BB=84=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=20onReady=20=E4=BA=8B=E4=BB=B6=20close=20#5806,=20close=20#610?= =?UTF-8?q?6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-runtime/src/current.ts | 3 ++- packages/taro-runtime/src/dsl/common.ts | 31 ++++++++++++++++++------- packages/taro/types/taro.extend.d.ts | 3 ++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/taro-runtime/src/current.ts b/packages/taro-runtime/src/current.ts index bee5b2caddfc..a04e4c57045c 100644 --- a/packages/taro-runtime/src/current.ts +++ b/packages/taro-runtime/src/current.ts @@ -2,7 +2,8 @@ import { AppInstance, PageInstance } from './dsl/instance' interface Router { params: Record - path: string + path: string, + onReady: string } interface Current { diff --git a/packages/taro-runtime/src/dsl/common.ts b/packages/taro-runtime/src/dsl/common.ts index 04e5a5bcb969..68527c466765 100644 --- a/packages/taro-runtime/src/dsl/common.ts +++ b/packages/taro-runtime/src/dsl/common.ts @@ -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() @@ -80,6 +82,10 @@ export function getPath (id: string, options?: Record): string return path } +export function getOnReadyEventKey (path: string) { + return path + '.' + 'onReady' +} + export function createPageConfig (component: React.ComponentClass, pageName?: string, data?: Record) { const id = pageName ?? `taro_page_${pageId()}` // 小程序 Page 构造器是一个傲娇小公主,不能把复杂的对象挂载到参数上 @@ -87,15 +93,16 @@ export function createPageConfig (component: React.ComponentClass, pageName?: st 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(path) @@ -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 () { @@ -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 () { diff --git a/packages/taro/types/taro.extend.d.ts b/packages/taro/types/taro.extend.d.ts index b8355e3ef5b6..b21a5d305076 100644 --- a/packages/taro/types/taro.extend.d.ts +++ b/packages/taro/types/taro.extend.d.ts @@ -109,6 +109,7 @@ declare namespace Taro { const Current: { app: AppInstance | null, router: RouterInfo | null, - page: PageInstance | null + page: PageInstance | null, + onReady: string } }