Skip to content

Commit

Permalink
fix: 相交api问题修改
Browse files Browse the repository at this point in the history
  • Loading branch information
human committed Jan 15, 2025
1 parent 38446f3 commit c53a7e1
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isArray, isObject, isString, noop } from '@mpxjs/utils'
import throttle from 'lodash/throttle'
import { Dimensions } from 'react-native'
import { getFocusedNavigation } from '../../../common/js'

const WindowRefStr = 'window'
const IgnoreTarget = 'ignore'
const DefaultMargin = { top: 0, bottom: 0, left: 0, right: 0 }
Expand All @@ -12,10 +11,14 @@ class RNIntersectionObserver {
constructor (component, options, intersectionCtx) {
this.id = idCount++
this.component = component
this.options = options
this.thresholds = options.thresholds.sort((a, b) => a - b) || [0]
this.initialRatio = options.initialRatio || 0
this.observeAll = options.observeAll || false
this.options = Object.assign({
thresholds: [0],
initialRatio: 0,
observeAll: false
}, options || {})
this.thresholds = this.options.thresholds.sort((a, b) => a - b) || [0]
this.initialRatio = this.options.initialRatio
this.observeAll = this.options.observeAll

// 组件上挂载对应的observers,用于在组件销毁的时候进行批量disconnect
this.component._intersectionObservers = this.component.__intersectionObservers || []
Expand All @@ -26,7 +29,7 @@ class RNIntersectionObserver {
this.margins = DefaultMargin
this.callback = noop

this.throttleMeasure = this.getThrottleMeasure(options.throttleTime || 100)
this.throttleMeasure = this.getThrottleMeasure(this.options.throttleTime || 100)

// 记录上一次相交的比例
this.previousIntersectionRatio = []
Expand Down Expand Up @@ -65,7 +68,7 @@ class RNIntersectionObserver {

observe (selector, callback) {
if (this.observerRefs) {
console.error('"observe" call can be only called once in IntersectionObserver')
console.warn('"observe" call can be only called once in IntersectionObserver')
return
}
let targetRef = null
Expand All @@ -75,7 +78,7 @@ class RNIntersectionObserver {
targetRef = this.component.__selectRef(selector, 'node')
}
if (!targetRef || targetRef.length === 0) {
console.error('intersection observer target not found')
console.warn('intersection observer target not found')
return
}
this.observerRefs = isArray(targetRef) ? targetRef : [targetRef]
Expand All @@ -87,18 +90,18 @@ class RNIntersectionObserver {
if (this.windowRect) return this.windowRect
const navigation = getFocusedNavigation() || {}
const screen = Dimensions.get('screen')
const navigationLayout = navigation.layout || {
const navigationLayout = navigation.layout || {
x: 0,
y: 0,
width: screen.width,
height: screen.height
}

const windowRect = {
top: navigationLayout.y + this.margins.top,
left: this.margins.left,
right: navigationLayout.width - this.margins.right,
bottom: navigationLayout.y + navigationLayout.height - this.margins.bottom
top: navigationLayout.y - this.margins.top,
left: 0 - this.margins.left,
right: navigationLayout.width + this.margins.right,
bottom: navigationLayout.y + navigationLayout.height + this.margins.bottom
}

this.windowRect = windowRect
Expand Down

0 comments on commit c53a7e1

Please sign in to comment.