Skip to content

Commit 2ef842f

Browse files
authored
Merge pull request #218 from microbit-foundation/reafactor-compatability-checker
Reafactor compatability checker
2 parents 93f8946 + 3520fc1 commit 2ef842f

File tree

4 files changed

+44
-48
lines changed

4 files changed

+44
-48
lines changed

src/App.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import { fade } from 'svelte/transition';
3232
import { 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/script/compatibility/CompatibilityChecker.ts

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,45 @@ export type CompatibilityStatus = {
1212
bluetooth: boolean;
1313
usb: boolean;
1414
platformAllowed: boolean;
15-
webGL: boolean
15+
webGL: boolean;
1616
};
1717

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-
};
18+
export function checkCompatibility(): CompatibilityStatus {
19+
if (localStorage.getItem('isTesting')) {
20+
return { bluetooth: true, usb: true, platformAllowed: true, webGL: true };
5821
}
22+
const browser = Bowser.getParser(window.navigator.userAgent);
23+
const browserName = browser.getBrowser().name ?? 'unknown';
24+
const osName = browser.getOS().name ?? 'unknown';
25+
26+
const canvas = document.createElement("canvas")
27+
// TODO: Handle webgl1 vs webgl2 in relation to threejs
28+
const webGL = canvas.getContext("webgl") instanceof WebGLRenderingContext
29+
30+
const browserVersion = browser.getBrowserVersion();
31+
if (!browserVersion) {
32+
return { bluetooth: false, usb: false, platformAllowed: true, webGL: webGL };
33+
}
34+
const majorVersion = browser.getBrowserVersion().split('.')[0];
35+
const minorVersion = browser.getBrowserVersion().split('.')[1];
36+
const semVer: SemVer = new SemVerImpl(majorVersion, minorVersion);
37+
const isBluetoothSupported = BTComp.isVersionSupported(browserName, semVer, osName);
38+
39+
const isUsbSupported = USBComp.isVersionSupported(browserName, semVer, osName);
40+
let platformType = browser.getPlatform().type;
41+
42+
// If platform won't report what it is, just assume desktop (ChromeOS doesnt report it)
43+
if (platformType == undefined) {
44+
platformType = 'desktop';
45+
}
46+
const isPlatformAllowed =
47+
Environment.isInDevelopment || !nonAllowedPlatforms.includes(platformType);
48+
49+
return {
50+
bluetooth: isBluetoothSupported,
51+
usb: isUsbSupported,
52+
platformAllowed: isPlatformAllowed,
53+
webGL: webGL,
54+
};
5955
}
6056

61-
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { get, writable } from 'svelte/store';
2-
import CompatibilityChecker, { type CompatibilityStatus } from '../compatibility/CompatibilityChecker';
2+
import { type CompatibilityStatus, checkCompatibility} from '../compatibility/CompatibilityChecker';
33
import { t } from '../../i18n';
44
import { gestures } from './mlStore';
55
import { DeviceRequestStates } from './connectDialogStore';
6-
import exp from 'constants';
76

87
// TODO: Rename? Split up further?
98

109
let text: (key: string, vars?: object) => string;
1110
t.subscribe(t => (text = t));
1211

1312
export const compatibility = writable<CompatibilityStatus>(
14-
CompatibilityChecker.checkCompatibility(),
13+
checkCompatibility(),
1514
);
15+
1616
export const isBluetoothWarningDialogOpen = writable<boolean>(
1717
get(compatibility) ? !get(compatibility).bluetooth : false,
1818
);

0 commit comments

Comments
 (0)