Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useGamepad): improve data updating logic #3775

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 6 additions & 15 deletions packages/core/useGamepad/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,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 @@ -124,13 +119,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