From 522fdf106eea97c3667ec34600d419254fcdb64d Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Wed, 2 Jul 2025 12:07:14 -0400 Subject: [PATCH] throttle mouse events --- components/TiltCard.tsx | 7 +++++-- hooks/useSocket.ts | 7 ++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/components/TiltCard.tsx b/components/TiltCard.tsx index 34e0c9a..f12fb86 100644 --- a/components/TiltCard.tsx +++ b/components/TiltCard.tsx @@ -1,5 +1,8 @@ import { useEffect, useRef, useState } from 'react'; import { useAppContext } from '@/app/AppContext'; +import throttle from '@/tools/throttle'; + +import { thirtyFPS } from '@/constants/time'; import type { Tilt } from '@/types'; const ZERO_ROTATION = 'rotateX(0deg) rotateY(0deg)'; @@ -90,7 +93,7 @@ export default function TiltCard({ sheen.style.opacity = '0'; }, [untilt]); - const handleMouseMove = (e: React.MouseEvent) => { + const handleMouseMove = throttle((e: React.MouseEvent) => { const card = cardRef.current; if (!card) return; @@ -107,7 +110,7 @@ export default function TiltCard({ newTilt[cardIndex] = { rotateX, rotateY }; setTilt(newTilt); - }; + }, thirtyFPS); const handleMouseLeave = () => { setTilt([]); diff --git a/hooks/useSocket.ts b/hooks/useSocket.ts index 4963bf6..37b1f37 100644 --- a/hooks/useSocket.ts +++ b/hooks/useSocket.ts @@ -1,8 +1,5 @@ import { useEffect } from 'react'; import { socket } from '@/socket'; -import throttle from '@/tools/throttle'; - -import { thirtyFPS } from '@/constants/time'; import type { GameUpdate, Tilt } from '@/types'; interface UseSocketProps { @@ -70,12 +67,12 @@ export default function useSocket({ gameID, setGameData, setNoGame }: UseSocketP }); }; - const emitTilt = throttle((cardIndex: number, tilt: Tilt) => { + const emitTilt = (cardIndex: number, tilt: Tilt) => { socket.emit('tilt', { cardIndex, tilt, }); - }, thirtyFPS); + }; return { emitFlip,