Skip to content

Commit

Permalink
Use getWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
r100-stack committed Sep 19, 2024
1 parent 43f1374 commit 5da5303
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions packages/itwinui-react/src/utils/hooks/useWarningLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as React from 'react';
import { isUnitTest } from '../functions/dev.js';
import { getWindow } from '../functions/dom.js';

const _React = React as any;
const ReactInternals =
Expand All @@ -28,7 +29,7 @@ export const useWarningLogger =
process.env.NODE_ENV === 'development' && !isUnitTest
? function () {
const loggedRef = React.useRef(false);
const timeoutRef = React.useRef<number | null>(null);
const timeoutRef = React.useRef<number | undefined>(undefined);

// https://stackoverflow.com/a/71685253
const stack =
Expand All @@ -43,13 +44,8 @@ export const useWarningLogger =

const logWarning = React.useCallback(
(message: string) => {
// Bail if window not available (e.g. on server)
if (typeof window === 'undefined') {
return;
}

// Using setTimeout to delay execution until after rendering is complete.
timeoutRef.current = window.setTimeout(() => {
timeoutRef.current = getWindow()?.setTimeout(() => {
if (!loggedRef.current) {
console.error(prefix, message);
loggedRef.current = true;
Expand All @@ -63,13 +59,8 @@ export const useWarningLogger =
// Clearing timeout on unmount to avoid double execution in StrictMode.
// The warning should be logged only once per component instance.
return () => {
// Bail if window not available (e.g. on server)
if (typeof window === 'undefined') {
return;
}

if (timeoutRef.current) {
window.clearTimeout(timeoutRef.current);
getWindow()?.clearTimeout(timeoutRef.current);
}
};
}, []);
Expand Down

0 comments on commit 5da5303

Please sign in to comment.