Skip to content

Commit e0c5c3d

Browse files
committed
Rework uSES init to not rely on module initialization order
The previous attempt worked fine in local dev + tests, but failed when built and run in a real app.
1 parent 268e3d1 commit e0c5c3d

File tree

6 files changed

+31
-26
lines changed

6 files changed

+31
-26
lines changed

src/alternate-renderers.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import { useSyncExternalStore } from 'use-sync-external-store/shim'
77
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'
88

9-
import { setSyncFunctions } from './utils/useSyncExternalStore'
9+
import { initializeUseSelector } from './hooks/useSelector'
10+
import { initializeConnect } from './components/connect'
1011

11-
setSyncFunctions(useSyncExternalStore, useSyncExternalStoreWithSelector)
12+
initializeUseSelector(useSyncExternalStoreWithSelector)
13+
initializeConnect(useSyncExternalStore)
1214

1315
import { getBatch } from './utils/batch'
1416

src/compat.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import { useSyncExternalStore } from 'use-sync-external-store/shim'
66
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'
77

8-
import { setSyncFunctions } from './utils/useSyncExternalStore'
98
import { unstable_batchedUpdates as batch } from './utils/reactBatchedUpdates'
109
import { setBatch } from './utils/batch'
1110

12-
setSyncFunctions(useSyncExternalStore, useSyncExternalStoreWithSelector)
11+
import { initializeUseSelector } from './hooks/useSelector'
12+
import { initializeConnect } from './components/connect'
13+
14+
initializeUseSelector(useSyncExternalStoreWithSelector)
15+
initializeConnect(useSyncExternalStore)
1316

1417
// Enable batched updates in our subscriptions for use
1518
// with standard React renderers (ReactDOM, React Native)

src/components/connect.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import defaultMergePropsFactories from '../connect/mergeProps'
2828

2929
import { createSubscription, Subscription } from '../utils/Subscription'
3030
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
31-
import { getSyncFunctions } from '../utils/useSyncExternalStore'
3231
import shallowEqual from '../utils/shallowEqual'
3332

3433
import {
@@ -37,7 +36,13 @@ import {
3736
ReactReduxContextInstance,
3837
} from './Context'
3938

40-
const [useSyncExternalStore] = getSyncFunctions()
39+
import type { uSES } from '../utils/useSyncExternalStore'
40+
import { notInitialized } from '../utils/useSyncExternalStore'
41+
42+
let useSyncExternalStore = notInitialized as uSES
43+
export const initializeConnect = (fn: uSES) => {
44+
useSyncExternalStore = fn
45+
}
4146

4247
// Define some constant arrays just to avoid re-creating these
4348
const EMPTY_ARRAY: [unknown, number] = [null, 0]

src/hooks/useSelector.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ import { useContext, useDebugValue } from 'react'
22

33
import { useReduxContext as useDefaultReduxContext } from './useReduxContext'
44
import { ReactReduxContext } from '../components/Context'
5-
import { getSyncFunctions } from '../utils/useSyncExternalStore'
65
import type { DefaultRootState, EqualityFn } from '../types'
6+
import type { uSESWS } from '../utils/useSyncExternalStore'
7+
import { notInitialized } from '../utils/useSyncExternalStore'
78

8-
const [, useSyncExternalStoreWithSelector] = getSyncFunctions()
9+
let useSyncExternalStoreWithSelector = notInitialized as uSESWS
10+
export const initializeUseSelector = (fn: uSESWS) => {
11+
useSyncExternalStoreWithSelector = fn
12+
}
913

1014
const refEquality: EqualityFn<any> = (a, b) => a === b
1115

src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import { useSyncExternalStore } from 'react'
88
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector'
99

10-
import { setSyncFunctions } from './utils/useSyncExternalStore'
1110
import { unstable_batchedUpdates as batch } from './utils/reactBatchedUpdates'
1211
import { setBatch } from './utils/batch'
1312

14-
setSyncFunctions(useSyncExternalStore, useSyncExternalStoreWithSelector)
13+
import { initializeUseSelector } from './hooks/useSelector'
14+
import { initializeConnect } from './components/connect'
15+
16+
initializeUseSelector(useSyncExternalStoreWithSelector)
17+
initializeConnect(useSyncExternalStore)
1518

1619
// Enable batched updates in our subscriptions for use
1720
// with standard React renderers (ReactDOM, React Native)

src/utils/useSyncExternalStore.ts

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import type { useSyncExternalStore } from 'use-sync-external-store'
22
import type { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector'
33

4-
const notInitialized = () => {
5-
throw new Error('Not initialize!')
4+
export const notInitialized = () => {
5+
throw new Error('uSES not initialized!')
66
}
77

8-
let uSES: typeof useSyncExternalStore = notInitialized
9-
let uSESWS: typeof useSyncExternalStoreWithSelector = notInitialized
10-
11-
// Allow injecting the actual functions from the entry points
12-
export const setSyncFunctions = (
13-
sync: typeof useSyncExternalStore,
14-
withSelector: typeof useSyncExternalStoreWithSelector
15-
) => {
16-
uSES = sync
17-
uSESWS = withSelector
18-
}
19-
20-
// Supply a getter just to skip dealing with ESM bindings
21-
export const getSyncFunctions = () => [uSES, uSESWS] as const
8+
export type uSES = typeof useSyncExternalStore
9+
export type uSESWS = typeof useSyncExternalStoreWithSelector

0 commit comments

Comments
 (0)