Skip to content

Commit

Permalink
Clean up timeouts and empty catches
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-nexus committed Jul 24, 2024
1 parent 75618cb commit de60fde
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
19 changes: 12 additions & 7 deletions src/components/AnimatedEye/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function AnimatedEye({ isLoading }: { isLoading: boolean }) {
pupil.current.style.transform = `translate(${translateX}px, ${translateY}px)`
}
catch (error) {

console.log(error)
}
}
}
Expand All @@ -42,34 +42,39 @@ export default function AnimatedEye({ isLoading }: { isLoading: boolean }) {

// spin when loading
useEffect(() => {
let timeout: NodeJS.Timeout
if (pupil.current && isLoading) {
setTimeout(() => {
timeout = setTimeout(() => {
// Calculate the x and y based on angle. Multiply with different factors for x and y, to account for eye ball height being greater than width.
const translateX = (Math.cos(angle * (Math.PI / 180)) * PUPIL_X_ADJUSTMENT)
const translateY = (Math.sin(angle * (Math.PI / 180)) * PUPIL_Y_ADJUSTMENT)
// Just repeating the try catch from above, for extra safety
try {
pupil.current.style.transform = `translate(${translateX}px, ${translateY}px)`
} catch (error) {

console.log(error)
}
setAngle(angle + 10)
}, 20)
}

return (() => clearTimeout(timeout))
}, [angle, isLoading])

// blink eye periodically
useEffect(() => {
let timeout: NodeJS.Timeout
if (isClosed) {
setTimeout(() => {
timeout = setTimeout(() => {
setIsClosed(false)
}, 400);
}, 400)
}
else {
setTimeout(() => {
timeout = setTimeout(() => {
setIsClosed(true)
}, 2500);
}, 2500)
}
return (() => clearTimeout(timeout))
}, [isClosed])

return (
Expand Down
8 changes: 2 additions & 6 deletions src/components/Player/PlayerControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ export default function PlayerControls() {
useEffect(() => {
if (isMobileOrTablet) {
window.addEventListener("blur", blurCallback)
return (() => {
window.removeEventListener("blur", blurCallback)
})
return (() => window.removeEventListener("blur", blurCallback))
}
}, [isMobileOrTablet])

Expand All @@ -95,9 +93,7 @@ export default function PlayerControls() {
useEffect(() => {
if (getIframeStatus() === "cross-origin-iframe") {
window.addEventListener("blur", blurCallback)
return (() => {
window.removeEventListener("blur", blurCallback)
})
return (() => window.removeEventListener("blur", blurCallback))
}
}, [])

Expand Down
4 changes: 1 addition & 3 deletions src/components/Toast.tsx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export default function Toast({ showToast, setShowToast, children }: ToastProps)
}, 5000)

// Clear the timeout
return () => {
clearTimeout(toastTimeOut)
}
return (() => clearTimeout(toastTimeOut))
}
// showToast dependency to show toast whenever setShowToast(true) is called
// children dependency to clear old toast timeout and start a new timeout, whenever children changes
Expand Down

0 comments on commit de60fde

Please sign in to comment.