From 0c557d46d37ce9d00886e30218ad9b67342e8ae0 Mon Sep 17 00:00:00 2001 From: Daishi Kato Date: Tue, 10 Sep 2024 09:07:43 +0900 Subject: [PATCH] fix: check global process at module level (#215) --- src/createContainer.ts | 6 ++++-- src/createTrackedSelector.ts | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/createContainer.ts b/src/createContainer.ts index 46982df6..5617f763 100644 --- a/src/createContainer.ts +++ b/src/createContainer.ts @@ -16,6 +16,8 @@ import type { Context } from 'use-context-selector'; import { createTrackedSelector } from './createTrackedSelector.js'; +const hasGlobalProcess = typeof process === 'object'; + // eslint-disable-next-line @typescript-eslint/no-explicit-any type AnyFunction = (...args: any[]) => any; type Options = { @@ -69,7 +71,7 @@ export const createContainer = ( }; const useSelector = (selector: (state: State) => Selected) => { - if (typeof process === 'object' && process.env.NODE_ENV !== 'production') { + if (hasGlobalProcess && process.env.NODE_ENV !== 'production') { const selectorOrig = selector; selector = (state: State) => { if (state === undefined) { @@ -91,7 +93,7 @@ export const createContainer = ( const useUpdate = concurrentMode ? () => { if ( - typeof process === 'object' && + hasGlobalProcess && process.env.NODE_ENV !== 'production' && useContextOrig(UpdateContext) === undefined ) { diff --git a/src/createTrackedSelector.ts b/src/createTrackedSelector.ts index fe0ec28f..31e41844 100644 --- a/src/createTrackedSelector.ts +++ b/src/createTrackedSelector.ts @@ -3,6 +3,8 @@ import { createProxy, isChanged } from 'proxy-compare'; import { useAffectedDebugValue } from './utils.js'; +const hasGlobalProcess = typeof process === 'object'; + export const createTrackedSelector = ( useSelector: (selector: (state: State) => Selected) => Selected, ) => { @@ -38,7 +40,7 @@ export const createTrackedSelector = ( [affected], ); const state = useSelector(selector); - if (typeof process === 'object' && process.env.NODE_ENV !== 'production') { + if (hasGlobalProcess && process.env.NODE_ENV !== 'production') { // eslint-disable-next-line react-hooks/rules-of-hooks useAffectedDebugValue(state, affected); }