Skip to content

Commit

Permalink
fix(useGamepad): improve data updating logic (#3775)
Browse files Browse the repository at this point in the history
  • Loading branch information
okydk committed Feb 20, 2024
1 parent 8c73515 commit 9b8ed55
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions packages/core/useGamepad/index.ts
Expand Up @@ -92,14 +92,9 @@ export function useGamepad(options: UseGamepadOptions = {}) {
const updateGamepadState = () => {
const _gamepads = navigator?.getGamepads() || []

for (let i = 0; i < _gamepads.length; ++i) {
const gamepad = _gamepads[i]
if (gamepad) {
const index = gamepads.value.findIndex(({ index }) => index === gamepad.index)

if (index > -1)
gamepads.value[index] = stateFromGamepad(gamepad)
}
for (const gamepad of _gamepads) {
if (gamepad && gamepads.value[gamepad.index])
gamepads.value[gamepad.index] = stateFromGamepad(gamepad)
}
}

Expand All @@ -125,13 +120,9 @@ export function useGamepad(options: UseGamepadOptions = {}) {
tryOnMounted(() => {
const _gamepads = navigator?.getGamepads() || []

if (_gamepads) {
for (let i = 0; i < _gamepads.length; ++i) {
const gamepad = _gamepads[i]

if (gamepad)
onGamepadConnected(gamepad)
}
for (const gamepad of _gamepads) {
if (gamepad && gamepads.value[gamepad.index])
onGamepadConnected(gamepad)
}
})

Expand Down

0 comments on commit 9b8ed55

Please sign in to comment.