Skip to content

Commit

Permalink
fix(mobile): add correct dependencies in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
vydimitrov committed Aug 29, 2020
1 parent 288320f commit 9d77921
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/mobile/__tests__/TimeWrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Math.random = () => 0.124578

const fixture = {
durationMilliseconds: 10000,
animatedElapsedTime: { addListener: () => {} },
animatedElapsedTime: { addListener: jest.fn(), removeListener: jest.fn() },
renderAriaTime: ({ remainingTime }) => remainingTime,
animatedColor: {},
children: ({ remainingTime }) => <Text>{remainingTime}</Text>,
Expand Down Expand Up @@ -40,6 +40,7 @@ describe('behaviour tests', () => {
it('should pass the new remaining time to the children when new value is provided by the listener', () => {
let listener
const animatedElapsedTime = {
removeListener: jest.fn(),
addListener: (cb) => {
listener = cb
},
Expand Down
8 changes: 6 additions & 2 deletions packages/mobile/src/components/TimeWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ const TimeWrapper = (props) => {
})

useEffect(() => {
animatedElapsedTime.addListener(({ value }) => {
const animatedListenerId = animatedElapsedTime.addListener(({ value }) => {
setTimeProps({
remainingTime: Math.ceil((durationMilliseconds - value) / 1000),
elapsedTime: value,
animatedColor,
})
})
}, [])

return () => {
animatedElapsedTime.removeListener(animatedListenerId)
}
}, [animatedElapsedTime, animatedColor, durationMilliseconds])

return (
<>
Expand Down
6 changes: 3 additions & 3 deletions packages/mobile/src/hooks/useCountdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const useCountdown = ({
animatedElapsedTime.removeAllListeners()
clearTimeout(repeatTimeoutRef.current)
}
}, [])
}, [animatedElapsedTime, startAt])

// toggle playing effect
useEffect(() => {
Expand All @@ -81,7 +81,7 @@ export const useCountdown = ({
if (finished && elapsedTime.current === durationMilliseconds) {
setIsProgressPathVisible(false)
if (typeof onComplete === 'function') {
totalElapsedTime.current += duration
totalElapsedTime.current += durationMilliseconds / 1000

const [shouldRepeat = false, delay = 0] =
onComplete(totalElapsedTime.current) || []
Expand All @@ -106,7 +106,7 @@ export const useCountdown = ({
return () => {
animatedElapsedTime.stopAnimation()
}
}, [isPlaying])
}, [isPlaying, animatedElapsedTime, durationMilliseconds, onComplete])

return {
path,
Expand Down

0 comments on commit 9d77921

Please sign in to comment.