Skip to content

Commit

Permalink
fix: check global process at module level (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Sep 10, 2024
1 parent 610367e commit 0c557d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/createContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<State, Update extends AnyFunction> = {
Expand Down Expand Up @@ -69,7 +71,7 @@ export const createContainer = <State, Update extends AnyFunction, Props>(
};

const useSelector = <Selected>(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) {
Expand All @@ -91,7 +93,7 @@ export const createContainer = <State, Update extends AnyFunction, Props>(
const useUpdate = concurrentMode
? () => {
if (
typeof process === 'object' &&
hasGlobalProcess &&
process.env.NODE_ENV !== 'production' &&
useContextOrig(UpdateContext) === undefined
) {
Expand Down
4 changes: 3 additions & 1 deletion src/createTrackedSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createProxy, isChanged } from 'proxy-compare';

import { useAffectedDebugValue } from './utils.js';

const hasGlobalProcess = typeof process === 'object';

export const createTrackedSelector = <State>(
useSelector: <Selected>(selector: (state: State) => Selected) => Selected,
) => {
Expand Down Expand Up @@ -38,7 +40,7 @@ export const createTrackedSelector = <State>(
[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);
}
Expand Down

0 comments on commit 0c557d4

Please sign in to comment.