Skip to content

Commit

Permalink
feat: try to avoid freezes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBellas committed May 3, 2024
1 parent 75ae923 commit 961957e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/views/Game/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ import {
import { IDirection } from '@interfaces/direction'
import BoardDatabaseService from '@services/database/board.database'
import { RotateCcw } from 'lucide-react-native'
import { memo, useEffect } from 'react'
import { useEffect, useState } from 'react'
import { Alert, Platform } from 'react-native'
import { Gesture, GestureDetector } from 'react-native-gesture-handler'
import { useSharedValue } from 'react-native-reanimated'

import Tile from './Tile'

const Board = () => {
export default function Board() {
// #region Contexts
const { board, isGameOver, hasWon, numOfMoves } = useBoard()
const boardDispatch = useBoardDispatch()
// #endregion

const [isMoveInTimeout, setIsMoveInTimeout] = useState(false)

// #region Constant values
const space = 'md'
const moveTimeout = 100
// #endregion

// #region Animation
Expand All @@ -35,11 +38,14 @@ const Board = () => {
const pan = Gesture.Pan()
.activeOffsetX([-80, 80])
.activeOffsetY([-80, 80])
.shouldCancelWhenOutside(true)
.onStart((e) => {
startXSlide.value = e.x
startYSlide.value = e.y
})
.onEnd((e) => {
if (isMoveInTimeout) return

const dx = e.x - (startXSlide.value ?? 0)
const dy = e.y - (startYSlide.value ?? 0)
if (dx === 0 && dy === 0) return
Expand All @@ -58,6 +64,7 @@ const Board = () => {
: { type: 'move', direction: 'up' }
}

setIsMoveInTimeout(true)
boardDispatch(boardAction)
})
.onFinalize(() => {
Expand All @@ -68,6 +75,15 @@ const Board = () => {

// #region Effects

// onMoveTimeout
useEffect(() => {
if (!isMoveInTimeout) return

setTimeout(() => {
setIsMoveInTimeout(false)
}, moveTimeout)
}, [isMoveInTimeout])

// onKeyboardMove
useEffect(() => {
if (Platform.OS !== 'web') return
Expand Down Expand Up @@ -210,5 +226,3 @@ const Board = () => {
</GestureDetector>
)
}

export default memo(Board)
6 changes: 5 additions & 1 deletion src/views/Game/Tile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ const Tile = (props: Readonly<ITileProps>) => {
justifyContent="center"
>
{image && <TileImage source={image.source} alt={image.alt} />}
<Text color={textColor} fontSize="$3xl" fontWeight="$bold">
<Text
color={textColor}
fontSize={value && value >= 1024 ? '$xl' : '$3xl'}
fontWeight="$bold"
>
{value}
</Text>
</AnimatedBox>
Expand Down

0 comments on commit 961957e

Please sign in to comment.