createIntersectionObserver.ts 2.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import { extend } from '@vue/shared'

fxy060608's avatar
fxy060608 已提交
3 4
import { ServiceJSBridge } from '@dcloudio/uni-core'

fxy060608's avatar
fxy060608 已提交
5
import { createApi } from '../../helpers/api'
fxy060608's avatar
fxy060608 已提交
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
import { getCurrentPageVm } from '../utils'

const defaultOptions = {
  thresholds: [0],
  initialRatio: 0,
  observeAll: false
}

interface Margins {
  bottom?: number
  left?: number
  right?: number
  top?: number
}

interface RelativeInfo {
  selector: string
  margins: Margins
}

type ObserveResultCallback = (result: UniApp.ObserveResult) => void

interface requestComponentObserver {
  reqId: number
  reqEnd: boolean
  res: UniApp.ObserveResult
}

let reqComponentObserverId = 1

const reqComponentObserverCallbacks: Record<number, ObserveResultCallback> = {}

fxy060608's avatar
fxy060608 已提交
38
ServiceJSBridge.subscribe(
fxy060608's avatar
fxy060608 已提交
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
  'requestComponentObserver',
  ({ reqId, reqEnd, res }: requestComponentObserver) => {
    const callback = reqComponentObserverCallbacks[reqId]
    if (callback) {
      if (reqEnd) {
        return delete reqComponentObserverCallbacks[reqId]
      }
      callback(res)
    }
  }
)

class ServiceIntersectionObserver {
  private _reqId?: number
  private _options: UniApp.CreateIntersectionObserverOptions
  private _component: any
  private _pageId: number

  private _relativeInfo: RelativeInfo[]

  constructor(
    component: any,
    options: UniApp.CreateIntersectionObserverOptions
  ) {
    this._pageId = component.$page.id
    this._component = component._$id || component // app-plus 平台传输_$id
    this._options = extend({}, defaultOptions, options || {})
    this._relativeInfo = []
  }

  relativeTo(selector: string, margins: Margins) {
    if (this._reqId) {
      throw new Error(
        'Relative nodes cannot be added after "observe" call in IntersectionObserver'
      )
    }
    this._relativeInfo.push({
      selector,
      margins
    })
    return this
  }

  relativeToViewport(margins: Margins) {
    return this.relativeTo((null as unknown) as string, margins)
  }

  observe(selector: string, callback: ObserveResultCallback) {
    if (typeof callback !== 'function') {
      return
    }
    if (this._reqId) {
      throw new Error(
        '"observe" call can be only called once in IntersectionObserver'
      )
    }

    this._reqId = reqComponentObserverId++
    reqComponentObserverCallbacks[this._reqId] = callback

    UniServiceJSBridge.publishHandler(
      'addIntersectionObserver',
      {
        selector,
        reqId: this._reqId,
        component: this._component,
        options: this._options,
        relativeInfo: this._relativeInfo
      },
      this._pageId
    )
  }

  disconnect() {
    UniServiceJSBridge.publishHandler(
      'removeIntersectionObserver',
      {
        reqId: this._reqId
      },
      this._pageId
    )
  }
}
fxy060608's avatar
fxy060608 已提交
122

fxy060608's avatar
fxy060608 已提交
123 124 125 126 127 128 129 130
export const createIntersectionObserver = createApi<
  typeof uni.createIntersectionObserver
>((context?, options?) => {
  if (!context) {
    context = getCurrentPageVm()
  }
  return new ServiceIntersectionObserver(context, options)
})