Skip to content

Commit

Permalink
refactor: be safer around referring to globals
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpatiiuk committed Feb 19, 2024
1 parent 23ab9bd commit 353a0ea
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 14 deletions.
4 changes: 3 additions & 1 deletion specifyweb/frontend/js_src/lib/components/Atoms/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export const Key = wrap(
'bg-gray-200 border-1 dark:border-none dark:bg-neutral-700 rounded-sm mx-1 p-0.5'
);

const defaultOneRem = 16;
export const oneRem = Number.parseFloat(
getComputedStyle(document.documentElement).fontSize
globalThis.getComputedStyle?.(document.documentElement).fontSize ??
defaultOneRem
);
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function entrypoint(): void {
console.log(entrypointName);
unlockInitialContext(entrypointName);

globalThis.window.addEventListener('load', () => {
globalThis.addEventListener?.('load', () => {
const root = document.getElementById('root');
const portalRoot = document.getElementById('portal-root');
if (root === null || portalRoot === null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export function OnlineStatus(): JSX.Element | null {
setShowOnlineStatus(false);
}

window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
globalThis.addEventListener('online', handleOnline);
globalThis.addEventListener('offline', handleOffline);

return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
globalThis.removeEventListener('online', handleOnline);
globalThis.removeEventListener('offline', handleOffline);
};
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ const dialogIndexes = new Set<number>();
const getNextIndex = (): number =>
dialogIndexes.size === 0 ? initialIndex : Math.max(...dialogIndexes) + 1;

const supportsBackdropBlur = globalThis.CSS.supports(
'((-webkit-backdrop-filter: none) or (backdrop-filter: none))'
);
const supportsBackdropBlur =
globalThis.CSS?.supports(
'((-webkit-backdrop-filter: none) or (backdrop-filter: none))'
) ?? false;

// Used for 'inert' attribute addition
const root = document.getElementById('root');
const root = globalThis.document?.getElementById('root');

/**
* Modal or non-modal dialog. Highly customizable. Used all over the place
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function GenericGeoLocate({
handleUpdate?.({ latitude, longitude, uncertainty, polygon });
}

globalThis.window.addEventListener('message', listener);
globalThis.addEventListener('message', listener);
return (): void => globalThis.removeEventListener('message', listener);
}, [handleUpdate]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ let delayFocusIn = 400;
/** Delay before showing tooltip if using mouse */
let delayMouseIn = 1000;
// Disable delays on touch screen devices
window?.addEventListener(
globalThis.addEventListener?.(
'touchstart',
() => {
delayFocusIn = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class WbView extends Backbone.View {
this.wbUtils.toggleCellTypes('invalidCells', 'remove');

this.cells.flushIndexedCellData = true;
globalThis.window.addEventListener('resize', this.handleResize);
globalThis.addEventListener('resize', this.handleResize);

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion specifyweb/frontend/js_src/lib/utils/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let eventListenerIsInitialized = false;

/** Listen for changes to localStorage from other tabs */
function initialize(): void {
globalThis.addEventListener(
globalThis.addEventListener?.(
'storage',
({ storageArea, key: formattedKey, newValue }) => {
// "key" is null only when running `localStorage.clear()`
Expand Down

0 comments on commit 353a0ea

Please sign in to comment.