From bb9735b1084b25b27d8cfb38c92f4a9dac69592c Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 2 Mar 2024 22:57:34 +0900 Subject: [PATCH] breaking: compatibility with memo --- src/react.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/react.ts b/src/react.ts index 91cf9e8c..ec7077ca 100644 --- a/src/react.ts +++ b/src/react.ts @@ -110,8 +110,9 @@ export function useSnapshot( options?: Options, ): Snapshot { const notifyInSync = options?.sync + // per-hook affected, it's not ideal but memo compatible + const affected = useMemo(() => new WeakMap(), []) const lastSnapshot = useRef>() - const lastAffected = useRef>() let inRender = true const currSnapshot = useSyncExternalStore( useCallback( @@ -128,11 +129,10 @@ export function useSnapshot( if ( !inRender && lastSnapshot.current && - lastAffected.current && !isChanged( lastSnapshot.current, nextSnapshot, - lastAffected.current, + affected, new WeakMap(), ) ) { @@ -147,20 +147,13 @@ export function useSnapshot( () => snapshot(proxyObject), ) inRender = false - const currAffected = new WeakMap() useEffect(() => { lastSnapshot.current = currSnapshot - lastAffected.current = currAffected }) if (import.meta.env?.MODE !== 'production') { // eslint-disable-next-line react-hooks/rules-of-hooks - useAffectedDebugValue(currSnapshot, currAffected) + useAffectedDebugValue(currSnapshot, affected) } const proxyCache = useMemo(() => new WeakMap(), []) // per-hook proxyCache - return createProxyToCompare( - currSnapshot, - currAffected, - proxyCache, - targetCache, - ) + return createProxyToCompare(currSnapshot, affected, proxyCache, targetCache) }