Skip to content

Commit

Permalink
Change usage of window
Browse files Browse the repository at this point in the history
  • Loading branch information
elliothershberg committed Mar 18, 2021
1 parent cf315fb commit 9523379
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/core/rpc/BaseRpcDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/rpc/ElectronRpcDriver.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 => {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/util/calculateStaticBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions packages/core/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/util/io/ElectronLocalFile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { GenericFilehandle, FilehandleOptions } from 'generic-filehandle'

declare global {
interface Window {
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
Expand Down
4 changes: 2 additions & 2 deletions packages/core/util/io/ElectronRemoteFile.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 9523379

Please sign in to comment.