Skip to content

Commit

Permalink
fix: revert autoPlay changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Sep 12, 2024
1 parent 8b0096e commit 5fa78a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-snails-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-native-reanimated-carousel": patch
---

revert autoPlay changes
35 changes: 10 additions & 25 deletions src/hooks/useAutoPlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,26 @@ export function useAutoPlay(opts: {
} = opts;

const { prev, next } = carouselController;
const lastTimestampRef = React.useRef<number | null>(null);
const timer = React.useRef<ReturnType<typeof setTimeout>>();
const stopped = React.useRef<boolean>(!autoPlay);

const play = React.useCallback(() => {
if (stopped.current)
return;

const currentTimestamp = Date.now();

if (lastTimestampRef.current) {
const elapsed = currentTimestamp - lastTimestampRef.current;

if (elapsed >= (autoPlayInterval ?? 1000)) {
autoPlayReverse
? prev({ onFinished: play })
: next({ onFinished: play });
lastTimestampRef.current = currentTimestamp;
}
}
else {
lastTimestampRef.current = currentTimestamp;
}

requestAnimationFrame(play);
timer.current && clearTimeout(timer.current);
timer.current = setTimeout(() => {
autoPlayReverse
? prev({ onFinished: play })
: next({ onFinished: play });
}, autoPlayInterval);
}, [autoPlayReverse, autoPlayInterval, prev, next]);

const pause = React.useCallback(() => {
if (!autoPlay)
return;

lastTimestampRef.current = null;
timer.current && clearTimeout(timer.current);
stopped.current = true;
}, [autoPlay]);

Expand All @@ -55,8 +44,7 @@ export function useAutoPlay(opts: {
return;

stopped.current = false;
lastTimestampRef.current = Date.now();
requestAnimationFrame(play);
play();
}, [play, autoPlay]);

React.useEffect(() => {
Expand All @@ -65,10 +53,7 @@ export function useAutoPlay(opts: {
else
pause();

return () => {
lastTimestampRef.current = null;
stopped.current = true;
};
return pause;
}, [pause, start, autoPlay]);

return {
Expand Down

0 comments on commit 5fa78a7

Please sign in to comment.