Clean up environment detection code
This commit is contained in:
parent
5e6f057c4c
commit
e5b2d737b4
15 changed files with 51 additions and 78 deletions
|
@ -1,15 +1,13 @@
|
|||
import { haveWindow } from "@/next/env";
|
||||
import { styled } from "@mui/material";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { runningInBrowser } from "utils/common";
|
||||
import { MapButton } from "./MapButton";
|
||||
|
||||
import { t } from "i18next";
|
||||
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.webpack.css"; // Re-uses images from ~leaflet package
|
||||
import "leaflet/dist/leaflet.css";
|
||||
runningInBrowser() && require("leaflet-defaulticon-compatibility");
|
||||
const L = runningInBrowser()
|
||||
? (require("leaflet") as typeof import("leaflet"))
|
||||
: null;
|
||||
haveWindow && require("leaflet-defaulticon-compatibility");
|
||||
const L = haveWindow ? (require("leaflet") as typeof import("leaflet")) : null;
|
||||
|
||||
const LAYER_TILE_URL = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
|
||||
const LAYER_TILE_ATTRIBUTION =
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { haveWindow } from "@/next/env";
|
||||
import { getDedicatedCryptoWorker } from "@ente/shared/crypto";
|
||||
import { DedicatedCryptoWorker } from "@ente/shared/crypto/internal/crypto.worker";
|
||||
import { addLogLine } from "@ente/shared/logging";
|
||||
|
@ -24,7 +25,6 @@ import {
|
|||
SceneDetectionMethod,
|
||||
SceneDetectionService,
|
||||
} from "types/machineLearning";
|
||||
import { getConcurrency } from "utils/common/concurrency";
|
||||
import { logQueueStats } from "utils/machineLearning";
|
||||
import arcfaceAlignmentService from "./arcfaceAlignmentService";
|
||||
import arcfaceCropService from "./arcfaceCropService";
|
||||
|
@ -232,3 +232,6 @@ export class LocalMLSyncContext implements MLSyncContext {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const getConcurrency = () =>
|
||||
haveWindow && Math.max(2, Math.ceil(navigator.hardwareConcurrency / 2));
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { haveWindow } from "@/next/env";
|
||||
import { ComlinkWorker } from "@ente/shared/worker/comlinkWorker";
|
||||
import { Remote } from "comlink";
|
||||
import { runningInBrowser } from "utils/common";
|
||||
import { DedicatedConvertWorker } from "worker/convert.worker";
|
||||
|
||||
class ComlinkConvertWorker {
|
||||
|
@ -16,7 +16,7 @@ class ComlinkConvertWorker {
|
|||
}
|
||||
|
||||
export const getDedicatedConvertWorker = () => {
|
||||
if (runningInBrowser()) {
|
||||
if (haveWindow) {
|
||||
const cryptoComlinkWorker = new ComlinkWorker<
|
||||
typeof DedicatedConvertWorker
|
||||
>(
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { haveWindow } from "@/next/env";
|
||||
import { ComlinkWorker } from "@ente/shared/worker/comlinkWorker";
|
||||
import { runningInBrowser } from "utils/common";
|
||||
import { DedicatedMLWorker } from "worker/ml.worker";
|
||||
|
||||
export const getDedicatedMLWorker = (name: string) => {
|
||||
if (runningInBrowser()) {
|
||||
if (haveWindow) {
|
||||
const cryptoComlinkWorker = new ComlinkWorker<typeof DedicatedMLWorker>(
|
||||
name ?? "ente-ml-worker",
|
||||
new Worker(new URL("worker/ml.worker.ts", import.meta.url)),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { haveWindow } from "@/next/env";
|
||||
import { ComlinkWorker } from "@ente/shared/worker/comlinkWorker";
|
||||
import { Remote } from "comlink";
|
||||
import { runningInBrowser } from "utils/common";
|
||||
import { DedicatedSearchWorker } from "worker/search.worker";
|
||||
|
||||
class ComlinkSearchWorker {
|
||||
|
@ -16,7 +16,7 @@ class ComlinkSearchWorker {
|
|||
}
|
||||
|
||||
export const getDedicatedSearchWorker = () => {
|
||||
if (runningInBrowser()) {
|
||||
if (haveWindow) {
|
||||
const cryptoComlinkWorker = new ComlinkWorker<
|
||||
typeof DedicatedSearchWorker
|
||||
>(
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
import { runningInBrowser } from ".";
|
||||
|
||||
export const getConcurrency = () =>
|
||||
runningInBrowser() &&
|
||||
Math.max(2, Math.ceil(navigator.hardwareConcurrency / 2));
|
|
@ -1,7 +1,6 @@
|
|||
import { APP_DOWNLOAD_URL } from "@ente/shared/constants/urls";
|
||||
import { CustomError } from "@ente/shared/error";
|
||||
import { isPromise } from "@ente/shared/utils";
|
||||
import isElectron from "is-electron";
|
||||
|
||||
export function checkConnectivity() {
|
||||
if (navigator.onLine) {
|
||||
|
@ -10,46 +9,6 @@ export function checkConnectivity() {
|
|||
throw new Error(CustomError.NO_INTERNET_CONNECTION);
|
||||
}
|
||||
|
||||
export function runningInBrowser() {
|
||||
return typeof window !== "undefined";
|
||||
}
|
||||
|
||||
export function runningInWorker() {
|
||||
return typeof importScripts === "function";
|
||||
}
|
||||
|
||||
export function runningInElectron() {
|
||||
return isElectron();
|
||||
}
|
||||
|
||||
export function runningInChrome(includeMobile: boolean) {
|
||||
try {
|
||||
const userAgentData = navigator["userAgentData"];
|
||||
const chromeBrand = userAgentData?.brands?.filter(
|
||||
(b) => b.brand === "Google Chrome" || b.brand === "Chromium",
|
||||
)?.[0];
|
||||
return chromeBrand && (includeMobile || userAgentData.mobile === false);
|
||||
} catch (error) {
|
||||
console.error("Error in runningInChrome: ", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function offscreenCanvasSupported() {
|
||||
return !(typeof OffscreenCanvas === "undefined");
|
||||
}
|
||||
|
||||
export function webglSupported() {
|
||||
try {
|
||||
const canvas = document.createElement("canvas");
|
||||
const gl = canvas.getContext("webgl");
|
||||
return gl && gl instanceof WebGLRenderingContext;
|
||||
} catch (error) {
|
||||
console.error("Error in webglSupported: ", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function downloadApp() {
|
||||
openLink(APP_DOWNLOAD_URL, true);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { haveWindow, inElectron } from "@/next/env";
|
||||
import { addLogLine } from "@ente/shared/logging";
|
||||
import { logError } from "@ente/shared/sentry";
|
||||
import {
|
||||
|
@ -23,7 +24,6 @@ import {
|
|||
Thing,
|
||||
} from "types/machineLearning";
|
||||
import { IndexStatus } from "types/machineLearning/ui";
|
||||
import { runningInBrowser, runningInElectron } from "utils/common";
|
||||
|
||||
interface Config {}
|
||||
|
||||
|
@ -64,7 +64,7 @@ class MLIDbStorage {
|
|||
public _db: Promise<IDBPDatabase<MLDb>>;
|
||||
|
||||
constructor() {
|
||||
if (!runningInBrowser() || !runningInElectron()) {
|
||||
if (!haveWindow || !inElectron) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import isElectron from "is-electron";
|
||||
|
||||
/**
|
||||
* A build is considered as a development build if either the NODE_ENV is
|
||||
* environment variable is set to 'development'.
|
||||
|
@ -10,3 +12,28 @@
|
|||
* all other commands.
|
||||
*/
|
||||
export const isDevBuild = process.env.NODE_ENV === "development";
|
||||
|
||||
/**
|
||||
* `true` if we're running in the default global context (aka the main thread)
|
||||
* of a web browser.
|
||||
*
|
||||
* In particular, this is `false` when we're running in a Web Worker,
|
||||
* irrespecitve of whether the worker is running in a Node.js context or a web
|
||||
* browser context.
|
||||
*
|
||||
* > We can be running in a browser context either if the user has the page open
|
||||
* in a web browser, or if we're the renderer process of an Electron app.
|
||||
*/
|
||||
export const haveWindow = typeof window !== "undefined";
|
||||
|
||||
/**
|
||||
* Return true if we are running in a [Web
|
||||
* Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API)
|
||||
*/
|
||||
export const inWorker = typeof importScripts === "function";
|
||||
|
||||
/**
|
||||
* Return true if we're running in an Electron app (either main or renderer
|
||||
* process).
|
||||
*/
|
||||
export const inElectron = isElectron();
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
"get-user-locale": "^2.3",
|
||||
"i18next": "^23.10",
|
||||
"i18next-resources-to-backend": "^1.2.0",
|
||||
"is-electron": "^2.2",
|
||||
"next": "^14.1",
|
||||
"react": "^18",
|
||||
"react-dom": "^18",
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
{
|
||||
"extends": "@/build-config/tsconfig-typecheck.json",
|
||||
"compilerOptions": {
|
||||
/* Also indicate expectation of a WebWorker runtime */
|
||||
"lib": ["ESnext", "DOM", "DOM.Iterable", "WebWorker"]
|
||||
},
|
||||
/* Typecheck all files with the given extensions (here or in subfolders) */
|
||||
"include": ["**/*.ts", "**/*.tsx"]
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { runningInWorker } from "@ente/shared/platform";
|
||||
import { inWorker } from "@/next/env";
|
||||
import * as Comlink from "comlink";
|
||||
import { wrap } from "comlink";
|
||||
import { ElectronAPIsType } from "./types";
|
||||
|
@ -17,7 +17,7 @@ class WorkerSafeElectronServiceImpl implements LimitedElectronAPIs {
|
|||
this.ready = this.init();
|
||||
}
|
||||
private async init() {
|
||||
if (runningInWorker()) {
|
||||
if (inWorker) {
|
||||
const workerSafeElectronClient =
|
||||
wrap<typeof WorkerSafeElectronClient>(self);
|
||||
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
import isElectron from "is-electron";
|
||||
|
||||
export function runningInBrowser() {
|
||||
return typeof window !== "undefined";
|
||||
}
|
||||
|
||||
export function runningInWorker() {
|
||||
return typeof importScripts === "function";
|
||||
}
|
||||
|
||||
export function runningInElectron() {
|
||||
return isElectron();
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
import { runningInBrowser } from "../../platform";
|
||||
import { haveWindow } from "@/next/env";
|
||||
|
||||
import localForage from "localforage";
|
||||
|
||||
if (runningInBrowser()) {
|
||||
if (haveWindow) {
|
||||
localForage.config({
|
||||
name: "ente-files",
|
||||
version: 1.0,
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"is-electron": "^2.2",
|
||||
"libsodium-wrappers": "0.7.9",
|
||||
"yup": "^1.4"
|
||||
},
|
||||
|
|
Loading…
Add table
Reference in a new issue