Skip to content

Commit eeb637d

Browse files
Clean up some boilerplate
1 parent ad52fbd commit eeb637d

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

injected/src/features/web-compat.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,17 @@ export class WebCompat extends ContentFeature {
867867
return deviceInfo;
868868
}
869869

870+
/**
871+
* Helper to wrap a promise with timeout
872+
* @param {Promise} promise - Promise to wrap
873+
* @param {number} timeoutMs - Timeout in milliseconds
874+
* @returns {Promise} Promise that rejects on timeout
875+
*/
876+
withTimeout(promise, timeoutMs) {
877+
const timeout = new Promise((_resolve, reject) => setTimeout(() => reject(new Error('Request timeout')), timeoutMs));
878+
return Promise.race([promise, timeout]);
879+
}
880+
870881
/**
871882
* Fixes device enumeration to handle permission prompts gracefully
872883
*/
@@ -883,27 +894,13 @@ export class WebCompat extends ContentFeature {
883894
* @returns {Promise<MediaDeviceInfo[]>}
884895
*/
885896
apply: async (target, thisArg, args) => {
886-
// Get timeout settings - enabled by default with 5 second timeout
887897
const settings = this.getFeatureSetting('enumerateDevices') || {};
888-
const timeoutEnabled = settings.timeoutEnabled !== false; // enabled by default
889-
const timeoutMs = typeof settings.timeoutMs === 'number' ? settings.timeoutMs : 5000;
898+
const timeoutEnabled = settings.timeoutEnabled !== false;
899+
const timeoutMs = settings.timeoutMs ?? 2000;
890900

891901
try {
892-
let response;
893-
894-
if (timeoutEnabled) {
895-
// Create a timeout promise
896-
const timeoutPromise = new Promise((_resolve, reject) => {
897-
setTimeout(() => reject(new Error('Device enumeration request timeout')), timeoutMs);
898-
});
899-
900-
// Race the messaging request against the timeout
901-
const messagingPromise = this.messaging.request(MSG_DEVICE_ENUMERATION, {});
902-
response = await Promise.race([messagingPromise, timeoutPromise]);
903-
} else {
904-
// No timeout, use original behavior
905-
response = await this.messaging.request(MSG_DEVICE_ENUMERATION, {});
906-
}
902+
const messagingPromise = this.messaging.request(MSG_DEVICE_ENUMERATION, {});
903+
const response = timeoutEnabled ? await this.withTimeout(messagingPromise, timeoutMs) : await messagingPromise;
907904

908905
// Check if native indicates that prompts would be required
909906
if (response.willPrompt) {

0 commit comments

Comments
 (0)