import create , { CreateFunction , IHostInstance } from './create' ; import isFunction from '../common/isFunction' ; import { OptionsEventName } from '../context/PageEventContext' ; export default function createPage ( fn ) { const options = { } ; const hooksOptions = create ( fn , 'page' ) ; type PageInstance = IHostInstance < undefined > & tinyapp . IPageInstance < any > & Pick < tinyapp . IPageOptionsMethods , 'onShareAppMessage' > ; const oldOnLoad = options . onLoad ; options . onLoad = function ( this , query ) { this . $$query = query ; hooksOptions . init . call ( this ) ; if ( this . $$pageEventContext ?. hasEventCallback ( 'onShareAppMessage' ) ) { const oldOnShareAppMessage = this . onShareAppMessage ; this . onShareAppMessage = function ( this , options ) { const previousResult = isFunction ( oldOnShareAppMessage ) ? oldOnShareAppMessage . call ( this , options ) : { } ; return { ... previousResult , ... this . $$pageEventContext ?. call ( 'onShareAppMessage' , this , options ) , } ; } ; } if ( isFunction ( oldOnLoad ) ) { oldOnLoad . call ( this , query ) ; } } ; const oldOnReady = options . onReady ; options . onReady = function ( this ) { if ( isFunction ( oldOnReady ) ) { oldOnReady . call ( this ) ; } hooksOptions . mounted . call ( this ) ; } ; const oldOnUnload = options . onUnload ; options . onUnload = function ( this ) { hooksOptions . unmounted . call ( this ) ; if ( isFunction ( oldOnUnload ) ) { oldOnUnload . call ( this ) ; } } ; const optionsEventNameList = [ 'onLoad' , 'onShow' , 'onReady' , 'onHide' , 'onUnload' , 'onPageScroll' , 'onReachBottom' , ] ; optionsEventNameList . forEach ( name => { const oldFn = options [ name ] ; options [ name ] = function ( this , ... args ) { if ( isFunction ( oldFn ) ) { ( oldFn as any ) . call ( this , ... args ) ; } this . $$pageEventContext ?. call ( name , this , ... args ) ; } as any ; } ) ; const eventNameList = [ 'onBack' , 'onKeyboardHeight' , 'onOptionMenuClick' , 'onPopMenuClick' , 'onPullIntercept' , 'onPullDownRefresh' , 'onTitleClick' , 'onTabItemTap' , 'beforeTabItemTap' , ] ; const events = options . events || { } ; eventNameList . forEach ( name => { const oldFn = events [ name ] ; events [ name ] = function ( this , ... args ) { if ( isFunction ( oldFn ) ) { ( oldFn as any ) . call ( this , ... args ) ; } this . $$pageEventContext ?. call ( name , this , ... args ) ; } as any ; } ) ; options . events = events ; return options ; }