import { inject , ref , onMounted } from 'vue' ; import { Current , getPageInstance , injectPageInstance } from '@tarojs/runtime' ; import { isFunction , isUndefined , isArray } from '@tarojs/shared' ; function createHook ( lifecycle ) { return fn => { const id = inject < string > ( 'id' ) ! ; const fnRef = ref ( fn ) ; onMounted ( ( ) => { let inst = getPageInstance ( id ) ; if ( inst === undefined ) { inst = Object . create ( { $options : { } } ) ; injectPageInstance ( inst ! , id ) ; } inst = inst . $options ; const callback = ( ... args ) => fnRef . value ( ... args ) ; const currentCallback = inst [ lifecycle ] ; if ( isUndefined ( currentCallback ) ) { inst [ lifecycle ] = callback ; } else if ( isFunction ( currentCallback ) ) { inst [ lifecycle ] = [ inst [ lifecycle ] , callback ] ; } else if ( isArray ( currentCallback ) ) { inst [ lifecycle ] = [ ... currentCallback , callback ] ; } } ) ; } ; } export const useDidShow = createHook ( 'onShow' ) ; export const useDidHide = createHook ( 'onHide' ) ; export const useReady = createHook ( 'onReady' ) ; export const usePullDownRefresh = createHook ( 'onPullDownRefresh' ) ; export const useReachBottom = createHook ( 'onReachBottom' ) ; export const usePageScroll = createHook ( 'onPageScroll' ) ; export const useResize = createHook ( 'onResize' ) ; export const useShareAppMessage = createHook ( 'onShareAppMessage' ) ; export const useTabItemTap = createHook ( 'onTabItemTap' ) ; export const useTitleClick = createHook ( 'onTitleClick' ) ; export const useOptionMenuClick = createHook ( 'onOptionMenuClick' ) ; export const usePullIntercept = createHook ( 'onPullIntercept' ) ; export const useShareTimeline = createHook ( 'onShareTimeline' ) ; export const useAddToFavorites = createHook ( 'onAddToFavorites' ) ; export const useRouter = ( ) => { return Current . router ; } ;