@@ -35,7 +35,7 @@ import Animated, {
3535
3636import useInnerProps from './getInnerListeners'
3737import useNodesRef , { HandlerRef } from './useNodesRef'
38- import { useLayout , useTransformStyle , extendObject } from './utils'
38+ import { useLayout , useTransformStyle , extendObject , useRunOnJSCallback } from './utils'
3939import Portal from './mpx-portal'
4040
4141export interface ProgressProps {
@@ -70,7 +70,6 @@ const Progress = forwardRef<
7070 active = false ,
7171 'active-mode' : activeMode = 'backwards' ,
7272 duration = 30 ,
73- bindactiveend,
7473 style = { } ,
7574 'enable-var' : enableVar ,
7675 'external-var-context' : externalVarContext ,
@@ -80,7 +79,7 @@ const Progress = forwardRef<
8079 } = props
8180
8281 const nodeRef = useRef ( null )
83- const propsRef = useRef ( { } )
82+ const propsRef = useRef ( props )
8483 propsRef . current = props
8584
8685 // 进度值状态
@@ -113,6 +112,22 @@ const Progress = forwardRef<
113112 style : normalStyle
114113 } )
115114
115+ // 使用 useRunOnJSCallback 处理动画回调
116+ const runOnJSCallbackRef = useRef ( {
117+ triggerActiveEnd : ( percent : number ) => {
118+ const currentProps = propsRef . current
119+ if ( currentProps . bindactiveend ) {
120+ currentProps . bindactiveend ( {
121+ type : 'activeend' ,
122+ detail : {
123+ percent : percent
124+ }
125+ } )
126+ }
127+ }
128+ } )
129+ const runOnJSCallback = useRunOnJSCallback ( runOnJSCallbackRef )
130+
116131 // 进度条动画函数
117132 const startProgressAnimation = ( targetPercent : number , startPercent : number , animationDuration : number , onFinished ?: ( ) => void ) => {
118133 // 根据 active-mode 设置起始位置
@@ -125,25 +140,13 @@ const Progress = forwardRef<
125140 } ,
126141 ( finished ) => {
127142 if ( finished && onFinished ) {
128- // 在动画回调中,需要使用runOnJS回到主线程
129- runOnJS ( onFinished ) ( )
143+ // 在动画回调中,执行传入的worklet函数
144+ onFinished ( )
130145 }
131146 }
132147 )
133148 }
134149
135- // 创建在主线程执行的事件回调函数
136- const triggerActiveEnd = ( percent : number ) => {
137- if ( bindactiveend ) {
138- bindactiveend ( {
139- type : 'activeend' ,
140- detail : {
141- percent : percent
142- }
143- } )
144- }
145- }
146-
147150 // 进度变化时的动画效果
148151 useEffect ( ( ) => {
149152 const targetPercent = Math . max ( 0 , Math . min ( 100 , percent ) )
@@ -161,19 +164,18 @@ const Progress = forwardRef<
161164 const percentDiff = Math . abs ( targetPercent - startPercent )
162165 const animationDuration = percentDiff * duration
163166
164- // 创建动画完成回调
165- const onAnimationFinished = ( ) => {
166- triggerActiveEnd ( targetPercent )
167- }
168-
169167 // 执行动画
170- startProgressAnimation ( targetPercent , startPercent , animationDuration , onAnimationFinished )
168+ startProgressAnimation ( targetPercent , startPercent , animationDuration , ( ) => {
169+ 'worklet'
170+ // 在worklet函数内部执行runOnJS调用runOnJSCallback
171+ runOnJS ( runOnJSCallback ) ( 'triggerActiveEnd' , targetPercent )
172+ } )
171173 } else {
172174 progressWidth . value = targetPercent
173175 }
174176
175177 setLastPercent ( targetPercent )
176- } , [ percent , active , activeMode , duration , bindactiveend ] )
178+ } , [ percent , active , activeMode , duration ] )
177179
178180 // 初始化时设置进度值
179181 useEffect ( ( ) => {
0 commit comments