-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuseGamepad.ts
75 lines (63 loc) · 2.71 KB
/
useGamepad.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { useCallback, useEffect, useRef } from "react";
export function useGamepad(
{didConnect, didDisconnect, didUpdate}: {
didConnect: (gamepad: Gamepad) => void,
didDisconnect: (gamepad: Gamepad) => void,
didUpdate: (gamepad: Gamepad) => void,
}
): void {
// MDN says that valid request IDs are non-zero, so we use zero to indicate
// that there is no pending animation request.
const animationRequestId = useRef<number>(0);
const onAnimationFrame = useCallback(() => {
let gamepadCount = 0;
for (const gamepad of navigator.getGamepads()) {
if (gamepad == null) continue;
didUpdate(gamepad);
gamepadCount ++;
}
// Reschedule for the next animation frame if there are any gamepads
animationRequestId.current = (gamepadCount === 0) ? 0 :
window.requestAnimationFrame(onAnimationFrame);
}, [didUpdate]);
// onAnimationFrame reschedules itself, and the reference to itself can
// become stale as dependencies change. When this happens, cancel the old
// function and schedule the new one. (Thanks Adam!)
useEffect(() => {
if (animationRequestId.current !== 0) {
window.cancelAnimationFrame(animationRequestId.current);
}
animationRequestId.current =
window.requestAnimationFrame(onAnimationFrame);
}, [onAnimationFrame]);
const onConnect = useCallback((event: GamepadEvent) => {
didConnect(event.gamepad);
// Schedule an animation frame if there is not already one pending
if (animationRequestId.current === 0) {
animationRequestId.current =
window.requestAnimationFrame(onAnimationFrame);
}
}, [didConnect, onAnimationFrame]);
const onDisconnect = useCallback((event: GamepadEvent) => {
didDisconnect(event.gamepad);
}, [didDisconnect]);
// Register event listeners for gamepad connection and disconnection, and
// unregister them when the component unmounts.
useEffect(() => {
window.addEventListener("gamepadconnected", onConnect);
window.addEventListener("gamepaddisconnected", onDisconnect);
return () => {
window.removeEventListener("gamepadconnected", onConnect);
window.removeEventListener("gamepaddisconnected", onDisconnect);
};
}, [onConnect, onDisconnect]);
// Cancel any pending animation frames when the component unmounts
useEffect(() => {
return () => {
if (animationRequestId.current !== 0) {
window.cancelAnimationFrame(animationRequestId.current);
animationRequestId.current = 0;
}
};
}, []);
}