急停 发送ws 遥感控制 避免 raf循环闭包

This commit is contained in:
mmc
2026-10-15 14:02:02 +08:00
parent 4e1356b8af
commit 652dbdcc78
6 changed files with 46 additions and 3 deletions

View File

@@ -14,6 +14,9 @@ const XboxController = ({ onGamepadUpdate }) => {
});
const requestRef = useRef<number>(null);
// 用 ref 保存最新的回调,避免 RAF 循环闭包捕获旧 props 导致权限判断失效
const onGamepadUpdateRef = useRef(onGamepadUpdate);
onGamepadUpdateRef.current = onGamepadUpdate;
const DEAD_ZONE = 0.08;
const normalizeAxis = (v: number) =>
@@ -46,7 +49,7 @@ const XboxController = ({ onGamepadUpdate }) => {
now - lastSendRef.current > SEND_INTERVAL
// payload !== lastPayloadRef.current
) {
onGamepadUpdate(state); // ✅ 只有这里才发
onGamepadUpdateRef.current(state); // ✅ 只有这里才发
lastSendRef.current = now;
lastPayloadRef.current = payload;
}