Skip to content

Commit

Permalink
fix(useGamepad): improve data updating logic (vueuse#3775)
Browse files Browse the repository at this point in the history
  • Loading branch information
okydk authored and noook committed Mar 26, 2024
1 parent 84cde82 commit aff1297
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions packages/core/useGamepad/index.ts
Original file line number Diff line number Diff line change
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 aff1297

Please sign in to comment.