Skip to content

Commit

Permalink
feat: add isWeb helper
Browse files Browse the repository at this point in the history
  • Loading branch information
chmelevskij committed Sep 17, 2023
1 parent 44a5ac7 commit 5b7ef3e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/js/msp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import GUI from "./gui.js";
import CONFIGURATOR from "./data_storage.js";
import serialNWJS from "./serial.js";
import serialWeb from "./webSerial.js";
import { isWeb } from "./utils/isWeb.js";

const serial = import.meta.env ? serialWeb : serialNWJS;
const serial = isWeb() ? serialWeb : serialNWJS;

const MSP = {
symbols: {
Expand Down Expand Up @@ -307,7 +308,7 @@ const MSP = {
return bufferOut;
},
send_message(code, data, callback_sent, callback_msp, doCallbackOnError) {
const connected = import.meta.env ? serial.connected : serial.connectionId;
const connected = isWeb ? serial.connected : serial.connectionId;
if (code === undefined || !connected || CONFIGURATOR.virtualMode) {
if (callback_msp) {
callback_msp();
Expand All @@ -324,7 +325,7 @@ const MSP = {
}
}

if (import.meta.env && (code === undefined || !serial.connectionInfo)) {
if (isWeb() && (code === undefined || !serial.connectionInfo)) {
console.log('ERROR: code undefined or no connectionId');
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/js/port_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { get as getConfig } from "./ConfigStorage";
import serial from "./serial";
import MdnsDiscovery from "./mdns_discovery";
import $ from 'jquery';
import { isWeb } from "./utils/isWeb";

const TIMEOUT_CHECK = 500 ; // With 250 it seems that it produces a memory leak and slowdown in some versions, reason unknown

Expand All @@ -32,7 +33,7 @@ PortHandler.initialize = function () {

// currently web build doesn't need port handler,
// so just bail out.
if (import.meta.env) {
if (isWeb()) {
return 'not implemented';
}

Expand Down
7 changes: 4 additions & 3 deletions src/js/serial_backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import BuildApi from "./BuildApi";

import serialNWJS from "./serial.js";
import serialWeb from "./webSerial.js";
import { isWeb } from "./utils/isWeb";

const serial = import.meta.env ? serialWeb : serialNWJS;
const serial = isWeb() ? serialWeb : serialNWJS;

let mspHelper;
let connectionTimestamp;
Expand Down Expand Up @@ -118,7 +119,7 @@ export function initializeSerialBackend() {
CONFIGURATOR.virtualApiVersion = $('#firmware-version-dropdown :selected').val();

serial.connect('virtual', {}, onOpenVirtual);
} else if (import.meta.env) {
} else if (isWeb()) {
// Explicitly disconnect the event listeners before attaching the new ones.
serial.removeEventListener('connect', connectHandler);
serial.addEventListener('connect', connectHandler);
Expand Down Expand Up @@ -327,7 +328,7 @@ function onOpen(openInfo) {
result = getConfig('expertMode')?.expertMode ?? false;
$('input[name="expertModeCheckbox"]').prop('checked', result).trigger('change');

if(import.meta.env) {
if(isWeb()) {
serial.removeEventListener('receive', read_serial_adapter);
serial.addEventListener('receive', read_serial_adapter);
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/js/utils/isWeb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isWeb() {
return !!import.meta.env;
}

0 comments on commit 5b7ef3e

Please sign in to comment.