Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/js/gamepad-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,9 @@ class GamepadHelper {
};

// Check if we have an image mapping for this controller and button
if (imageMappings[controllerType] && imageMappings[controllerType].buttons[buttonIndex]) {
return basePath + imageMappings[controllerType].folder + imageMappings[controllerType].buttons[buttonIndex];
const imageMapping = imageMappings[controllerType];
if (imageMapping?.buttons[buttonIndex]) {
return basePath + imageMapping.folder + imageMapping.buttons[buttonIndex];
}

// Return null if no image is available
Expand Down Expand Up @@ -406,7 +407,7 @@ class GamepadHelper {
*/
getAxisName(controllerType, axisIndex) {
const mapping = this.controllerMappings[controllerType] || this.controllerMappings[this.CONTROLLER_TYPES.STANDARD];
return mapping.axisMap && mapping.axisMap[axisIndex] || `Axis ${axisIndex}`;
return mapping.axisMap?.[axisIndex] || `Axis ${axisIndex}`;
}

/**
Expand All @@ -415,7 +416,7 @@ class GamepadHelper {
* @returns {boolean} True if vibration is supported, false otherwise
*/
isVibrationSupported(gamepad) {
return !(!gamepad || !gamepad.vibrationActuator);
return !!gamepad?.vibrationActuator;
}

/**
Expand All @@ -424,7 +425,7 @@ class GamepadHelper {
* @returns {{supported: boolean, type: string|null}} Vibration capabilities information
*/
getVibrationCapabilities(gamepad) {
if (!gamepad || !gamepad.vibrationActuator) {
if (!gamepad?.vibrationActuator) {
return { supported: false, type: null };
}

Expand All @@ -447,7 +448,7 @@ class GamepadHelper {
vibrate(gamepad, options = {}) {
const { weakMagnitude = 0.5, strongMagnitude = 0.5, duration = 1000, startDelay = 0 } = options;

if (!gamepad || !gamepad.vibrationActuator) {
if (!gamepad?.vibrationActuator) {
return Promise.reject(new Error('Vibration not supported on this gamepad'));
}

Expand Down Expand Up @@ -504,7 +505,7 @@ class GamepadHelper {
* @returns {Promise<GamepadHapticsResult|Error>} Promise that resolves when vibration stops
*/
stopVibration(gamepad) {
if (gamepad && gamepad.vibrationActuator) {
if (gamepad?.vibrationActuator) {
return this.vibrate(gamepad, { weakMagnitude: 0, strongMagnitude: 0 });
}

Expand All @@ -524,8 +525,8 @@ class GamepadHelper {
}

// Expose to the global scope
if (typeof window !== 'undefined') {
window.GamepadHelper = GamepadHelper;
if (globalThis.window) {
globalThis.GamepadHelper = GamepadHelper;
}

// Export the GamepadHelper class
Expand Down
10 changes: 5 additions & 5 deletions tests/gamepad-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('GamepadHelper', () => {

beforeEach(() => {
// Setup basic mock of navigator.getGamepads
global.navigator.getGamepads = jest.fn();
globalThis.navigator.getGamepads = jest.fn();

// Create a new helper instance before each test
helper = new GamepadHelper();
Expand All @@ -24,7 +24,7 @@ describe('GamepadHelper', () => {
id: "Xbox Wireless Controller",
index: 0,
connected: true,
buttons: Array(17).fill({ pressed: false, value: 0 }),
buttons: new Array(17).fill({ pressed: false, value: 0 }),
axes: [0, 0, 0, 0],
mapping: "standard",
vibrationActuator: {
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('GamepadHelper', () => {
});

test('returns false when Gamepad API is not supported', () => {
delete global.navigator.getGamepads;
delete globalThis.navigator.getGamepads;
expect(helper.isSupported()).toBe(false);
});
});
Expand Down Expand Up @@ -291,13 +291,13 @@ describe('GamepadHelper', () => {

describe('getConnectedGamepads', () => {
test('returns empty array when API not supported', () => {
delete global.navigator.getGamepads;
delete globalThis.navigator.getGamepads;
expect(helper.getConnectedGamepads()).toEqual([]);
});

test('returns array of connected gamepads', () => {
const mockGamepads = [mockGamepad, null, { id: 'gamepad-2', index: 2 }, null];
global.navigator.getGamepads.mockReturnValue(mockGamepads);
globalThis.navigator.getGamepads.mockReturnValue(mockGamepads);

const result = helper.getConnectedGamepads();
expect(result).toEqual([mockGamepad, { id: 'gamepad-2', index: 2 }]);
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path');
const path = require('node:path');
const { codecovWebpackPlugin } = require("@codecov/webpack-plugin");

let production = process.env.NODE_ENV === 'production';
Expand Down