Cannot be a compile time constant

This commit is contained in:
Manav Rathi 2024-04-07 10:09:19 +05:30
parent e5b2d737b4
commit 01e6e79819
No known key found for this signature in database
9 changed files with 21 additions and 20 deletions

View file

@ -6,8 +6,10 @@ 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";
haveWindow && require("leaflet-defaulticon-compatibility");
const L = haveWindow ? (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 =

View file

@ -234,4 +234,4 @@ export class LocalMLSyncContext implements MLSyncContext {
}
export const getConcurrency = () =>
haveWindow && Math.max(2, Math.ceil(navigator.hardwareConcurrency / 2));
haveWindow() && Math.max(2, Math.ceil(navigator.hardwareConcurrency / 2));

View file

@ -16,7 +16,7 @@ class ComlinkConvertWorker {
}
export const getDedicatedConvertWorker = () => {
if (haveWindow) {
if (haveWindow()) {
const cryptoComlinkWorker = new ComlinkWorker<
typeof DedicatedConvertWorker
>(

View file

@ -3,7 +3,7 @@ import { ComlinkWorker } from "@ente/shared/worker/comlinkWorker";
import { DedicatedMLWorker } from "worker/ml.worker";
export const getDedicatedMLWorker = (name: string) => {
if (haveWindow) {
if (haveWindow()) {
const cryptoComlinkWorker = new ComlinkWorker<typeof DedicatedMLWorker>(
name ?? "ente-ml-worker",
new Worker(new URL("worker/ml.worker.ts", import.meta.url)),

View file

@ -16,7 +16,7 @@ class ComlinkSearchWorker {
}
export const getDedicatedSearchWorker = () => {
if (haveWindow) {
if (haveWindow()) {
const cryptoComlinkWorker = new ComlinkWorker<
typeof DedicatedSearchWorker
>(

View file

@ -1,4 +1,5 @@
import { haveWindow, inElectron } from "@/next/env";
import { haveWindow } from "@/next/env";
import isElectron from "is-electron";
import { addLogLine } from "@ente/shared/logging";
import { logError } from "@ente/shared/sentry";
import {
@ -64,7 +65,7 @@ class MLIDbStorage {
public _db: Promise<IDBPDatabase<MLDb>>;
constructor() {
if (!haveWindow || !inElectron) {
if (!haveWindow() || !isElectron()) {
return;
}

View file

@ -1,5 +1,3 @@
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'.
@ -23,17 +21,17 @@ export const isDevBuild = process.env.NODE_ENV === "development";
*
* > 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.
*
* Note that this cannot be a constant, otherwise it'll get inlined during SSR
* with the wrong value.
*/
export const haveWindow = typeof window !== "undefined";
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)
*
* Note that this cannot be a constant, otherwise it'll get inlined during SSR
* with the wrong value.
*/
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();
export const inWorker = () => typeof importScripts === "function";

View file

@ -17,7 +17,7 @@ class WorkerSafeElectronServiceImpl implements LimitedElectronAPIs {
this.ready = this.init();
}
private async init() {
if (inWorker) {
if (inWorker()) {
const workerSafeElectronClient =
wrap<typeof WorkerSafeElectronClient>(self);

View file

@ -2,7 +2,7 @@ import { haveWindow } from "@/next/env";
import localForage from "localforage";
if (haveWindow) {
if (haveWindow()) {
localForage.config({
name: "ente-files",
version: 1.0,