Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check global process at module level #215

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading