fix(player): address Greptile review on gamepad replay

Shorten the replay comment to two lines, and make the replay test assert
the pad lands in a player slot through a listener shaped like EmulatorJS's,
not just that dispatchEvent was called.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-26 14:10:43 -04:00
parent 16005c9474
commit 4ec4247e20
2 changed files with 19 additions and 7 deletions

View File

@@ -179,19 +179,32 @@ describe("installEJSDefaultOptionsTrap", () => {
expect(emulator.preGetSetting).toBe(patched);
});
it("replays connected for pads the gamepad handler saw before its listener", () => {
const connected = vi.fn();
it("assigns pads the gamepad handler saw before its listener to players", () => {
const emulator = makeEmulator({});
emulator.gamepadSelection = ["", "", "", ""];
type Listener = (arg: { gamepadIndex: number }) => void;
const listeners: Record<string, Listener> = {};
emulator.gamepad = {
gamepads: [{ index: 0, id: "pad" }, null, { index: 2, id: "pad" }],
on(name: string, cb: Listener) {
listeners[name] = cb;
},
dispatchEvent(name: string, arg: { gamepadIndex: number }) {
if (name === "connected") connected(arg.gamepadIndex);
listeners[name]?.(arg);
},
};
// Mirrors EmulatorJS 4.2.3's listener: fill the first free player slot.
const onConnected: Listener = ({ gamepadIndex }) => {
const pad = emulator.gamepad.gamepads[gamepadIndex];
const slot = emulator.gamepadSelection.indexOf("");
if (slot !== -1)
emulator.gamepadSelection[slot] = `${pad.id}_${pad.index}`;
};
emulator.gamepad.on("connected", onConnected);
window.EJS_emulator = emulator;
window.EJS_emulator = emulator;
expect(connected.mock.calls).toEqual([[0], [2]]);
expect(emulator.gamepadSelection).toEqual(["pad_0", "pad_2", "", ""]);
});
it("keeps booting when a replayed connected handler throws", () => {

View File

@@ -510,9 +510,8 @@ function installDefaultOptionsFallback(emulator: any) {
}
}
// GamepadHandler polls once in its constructor, before EmulatorJS registers
// its "connected" listener, so a pad the page already sees (e.g. the one that
// pressed Play) is never assigned to a player until it reconnects.
// GamepadHandler polls before EmulatorJS registers its "connected" listener,
// so pads it already saw (e.g. the one that pressed Play) get no player.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function replayConnectedGamepads(emulator: any) {
const handler = emulator.gamepad;