Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deviceDetector): ACT-777 replace benchmark to s3, add timeout #88

Merged
merged 1 commit into from
Aug 14, 2024
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
6 changes: 3 additions & 3 deletions src/hooks/useDeviceDetector/use-device-detector.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

// in mbps
const networkTierGrades = {
1: 1,
2: 10,
1: 0.7,
2: 2,
3: Infinity
};

Expand Down Expand Up @@ -83,7 +83,7 @@
gpuTierResult.tier = 3;
}

const downloadSpeed = await checkDownloadSpeed('https://api.readyplayer.me/v3/avatars/editor/benchmark');
const downloadSpeed = await checkDownloadSpeed('https://readyplayerme-avatars.s3.amazonaws.com/benchmark.glb');

let tier = 3;
if (downloadSpeed < networkTierGrades[1]) {
Expand All @@ -97,13 +97,13 @@
tier
};
} catch (error) {
console.error(error);

Check warning on line 100 in src/hooks/useDeviceDetector/use-device-detector.hook.ts

View workflow job for this annotation

GitHub Actions / Unit Test + Linting + Storybook

Unexpected console statement
}

setDeviceDetector(new DeviceDetectorService({ gpuTierResult, networkTierResult, ...options }));
};
fetchDeviceDetector();
}, []);

Check warning on line 106 in src/hooks/useDeviceDetector/use-device-detector.hook.ts

View workflow job for this annotation

GitHub Actions / Unit Test + Linting + Storybook

React Hook useEffect has a missing dependency: 'options'. Either include it or remove the dependency array. If 'setDeviceDetector' needs the current value of 'options', you can also switch to useReducer instead of useState and read 'options' in the reducer

return deviceDetector;
}
24 changes: 16 additions & 8 deletions src/services/DownloadSpeed.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
export async function checkDownloadSpeed(baseUrl: string): Promise<number> {
const startTime = performance.now();

const response = await fetch(baseUrl);
try {
const response = await fetch(baseUrl, { signal: AbortSignal.timeout(4000) });

const data = await response.blob();
const data = await response.blob();

const endTime = performance.now();
const endTime = performance.now();

const duration = (endTime - startTime) / 1000; // convert to seconds
const sizeInBytes = data.size;
const sizeInMegabits = (sizeInBytes * 8) / (1024 * 1024); // convert to megabits
const duration = (endTime - startTime) / 1000; // convert to seconds
const sizeInBytes = data.size;
const sizeInMegabits = (sizeInBytes * 8) / (1024 * 1024); // convert to megabits

const speedMbps = sizeInMegabits / duration;
const speedMbps = sizeInMegabits / duration;

return speedMbps;
return speedMbps;
} catch (error) {
if (['TimeoutError', 'AbortError'].includes((error as Error).name)) {
return 0;
}

return 10;
}
}
Loading