Skip to content

Commit 15e84cb

Browse files
committed
Change compatablity checker from static class method to function
1 parent 1d335aa commit 15e84cb

File tree

5 files changed

+78
-51
lines changed

5 files changed

+78
-51
lines changed

src/App.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import BottomBarMenuView from './views/BottomBarMenuView.svelte';
3030
import CookieBanner from './components/cookie-bannner/CookieBanner.svelte';
3131
import { fade } from 'svelte/transition';
32-
import { state } from './script/stores/uiStore';
32+
import { compatibility, state } from './script/stores/uiStore';
3333
import LoadingSpinner from './components/LoadingSpinner.svelte';
34-
import CompatibilityChecker from './script/compatibility/CompatibilityChecker';
34+
import { checkCompatibility } from './script/compatibility/CompatibilityChecker';
3535
import IncompatiblePlatformView from './views/IncompatiblePlatformView.svelte';
3636
import BluetoothIncompatibilityWarningDialog from './components/BluetoothIncompatibilityWarningDialog.svelte';
3737
import CookieManager from './script/CookieManager';
@@ -47,10 +47,11 @@
4747
CookieManager.unsetReconnectFlag();
4848
}
4949
50+
5051
document.title = Environment.pageTitle;
5152
</script>
5253

53-
{#if !CompatibilityChecker.checkCompatibility().platformAllowed}
54+
{#if !checkCompatibility().platformAllowed}
5455
<!-- Denies mobile users access to the platform -->
5556
<IncompatiblePlatformView />
5657
{:else}

src/components/connection-prompt/usb/FindUsbDialog.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
function onFindUsbClick() {
1010
Microbits.linkMicrobit()
1111
.then(() => onFoundUsb())
12-
.catch((e: Error) => {
13-
console.log(e);
12+
.catch(() => {
1413
onUsbLinkError();
1514
});
1615
}

src/script/compatibility/CompatibilityChecker.ts

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,66 @@ export type CompatibilityStatus = {
1212
bluetooth: boolean;
1313
usb: boolean;
1414
platformAllowed: boolean;
15-
webGL: boolean
15+
webGL: boolean,
16+
webUSB: boolean
1617
};
1718

18-
// TODO: Why is this a class? No constructor, no internal state, and only
19-
// a single static method. Might as well just be a function?
20-
class CompatibilityChecker {
21-
public static checkCompatibility(): CompatibilityStatus {
22-
if (localStorage.getItem('isTesting')) {
23-
return { bluetooth: true, usb: true, platformAllowed: true, webGL: true };
24-
}
25-
const browser = Bowser.getParser(window.navigator.userAgent);
26-
const browserName = browser.getBrowser().name ?? 'unknown';
27-
const osName = browser.getOS().name ?? 'unknown';
28-
29-
const canvas = document.createElement("canvas")
30-
// TODO: Handle webgl1 vs webgl2 in relation to threejs
31-
const webGL = canvas.getContext("webgl") instanceof WebGLRenderingContext
32-
33-
const browserVersion = browser.getBrowserVersion();
34-
if (!browserVersion) {
35-
return { bluetooth: false, usb: false, platformAllowed: true, webGL: webGL };
36-
}
37-
const majorVersion = browser.getBrowserVersion().split('.')[0];
38-
const minorVersion = browser.getBrowserVersion().split('.')[1];
39-
const semVer: SemVer = new SemVerImpl(majorVersion, minorVersion);
40-
const isBluetoothSupported = BTComp.isVersionSupported(browserName, semVer, osName);
41-
42-
const isUsbSupported = USBComp.isVersionSupported(browserName, semVer, osName);
43-
let platformType = browser.getPlatform().type;
44-
45-
// If platform won't report what it is, just assume desktop (ChromeOS doesnt report it)
46-
if (platformType == undefined) {
47-
platformType = 'desktop';
48-
}
49-
const isPlatformAllowed =
50-
Environment.isInDevelopment || !nonAllowedPlatforms.includes(platformType);
51-
52-
return {
53-
bluetooth: isBluetoothSupported,
54-
usb: isUsbSupported,
55-
platformAllowed: isPlatformAllowed,
56-
webGL: webGL
57-
};
19+
export function checkCompatibility(): CompatibilityStatus {
20+
if (localStorage.getItem('isTesting')) {
21+
return { bluetooth: true, usb: true, platformAllowed: true, webGL: true, webUSB: true };
22+
}
23+
const browser = Bowser.getParser(window.navigator.userAgent);
24+
const browserName = browser.getBrowser().name ?? 'unknown';
25+
const osName = browser.getOS().name ?? 'unknown';
26+
27+
const canvas = document.createElement("canvas")
28+
// TODO: Handle webgl1 vs webgl2 in relation to threejs
29+
const webGL = canvas.getContext("webgl") instanceof WebGLRenderingContext
30+
31+
const webUSB = false // Check for this is async, so another function is used
32+
// try {
33+
// await navigator.usb.getDevices()
34+
// webUSB = true
35+
// } catch {
36+
// webUSB = false
37+
// }
38+
39+
const browserVersion = browser.getBrowserVersion();
40+
if (!browserVersion) {
41+
return { bluetooth: false, usb: false, platformAllowed: true, webGL: webGL, webUSB };
42+
}
43+
const majorVersion = browser.getBrowserVersion().split('.')[0];
44+
const minorVersion = browser.getBrowserVersion().split('.')[1];
45+
const semVer: SemVer = new SemVerImpl(majorVersion, minorVersion);
46+
const isBluetoothSupported = BTComp.isVersionSupported(browserName, semVer, osName);
47+
48+
const isUsbSupported = USBComp.isVersionSupported(browserName, semVer, osName);
49+
let platformType = browser.getPlatform().type;
50+
51+
// If platform won't report what it is, just assume desktop (ChromeOS doesnt report it)
52+
if (platformType == undefined) {
53+
platformType = 'desktop';
54+
}
55+
const isPlatformAllowed =
56+
Environment.isInDevelopment || !nonAllowedPlatforms.includes(platformType);
57+
58+
return {
59+
bluetooth: isBluetoothSupported,
60+
usb: isUsbSupported,
61+
platformAllowed: isPlatformAllowed,
62+
webGL: webGL,
63+
webUSB: webUSB
64+
};
65+
}
66+
67+
export async function checkUSBCompatibility(): Promise<boolean> {
68+
try {
69+
await navigator.usb.getDevices()
70+
return true
71+
} catch {
72+
return false
5873
}
5974
}
75+
// }
6076

61-
export default CompatibilityChecker;
77+
// export default CompatibilityChecker;

src/script/microbit-interfacing/MicrobitUSB.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MicrobitUSB extends CortexM {
2424
* Open prompt for USB connection.
2525
* @returns {Promise<MicrobitUSB>} A promise that resolves to a new MicrobitUSB object.
2626
*/
27-
public static async requestConnection(): Promise<MicrobitUSB | undefined> {
27+
public static async requestConnection(): Promise<MicrobitUSB> {
2828
const requestOptions: USBDeviceRequestOptions = {
2929
filters: [
3030
{

src/script/stores/uiStore.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import { get, writable } from 'svelte/store';
2-
import CompatibilityChecker, { type CompatibilityStatus } from '../compatibility/CompatibilityChecker';
2+
import {
3+
type CompatibilityStatus,
4+
checkCompatibility,
5+
checkUSBCompatibility
6+
} from '../compatibility/CompatibilityChecker';
37
import { t } from '../../i18n';
48
import { gestures } from './mlStore';
59
import { DeviceRequestStates } from './connectDialogStore';
6-
import exp from 'constants';
710

811
// TODO: Rename? Split up further?
912

1013
let text: (key: string, vars?: object) => string;
1114
t.subscribe(t => (text = t));
1215

1316
export const compatibility = writable<CompatibilityStatus>(
14-
CompatibilityChecker.checkCompatibility(),
17+
checkCompatibility(),
1518
);
19+
20+
void checkUSBCompatibility().then(webUSB =>
21+
compatibility.update(old => {
22+
old.webUSB = webUSB
23+
return old
24+
})
25+
)
26+
1627
export const isBluetoothWarningDialogOpen = writable<boolean>(
1728
get(compatibility) ? !get(compatibility).bluetooth : false,
1829
);

0 commit comments

Comments
 (0)