All files / mp miniProgramBus.ts

0% Statements 0/9
100% Branches 0/0
0% Functions 0/3
0% Lines 0/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                                                                                                                                                                                                                                       
import { AnyObject, Defined } from '../types'
import { EventBus, EventBusBeforeOn, EventBusListener } from '../utils'
import { patchMiniProgram } from './patchMiniProgram'
 
export interface MiniProgramBusRouteChangePageInfo {
  url: string
  path: string
  query: AnyObject
}
 
export type MiniProgramBusRouteChangeAction = 'replace' | 'pop' | 'push'
 
export interface MiniProgramBusRouteChangePayload {
  from: MiniProgramBusRouteChangePageInfo | undefined
  to: MiniProgramBusRouteChangePageInfo
  action: MiniProgramBusRouteChangeAction
}
 
export interface MiniProgramBusListeners {
  appLaunch: Defined<WechatMiniprogram.App.Options<{}>['onLaunch']>
  appShow: Defined<WechatMiniprogram.App.Options<{}>['onShow']>
  appHide: Defined<WechatMiniprogram.App.Options<{}>['onHide']>
  appError: Defined<WechatMiniprogram.App.Options<{}>['onError']>
  appThemeChange: Defined<WechatMiniprogram.App.Options<{}>['onThemeChange']>
  appUnhandledRejectionThrow: Defined<
    WechatMiniprogram.App.Options<{}>['onUnhandledRejection']
  >
  pageNotFound: Defined<WechatMiniprogram.App.Options<{}>['onPageNotFound']>
  pageLoad: Defined<WechatMiniprogram.Page.Options<{}, {}>['onLoad']>
  pageShow: Defined<WechatMiniprogram.Page.Options<{}, {}>['onShow']>
  pageHide: Defined<WechatMiniprogram.Page.Options<{}, {}>['onHide']>
  pageReady: Defined<WechatMiniprogram.Page.Options<{}, {}>['onReady']>
  pageUnload: Defined<WechatMiniprogram.Page.Options<{}, {}>['onUnload']>
  pagePullDownRefresh: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onPullDownRefresh']
  >
  pageReachBottom: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onReachBottom']
  >
  pageShareAppMessage: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onShareAppMessage']
  >
  pageShareTimeline: Defined<
    // @ts-ignore
    WechatMiniprogram.Page.Options<{}, {}>['onShareTimeline']
  >
  pageAddToFavorites: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onAddToFavorites']
  >
  // 影响性能,暂不支持
  // pageScroll: Defined<WechatMiniprogram.Page.Options<{}, {}>['onPageScroll']>
  pageResize: Defined<WechatMiniprogram.Page.Options<{}, {}>['onResize']>
  pageTabItemTap: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onTabItemTap']
  >
  currentPageShow: Defined<WechatMiniprogram.Page.Options<{}, {}>['onShow']>
  currentPageHide: Defined<WechatMiniprogram.Page.Options<{}, {}>['onHide']>
  currentPageReady: Defined<WechatMiniprogram.Page.Options<{}, {}>['onReady']>
  currentPageUnload: Defined<WechatMiniprogram.Page.Options<{}, {}>['onUnload']>
  currentPagePullDownRefresh: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onPullDownRefresh']
  >
  currentPageReachBottom: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onReachBottom']
  >
  currentPageShareAppMessage: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onShareAppMessage']
  >
  currentPageShareTimeline: Defined<
    // @ts-ignore
    WechatMiniprogram.Page.Options<{}, {}>['onShareTimeline']
  >
  currentPageAddToFavorites: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onAddToFavorites']
  >
  // 影响性能,暂不支持
  // currentPageScroll: Defined<WechatMiniprogram.Page.Options<{}, {}>['onPageScroll']>
  currentPageResize: Defined<WechatMiniprogram.Page.Options<{}, {}>['onResize']>
  currentPageTabItemTap: Defined<
    WechatMiniprogram.Page.Options<{}, {}>['onTabItemTap']
  >
  routeChange: (payload: MiniProgramBusRouteChangePayload) => any
}
 
/** @private */
export const pageListenerToCurrentPageListener: Partial<
  Record<keyof MiniProgramBusListeners, keyof MiniProgramBusListeners>
> = {
  pageShow: 'currentPageShow',
  pageHide: 'currentPageHide',
  pageReady: 'currentPageReady',
  pageUnload: 'currentPageUnload',
  pagePullDownRefresh: 'currentPagePullDownRefresh',
  pageReachBottom: 'currentPageReachBottom',
  pageShareAppMessage: 'currentPageShareAppMessage',
  pageShareTimeline: 'currentPageShareTimeline',
  pageAddToFavorites: 'currentPageAddToFavorites',
  pageResize: 'currentPageResize',
  pageTabItemTap: 'currentPageTabItemTap',
}
 
/** @private */
export const pageListeners = Object.keys(
  pageListenerToCurrentPageListener,
) as Array<keyof MiniProgramBusListeners>
/** @private */
export const currentPageListeners = pageListeners.map(
  pageListener => pageListenerToCurrentPageListener[pageListener],
) as Array<keyof MiniProgramBusListeners>
 
// @ts-ignore
export const miniProgramBus = new EventBus<MiniProgramBusListeners>({
  beforeOn: currentPageListeners.reduce<
    // @ts-ignore
    EventBusBeforeOn<MiniProgramBusListeners>
  >((res, currentPageListenerName) => {
    res[currentPageListenerName] = function (cb: EventBusListener) {
      cb.__EVENT_BUS_TAG__ = patchMiniProgram.__CURRENT_PAGE_ID__
      return cb
    }
    return res
  }, {}),
})