From 55e5ff9405c0a62b0ca3b2fd4dba4cc36035edeb Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 16 Dec 2021 11:32:22 +0800 Subject: [PATCH] fix(types): lifecycle hooks (#3076) --- packages/uni-app/src/apiLifecycle.ts | 197 +++++++++++++++++++++++---- 1 file changed, 169 insertions(+), 28 deletions(-) diff --git a/packages/uni-app/src/apiLifecycle.ts b/packages/uni-app/src/apiLifecycle.ts index b163923867..b4cab4f707 100644 --- a/packages/uni-app/src/apiLifecycle.ts +++ b/packages/uni-app/src/apiLifecycle.ts @@ -30,6 +30,134 @@ import { UniLifecycleHooks, } from '@dcloudio/uni-shared' +interface ReferrerInfo { + appId: string + extraData?: any +} + +interface LaunchShowOption { + path: string + query: Record + scene: number + shareTicket: string + referrerInfo?: ReferrerInfo +} + +type LaunchOption = LaunchShowOption + +type onAppShowHook = (options: LaunchShowOption) => void +type onPageShowHook = () => void + +type onLaunchHook = (options: LaunchOption) => void +type onLoadHook = (query: Record) => void +type onErrorHook = (error: string) => void +interface ThemeChangeOption { + theme: 'dark' | 'light' +} +type onThemeChangeHook = (options: ThemeChangeOption) => void +interface PageNotFoundOption { + path: string + query: Record + isEntryPage: boolean +} +type onPageNotFoundHook = (options: PageNotFoundOption) => void + +interface UnhandledRejectionOption { + promise: Promise + reason: string +} +type onUnhandledRejectionHook = (options: UnhandledRejectionOption) => void + +interface ResizeOption { + size: { + windowWidth: number + windowHeight: number + } +} +type onResizeHook = (options: ResizeOption) => void +interface BackPressOption { + from: 'backbutton' | 'navigateBack' +} +type onBackPressHook = (options: BackPressOption) => boolean | void + +interface PageScrollOption { + scrollTop: number +} +type onPageScrollHook = (options: PageScrollOption) => void + +interface TabItemTapOption { + index: string + pagePath: string + text: string +} +type onTabItemTapHook = (options: TabItemTapOption) => void + +interface CustomShareTimeline { + title?: string + query?: string + imageUrl?: string +} + +type onShareTimelineHook = () => CustomShareTimeline | void + +interface AddToFavoritesOption { + webviewUrl?: string +} + +interface AddToFavoritesContent { + title?: string + imageUrl?: string + query?: string +} + +type onAddToFavoritesHook = ( + options: AddToFavoritesOption +) => AddToFavoritesContent | void + +interface ShareAppMessageOption { + from: 'button' | 'menu' | string + target: any + webViewUrl?: string +} + +interface CustomShareAppMessage { + title?: string + path?: string + imageUrl?: string +} + +type onShareAppMessageHook = ( + options: ShareAppMessageOption +) => CustomShareAppMessage | void + +interface NavigationBarButtonTapOption { + index: number +} + +type onNavigationBarButtonTapHook = ( + options: NavigationBarButtonTapOption +) => void +interface NavigationBarSearchInputChangedOption { + text: string +} +type onNavigationBarSearchInputChangedHook = ( + options: NavigationBarSearchInputChangedOption +) => void + +type NavigationBarSearchInputConfirmedOption = + NavigationBarSearchInputChangedOption + +type onNavigationBarSearchInputConfirmedHook = ( + options: NavigationBarSearchInputConfirmedOption +) => void +interface NavigationBarSearchInputFocusChanged { + focus: boolean +} + +type onNavigationBarSearchInputFocusChangedHook = ( + options: NavigationBarSearchInputFocusChanged +) => void + const createHook = any>( lifecycle: typeof UniLifecycleHooks[number] @@ -38,45 +166,58 @@ const createHook = // post-create lifecycle registrations are noops during SSR !isInSSRComponentSetup && injectHook(lifecycle as any, hook, target) -export const onShow = /*#__PURE__*/ createHook(ON_SHOW) +export const onShow = /*#__PURE__*/ createHook( + ON_SHOW +) export const onHide = /*#__PURE__*/ createHook(ON_HIDE) -export const onLaunch = /*#__PURE__*/ createHook(ON_LAUNCH) -export const onError = /*#__PURE__*/ createHook(ON_ERROR) -export const onThemeChange = /*#__PURE__*/ createHook(ON_THEME_CHANGE) -export const onPageNotFound = /*#__PURE__*/ createHook(ON_PAGE_NOT_FOUND) -export const onUnhandledRejection = /*#__PURE__*/ createHook( - ON_UNHANDLE_REJECTION -) +export const onLaunch = /*#__PURE__*/ createHook(ON_LAUNCH) +export const onError = /*#__PURE__*/ createHook(ON_ERROR) +export const onThemeChange = + /*#__PURE__*/ createHook(ON_THEME_CHANGE) +export const onPageNotFound = + /*#__PURE__*/ createHook(ON_PAGE_NOT_FOUND) +export const onUnhandledRejection = + /*#__PURE__*/ createHook(ON_UNHANDLE_REJECTION) // 小程序如果想在 setup 的 props 传递页面参数,需要定义 props,故同时暴露 onLoad 吧 -export const onLoad = /*#__PURE__*/ createHook(ON_LOAD) +export const onLoad = /*#__PURE__*/ createHook(ON_LOAD) export const onReady = /*#__PURE__*/ createHook(ON_READY) export const onUnload = /*#__PURE__*/ createHook(ON_UNLOAD) -export const onResize = /*#__PURE__*/ createHook(ON_RESIZE) -export const onBackPress = /*#__PURE__*/ createHook(ON_BACK_PRESS) -export const onPageScroll = /*#__PURE__*/ createHook(ON_PAGE_SCROLL) -export const onTabItemTap = /*#__PURE__*/ createHook(ON_TAB_ITEM_TAP) +export const onResize = /*#__PURE__*/ createHook(ON_RESIZE) +export const onBackPress = + /*#__PURE__*/ createHook(ON_BACK_PRESS) +export const onPageScroll = + /*#__PURE__*/ createHook(ON_PAGE_SCROLL) +export const onTabItemTap = + /*#__PURE__*/ createHook(ON_TAB_ITEM_TAP) export const onReachBottom = /*#__PURE__*/ createHook(ON_REACH_BOTTOM) export const onPullDownRefresh = /*#__PURE__*/ createHook(ON_PULL_DOWN_REFRESH) -export const onShareTimeline = /*#__PURE__*/ createHook(ON_SHARE_TIMELINE) -export const onAddToFavorites = /*#__PURE__*/ createHook(ON_ADD_TO_FAVORITES) -export const onShareAppMessage = /*#__PURE__*/ createHook(ON_SHARE_APP_MESSAGE) +export const onShareTimeline = + /*#__PURE__*/ createHook(ON_SHARE_TIMELINE) +export const onAddToFavorites = + /*#__PURE__*/ createHook(ON_ADD_TO_FAVORITES) +export const onShareAppMessage = + /*#__PURE__*/ createHook(ON_SHARE_APP_MESSAGE) -export const onNavigationBarButtonTap = /*#__PURE__*/ createHook( - ON_NAVIGATION_BAR_BUTTON_TAP -) +export const onNavigationBarButtonTap = + /*#__PURE__*/ createHook( + ON_NAVIGATION_BAR_BUTTON_TAP + ) -export const onNavigationBarSearchInputChanged = /*#__PURE__*/ createHook( - ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED -) +export const onNavigationBarSearchInputChanged = + /*#__PURE__*/ createHook( + ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED + ) export const onNavigationBarSearchInputClicked = /*#__PURE__*/ createHook( ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED ) -export const onNavigationBarSearchInputConfirmed = /*#__PURE__*/ createHook( - ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED -) -export const onNavigationBarSearchInputFocusChanged = /*#__PURE__*/ createHook( - ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED -) +export const onNavigationBarSearchInputConfirmed = + /*#__PURE__*/ createHook( + ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED + ) +export const onNavigationBarSearchInputFocusChanged = + /*#__PURE__*/ createHook( + ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED + ) -- GitLab