From 95233790e2a9cc65c8056083ea6516bdb0e48085 Mon Sep 17 00:00:00 2001 From: Elliot Hershberg Date: Thu, 18 Mar 2021 11:02:43 -0700 Subject: [PATCH] Change usage of window --- packages/core/rpc/BaseRpcDriver.ts | 2 +- packages/core/rpc/ElectronRpcDriver.ts | 5 +++-- packages/core/util/calculateStaticBlocks.ts | 5 +++-- packages/core/util/index.ts | 10 ++++++---- packages/core/util/io/ElectronLocalFile.ts | 3 ++- packages/core/util/io/ElectronRemoteFile.ts | 4 ++-- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/core/rpc/BaseRpcDriver.ts b/packages/core/rpc/BaseRpcDriver.ts index ff667508ae..b3e3548646 100644 --- a/packages/core/rpc/BaseRpcDriver.ts +++ b/packages/core/rpc/BaseRpcDriver.ts @@ -46,7 +46,7 @@ export async function watchWorker(worker: WorkerHandle, pingTime: number) { function detectHardwareConcurrency() { const mainThread = typeof window !== 'undefined' - const canDetect = 'hardwareConcurrency' in window.navigator + const canDetect = mainThread && 'hardwareConcurrency' in window.navigator if (mainThread && canDetect) { return window.navigator.hardwareConcurrency } diff --git a/packages/core/rpc/ElectronRpcDriver.ts b/packages/core/rpc/ElectronRpcDriver.ts index 8e417b64a6..5ad3edf4aa 100644 --- a/packages/core/rpc/ElectronRpcDriver.ts +++ b/packages/core/rpc/ElectronRpcDriver.ts @@ -1,4 +1,4 @@ -/* eslint-disable no-await-in-loop */ +/* eslint-disable no-await-in-loop,@typescript-eslint/no-explicit-any */ import shortid from 'shortid' import BaseRpcDriver from './BaseRpcDriver' import PluginManager from '../PluginManager' @@ -12,7 +12,8 @@ declare global { electron?: import('electron').AllElectron } } -const { electronBetterIpc = {}, electron } = window +const { electronBetterIpc = {}, electron } = + typeof window !== 'undefined' ? window : ({} as any) async function wait(ms: number) { return new Promise(resolve => { diff --git a/packages/core/util/calculateStaticBlocks.ts b/packages/core/util/calculateStaticBlocks.ts index bc78b793a4..e7e0ac9037 100644 --- a/packages/core/util/calculateStaticBlocks.ts +++ b/packages/core/util/calculateStaticBlocks.ts @@ -24,8 +24,9 @@ export default function calculateStaticBlocks( // on the main thread, window.innerWidth is used because this reduces // recalculating the blocks, otherwise, model.width for cases such as - // off-main-thread - width = window.innerWidth || model.width, + // off-main-thread. also this is not a ternary because our window.innerWidth + // might be undefined on off-main-thread, so instead use || model.width + width = (typeof window !== 'undefined' && window.innerWidth) || model.width, ) { const { offsetPx, diff --git a/packages/core/util/index.ts b/packages/core/util/index.ts index ed0ebcd0b8..e84a8b63ec 100644 --- a/packages/core/util/index.ts +++ b/packages/core/util/index.ts @@ -861,10 +861,12 @@ export const complement = (() => { // otherwise listens for prerendered_canvas but reads empty pixels, and doesn't // get the contents of the canvas export const rIC = - window.requestIdleCallback || - (typeof jest === 'undefined' - ? (cb: Function) => setTimeout(() => cb(), 1) - : (cb: Function) => cb()) + // eslint-disable-next-line no-nested-ternary + typeof jest === 'undefined' + ? typeof window !== 'undefined' + ? window.requestIdleCallback + : (cb: Function) => setTimeout(() => cb(), 1) + : (cb: Function) => cb() // xref https://gist.github.com/tophtucker/62f93a4658387bb61e4510c37e2e97cf export function measureText(str: string, fontSize = 10) { diff --git a/packages/core/util/io/ElectronLocalFile.ts b/packages/core/util/io/ElectronLocalFile.ts index 454d387c5f..4914a6f8c7 100644 --- a/packages/core/util/io/ElectronLocalFile.ts +++ b/packages/core/util/io/ElectronLocalFile.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { GenericFilehandle, FilehandleOptions } from 'generic-filehandle' declare global { @@ -5,7 +6,7 @@ declare global { electron?: import('electron').AllElectron } } -const { electron } = window +const { electron } = typeof window !== 'undefined' ? window : ({} as any) type PathLike = import('fs').PathLike type Stats = import('fs').Stats diff --git a/packages/core/util/io/ElectronRemoteFile.ts b/packages/core/util/io/ElectronRemoteFile.ts index a2614698c6..63724cc73d 100644 --- a/packages/core/util/io/ElectronRemoteFile.ts +++ b/packages/core/util/io/ElectronRemoteFile.ts @@ -1,4 +1,4 @@ -/* eslint-disable no-underscore-dangle */ +/* eslint-disable @typescript-eslint/no-explicit-any,no-underscore-dangle */ import uri2path from 'file-uri-to-path' import { Fetcher, @@ -15,7 +15,7 @@ declare global { electron?: import('electron').AllElectron } } -const { electron } = window +const { electron } = typeof window !== 'undefined' ? window : ({} as any) class ElectronRemoteFileError extends Error { public status: number | undefined