|
| 1 | +/** |
| 2 | + * ESM re-export of useSyncExternalStoreWithSelector. |
| 3 | + * |
| 4 | + * The CJS shim package uses `require("react")` which produces a |
| 5 | + * Rolldown `require` polyfill incompatible with Next.js Turbopack SSR. |
| 6 | + * This module provides a pure-ESM implementation using React 18+ |
| 7 | + * native useSyncExternalStore. |
| 8 | + */ |
| 9 | +import { useRef, useEffect, useMemo, useDebugValue, useSyncExternalStore } from 'react'; |
| 10 | + |
| 11 | +function is(x: unknown, y: unknown): boolean { |
| 12 | + return (x === y && (0 !== x || 1 / (x as number) === 1 / (y as number))) || (x !== x && y !== y); |
| 13 | +} |
| 14 | + |
| 15 | +const objectIs: (x: unknown, y: unknown) => boolean = |
| 16 | + typeof Object.is === 'function' ? Object.is : is; |
| 17 | + |
| 18 | +export function useSyncExternalStoreWithSelector<Snapshot, Selection>( |
| 19 | + subscribe: (onStoreChange: () => void) => () => void, |
| 20 | + getSnapshot: () => Snapshot, |
| 21 | + getServerSnapshot: undefined | null | (() => Snapshot), |
| 22 | + selector: (snapshot: Snapshot) => Selection, |
| 23 | + isEqual?: (a: Selection, b: Selection) => boolean, |
| 24 | +): Selection { |
| 25 | + const instRef = useRef<{ |
| 26 | + hasValue: boolean; |
| 27 | + value: Selection; |
| 28 | + } | null>(null); |
| 29 | + let inst: { hasValue: boolean; value: Selection }; |
| 30 | + if (instRef.current === null) { |
| 31 | + inst = { hasValue: false, value: null as Selection }; |
| 32 | + instRef.current = inst; |
| 33 | + } else { |
| 34 | + inst = instRef.current; |
| 35 | + } |
| 36 | + |
| 37 | + const [getSelection, getServerSelection] = useMemo(() => { |
| 38 | + let hasMemo = false; |
| 39 | + let memoizedSnapshot: Snapshot; |
| 40 | + let memoizedSelection: Selection; |
| 41 | + const memoizedSelector = (nextSnapshot: Snapshot): Selection => { |
| 42 | + if (!hasMemo) { |
| 43 | + hasMemo = true; |
| 44 | + memoizedSnapshot = nextSnapshot; |
| 45 | + const nextSelection = selector(nextSnapshot); |
| 46 | + if (isEqual !== undefined) { |
| 47 | + if (inst.hasValue) { |
| 48 | + const currentSelection = inst.value; |
| 49 | + if (isEqual(currentSelection, nextSelection)) { |
| 50 | + memoizedSelection = currentSelection; |
| 51 | + return currentSelection; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + memoizedSelection = nextSelection; |
| 56 | + return nextSelection; |
| 57 | + } |
| 58 | + const prevSnapshot = memoizedSnapshot; |
| 59 | + const prevSelection = memoizedSelection; |
| 60 | + if (objectIs(prevSnapshot, nextSnapshot)) { |
| 61 | + return prevSelection; |
| 62 | + } |
| 63 | + const nextSelection = selector(nextSnapshot); |
| 64 | + if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) { |
| 65 | + memoizedSnapshot = nextSnapshot; |
| 66 | + return prevSelection; |
| 67 | + } |
| 68 | + memoizedSnapshot = nextSnapshot; |
| 69 | + memoizedSelection = nextSelection; |
| 70 | + return nextSelection; |
| 71 | + }; |
| 72 | + const maybeGetServerSelection = |
| 73 | + getServerSnapshot === undefined || getServerSnapshot === null |
| 74 | + ? undefined |
| 75 | + : () => memoizedSelector(getServerSnapshot()); |
| 76 | + return [() => memoizedSelector(getSnapshot()), maybeGetServerSelection]; |
| 77 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 78 | + }, [getSnapshot, getServerSnapshot, selector, isEqual]); |
| 79 | + |
| 80 | + const value = useSyncExternalStore(subscribe, getSelection, getServerSelection); |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + inst.hasValue = true; |
| 84 | + inst.value = value; |
| 85 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 86 | + }, [value]); |
| 87 | + |
| 88 | + useDebugValue(value); |
| 89 | + return value; |
| 90 | +} |
0 commit comments