subscribe.ts 501 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import { getPageVmById } from '../../helpers/page'
import { invokeHook } from '../../helpers/hook'
fxy060608's avatar
fxy060608 已提交
3

fxy060608's avatar
fxy060608 已提交
4 5
const SUBSCRIBE_LIFECYCLE_HOOKS = ['onPageScroll', 'onReachBottom']

6
export function initSubscribe() {
fxy060608's avatar
fxy060608 已提交
7 8 9
  SUBSCRIBE_LIFECYCLE_HOOKS.forEach((name) =>
    UniServiceJSBridge.subscribe(name, createPageEvent(name))
  )
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15 16
}

function createPageEvent(name: string) {
  return (args: unknown, pageId: number) => {
    const vm = getPageVmById(pageId)
    if (vm) {
      invokeHook(vm, name, args)
fxy060608's avatar
fxy060608 已提交
17
    }
fxy060608's avatar
fxy060608 已提交
18
  }
19
}