Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swiper重构 #1711

Merged
merged 62 commits into from
Jan 15, 2025
Merged

Swiper重构 #1711

merged 62 commits into from
Jan 15, 2025

Conversation

xiao19880917lu
Copy link
Collaborator

No description provided.

let renderChild = children.slice()
if (props.circular && totalElements.value > 1) {
if (totalElements.value === 2) {
renderChild = renderChild.concat(children).concat(children)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

添加节点需要cloneElement

renderChild = renderChild.concat(children).concat(children)
} else {
// 最前加两个
renderChild.unshift(children[totalElements.value - 1])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前面不需要加元素

], { layoutRef: layoutRef })

function onWrapperLayout (e: LayoutChangeEvent) {
nodeRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

layout里面可以直接获取高宽,不需要measure


function onWrapperLayout (e: LayoutChangeEvent) {
nodeRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要

}
const attr = dir.value === 'x' ? 'width' : 'height'
changeState[attr] = changeState[attr] - previousMargin - nextMargin
if (dir.value === 'x') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不应该都去设置么

if (totalElements.value <= 1) return null
const dots: Array<ReactNode> = []
for (let i = 0; i < totalElements.value; i++) {
const dotStyle = useAnimatedStyle(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不应该在for中使用hooks,假如elements数量更新了就炸了

}

function handlerInterval (loopv: boolean, oldLoop: boolean | null) {
clearInterval(intervalId.current)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥有一个timeout有一个interval?

intervalId.current = setInterval(() => {
// canLoop变化比较快的情况下
if (canLoop.value) {
createAutoPlay()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个东西为啥要声明为worklet?看上去是个纯js函数

if (!isInit && props.bindchange) {
runOnJS(handleSwiperChange)(newIndex)
}
}, [targetIndex.value])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不需要写deps把

}
}, [targetIndex.value])

useAnimatedReaction(() => canLoop.value, (loopv, oldLoop) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canLoop是干啥的

}
}, [])

const reachBoundary = useCallback((eventData: EventDataType) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以把这一堆函数全都放在下面的useMemo里就行了,就不需要这么多useCallback了

const nextMarginShared = useSharedValue(nextMargin)
// 默认前后补位的元素个数
const patchEleNum = props.circular ? (preMargin ? 2 : 1) : 0
const patchElementNum = useSharedValue(patchEleNum)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有更新逻辑

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patchElmNum/patchElmNumShared

// 默认前后补位的元素个数
const patchEleNum = props.circular ? (preMargin ? 2 : 1) : 0
const patchElementNum = useSharedValue(patchEleNum)
const circular = useSharedValue(props.circular || false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有更新

}
})

const renderPagination = useCallback(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不需要useCallback

useEffect(() => {
let patchStep = 0
if (preMargin !== previousMargin.value) {
patchStep = preMargin - previousMargin.value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+=

const { textProps } = splitProps(props)
const preMargin = props['previous-margin'] ? parseInt(props['previous-margin']) : 0
const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0
const previousMargin = useSharedValue(preMargin)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preMarginShared

}
// resumeLoop在worklet中调用
function resumeLoop () {
if (props.autoplay && childrenLength.value > 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autoplay也得是个sharedValue

return targetOffset
}
// loop在JS线程中调用,createAutoPlay + useEffect中
function loop () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个clearTimeout

return
}
const targetOffset = getOffset(props.current || 0, step.value)
if (props.current !== undefined && props.current !== currentIndex.value) {
Copy link
Collaborator

@hiyuki hiyuki Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

封装updateCurrent函数在useEffect和onLayout中复用

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

判断条件可以改为targetOffset!==offset.value

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

内部基于props.current!==currentIndex.value决定是否使用动画及更新currentIndex.value

if (!step.value) {
return
}
if (props.autoplay && children.length > 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

封装updateAutoplay函数在useEffect和onLayout中复用

duration: easeDuration,
easing: easeMap[easeingFunc]
}, () => {
if (touchfinish.value !== false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

验证一下回调在cancelAnimation时的执行时机,是立刻执行还是得等duration结束再执行

return
}
const { isBoundary, resetOffset } = reachBoundary(eventData)
if (isBoundary && circular.value && !touchfinish.value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

touchmove时理论上不需要判断touchfinish吧

moveTime.value = new Date().getTime()
})
.onTouchesMove((e) => {
'worklet'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(touchfinish.value) return

const gestureHandler = useMemo(() => {
const gesturePan = Gesture.Pan()
.onBegin((e) => {
'worklet'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(!step.value) return

preAbsolutePos.value = touchEventData[strAbso]
})
.onTouchesUp((e) => {
'worklet'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(touchfinish.value) return

const realHeight = dir === 'y' ? height - preMargin - nextMargin : height
const iStep = dir === 'x' ? realWidth : realHeight
step.value = iStep
if (touchfinish.value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个判断干掉

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

判断step.value!==iStep再执行更新逻辑

return (<SwiperContext.Provider value={contextValue}>{arrChildren}</SwiperContext.Provider>)
}

const createAutoPlay = useCallback(() => {
Copy link
Collaborator

@hiyuki hiyuki Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMemo(()=>{
function createAutoplay(){}
function loop(){}
function resumeLoop(){}
function pauseLoop(){}
return {
loop,
resumeLoop,
pauseLoop
}
})

import { GestureDetector, Gesture } from 'react-native-gesture-handler'
import Animated, { useAnimatedStyle, useSharedValue, withTiming, Easing, runOnJS, useAnimatedReaction, cancelAnimation } from 'react-native-reanimated'

import React, { JSX, forwardRef, useRef, useEffect, ReactNode, ReactElement, useCallback, useMemo } from 'react'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useCallback可以删掉了

const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0
const previousMarginShared = useSharedValue(preMargin)
const nextMarginShared = useSharedValue(nextMargin)
const autoplayShared = useSharedValue(autoplay || false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

props的默认值统一在上面给吧

const { textProps } = splitProps(props)
const preMargin = props['previous-margin'] ? parseInt(props['previous-margin']) : 0
const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0
const previousMarginShared = useSharedValue(preMargin)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改为preMarginShared吧,和上面的preMargin对齐

}, [props.current])

useEffect(() => {
autoplayShared.value = autoplay || false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要默认值,统一在解构props时赋予默认值

const { textStyle } = splitStyle(normalStyle)
const { textProps } = splitProps(props)
const preMargin = props['previous-margin'] ? parseInt(props['previous-margin']) : 0
const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用global.__formatValue来获取数值,可以处理rpx

@hiyuki hiyuki merged commit b440816 into master Jan 15, 2025
6 checks passed
@hiyuki hiyuki deleted the feat-swiper-refactor branch January 15, 2025 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants