Skip to content

Commit 4883e33

Browse files
committed
refactor(store): minor simplify
1 parent 5eb1648 commit 4883e33

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/app/store/selector/useSyncExternalStoreWithSelector.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
// https://github.com/facebook/react/blob/8d74e8c73a5cc5e461bb1413a74c6b058c6be134/packages/use-sync-external-store/src/useSyncExternalStoreWithSelector.js
44
// MIT Licensed https://github.com/facebook/react/blob/be8aa76873e231555676483a36534bb48ad1b1a3/LICENSE
55

6-
import { useDebugValue, useEffect, useMemo, useRef, useSyncExternalStore } from 'react'
6+
import { useEffect, useMemo, useRef, useSyncExternalStore } from 'react'
77

88
const NIL = Symbol('NIL')
99

1010
export function useSyncExternalStoreWithSelector<Snapshot, Selection>(
1111
subscribe: (onStoreChange: () => void) => () => void,
1212
getSnapshot: () => Snapshot,
1313
selector: (snapshot: Snapshot) => Selection,
14-
isEqual: (a: Selection, b: Selection) => boolean,
14+
isEqual: (a: Selection, b: Selection) => boolean = Object.is,
1515
): Selection {
1616
// Use this to track the rendered snapshot.
17-
const selectionRef = useRef<Selection | typeof NIL>(NIL)
17+
const instRef = useRef<Selection | typeof NIL>(NIL)
1818

1919
const getSelection = useMemo(() => {
2020
// Track the memoized state using closure variables that are local to this
@@ -33,10 +33,12 @@ export function useSyncExternalStoreWithSelector<Snapshot, Selection>(
3333
// Even if the selector has changed, the currently rendered selection
3434
// may be equal to the new selection. We should attempt to reuse the
3535
// current value if possible, to preserve downstream memoizations.
36-
const currentSelection = selectionRef.current
37-
if (currentSelection !== NIL && isEqual(currentSelection, nextSelection)) {
38-
memoizedSelection = currentSelection
39-
return currentSelection
36+
if (instRef.current !== NIL) {
37+
const currentSelection = instRef.current
38+
if (isEqual(currentSelection, nextSelection)) {
39+
memoizedSelection = currentSelection
40+
return currentSelection
41+
}
4042
}
4143
memoizedSelection = nextSelection
4244
return nextSelection
@@ -77,9 +79,8 @@ export function useSyncExternalStoreWithSelector<Snapshot, Selection>(
7779
const selection = useSyncExternalStore(subscribe, getSelection)
7880

7981
useEffect(() => {
80-
selectionRef.current = selection
82+
instRef.current = selection
8183
}, [selection])
8284

83-
useDebugValue(selection)
8485
return selection
8586
}

0 commit comments

Comments
 (0)